Skip to content

Commit

Permalink
added better exception message for 'ridge line must intersect...'
Browse files Browse the repository at this point in the history
updated dependencies
added Python 3.10 support
fixed warnings for updated dependencies
  • Loading branch information
internaut committed Mar 4, 2022
1 parent a997fd5 commit 9de01f0
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 23 deletions.
Binary file modified examples/duplicate_points.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/random_points_across_italy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/random_points_across_italy_per_geom_false.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/random_points_and_area.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/random_points_brandenburg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/using_geopandas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/using_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.ops import cascaded_union
from shapely.ops import unary_union

from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords, points_to_coords
Expand All @@ -34,7 +34,7 @@
cities = cities.to_crs(south_am.crs) # convert city coordinates to same CRS!

# create the bounding shape as union of all South American countries' shapes
south_am_shape = cascaded_union(south_am.geometry)
south_am_shape = unary_union(south_am.geometry)
south_am_cities = cities[cities.geometry.within(south_am_shape)] # reduce to cities in South America

#%%
Expand Down
22 changes: 12 additions & 10 deletions geovoronoi/_voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from collections import defaultdict

import numpy as np
from scipy.spatial import Voronoi
from scipy.spatial.qhull import QhullError
from shapely.geometry import box, LineString, asPoint, MultiPoint, Polygon, MultiPolygon
from scipy.spatial import Voronoi, QhullError
from shapely.geometry import box, LineString, Point, MultiPoint, Polygon, MultiPolygon
from shapely.errors import TopologicalError
from shapely.ops import cascaded_union
from shapely.ops import unary_union

from ._geom import line_segment_intersection

Expand All @@ -32,7 +31,7 @@ def coords_to_points(coords):
:param coords: NumPy array of shape (N,2) with N coordinates in 2D space
:return: list of length N with Shapely Point objects
"""
return list(map(asPoint, coords))
return list(map(Point, coords))


def points_to_coords(pts):
Expand Down Expand Up @@ -276,7 +275,7 @@ def region_polygons_from_voronoi(vor, geom, return_point_assignments=False,
geom_bb = box(*geom.bounds).buffer(bounds_buf)

# center of all points
center = np.array(MultiPoint(vor.points).convex_hull.centroid)
center = np.array(MultiPoint(vor.points).convex_hull.centroid.coords)

# ridge vertice's coordinates
ridge_vert = np.asarray(vor.ridge_vertices)
Expand Down Expand Up @@ -361,7 +360,10 @@ def region_polygons_from_voronoi(vor, geom, return_point_assignments=False,
isects.append(isect)

if len(isects) == 0:
raise RuntimeError('ridge line must intersect with surrounding geometry from `geom`')
raise RuntimeError('ridge line must intersect with surrounding geometry from `geom`; '
'this error often arises when there are points outside of the '
'surrounding geometries; first check if all your points are inside '
'the surrounding geometries')
elif len(isects) == 1: # one intersection
far_pt = isects[0]
else: # multiple intersections - take closest intersection
Expand Down Expand Up @@ -440,14 +442,14 @@ def region_polygons_from_voronoi(vor, geom, return_point_assignments=False,
if len(inner_regions) == 1:
inner_regions_poly = region_polys[next(iter(inner_regions))]
else:
inner_regions_poly = cascaded_union([region_polys[i_reg] for i_reg in inner_regions])
inner_regions_poly = unary_union([region_polys[i_reg] for i_reg in inner_regions])

# generate polygon from all other regions' polygons other than the current region `i_reg`
other_regions_polys = [region_polys[i_other] for i_other in border_regions if i_reg != i_other]
if inner_regions_poly:
other_regions_polys.append(inner_regions_poly)

union_other_regions = cascaded_union(other_regions_polys)
union_other_regions = unary_union(other_regions_polys)

# generate difference between geom and other regions' polygons -- what's left is the current region's area
# plus any so far uncovered area
Expand Down Expand Up @@ -489,7 +491,7 @@ def region_polygons_from_voronoi(vor, geom, return_point_assignments=False,
# add new areas as union
if add:
old_reg_area = region_polys[i_reg].area
new_reg = cascaded_union([p] + add)
new_reg = unary_union([p] + add)
area_diff = new_reg.area - old_reg_area

region_polys[i_reg] = new_reg
Expand Down
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
GITHUB_URL = 'https://github.com/WZBSocialScienceCenter/geovoronoi'

__title__ = 'geovoronoi'
__version__ = '0.3.0'
__version__ = '0.3.1.dev'
__author__ = 'Markus Konrad'
__license__ = 'Apache License 2.0'

here = os.path.abspath(os.path.dirname(__file__))

DEPS_BASE = ['numpy>=1.19.0,<2', 'scipy>=1.5.0,<1.7', 'shapely>=1.7.0,<1.8']
DEPS_BASE = ['numpy>=1.19.0', 'scipy>=1.5.0', 'shapely>=1.7.0']

DEPS_EXTRA = {
'plotting': ['matplotlib>=3.3.0,<3.4', 'geopandas>=0.8.0,<0.9', 'descartes>=1.1.0,<1.2'],
'test': ['pytest>=6.2.0,<6.3', 'pytest-mpl>=0.12,<0.13', 'hypothesis>=6.0.0,<6.1', 'tox>=3.21.0,<3.22'],
'plotting': ['matplotlib>=3.3.0', 'geopandas>=0.8.0', 'descartes>=1.1.0'],
'test': ['pytest>=6.2.0', 'pytest-mpl>=0.12', 'hypothesis>=6.0.0', 'tox>=3.21.0'],
'develop': ['ipython>=7.19.0', 'twine>=3.3.0'],
}

Expand All @@ -40,7 +40,7 @@
url=GITHUB_URL,
project_urls={
'Source': GITHUB_URL,
'Tracker': 'https://github.com/WZBSocialScienceCenter/geovoronoi' + '/issues',
'Tracker': GITHUB_URL + '/issues',
},
author='Markus Konrad',
author_email='markus.konrad@wzb.eu',
Expand All @@ -60,6 +60,7 @@
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',

'Topic :: Scientific/Engineering :: Information Analysis',
'Topic :: Software Development :: Libraries :: Python Modules',
Expand Down
8 changes: 4 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import numpy as np
import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon, asPoint
from shapely.ops import cascaded_union
from shapely.geometry import Polygon, MultiPolygon, Point
from shapely.ops import unary_union
import pytest
from hypothesis import given, settings
import hypothesis.strategies as st
Expand Down Expand Up @@ -263,7 +263,7 @@ def test_voronoi_geopandas_with_plot():
cities = cities.to_crs(south_am.crs) # convert city coordinates to same CRS!

# create the bounding shape as union of all South American countries' shapes
south_am_shape = cascaded_union(south_am.geometry)
south_am_shape = unary_union(south_am.geometry)
south_am_cities = cities[cities.geometry.within(south_am_shape)] # reduce to cities in South America

# convert the pandas Series of Point objects to NumPy array of coordinates
Expand Down Expand Up @@ -407,7 +407,7 @@ def _check_region_polys(region_polys, region_pts, coords, expected_sum_area,
polybuf = poly
else:
polybuf = poly
assert all([polybuf.contains(asPoint(coords[i_pt])) for i_pt in pt_indices])
assert all([polybuf.contains(Point(coords[i_pt])) for i_pt in pt_indices])

sum_area += poly.area

Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
# and then run "tox" from this directory.

[tox]
envlist = py36, py37, py38, py39
envlist = py36, py37, py38, py39, py310

[testenv]
deps = .[test]
extras = plotting
commands =
pytest
pytest --mpl --mpl-baseline-path=tests/baseline

0 comments on commit 9de01f0

Please sign in to comment.