Skip to content

Commit

Permalink
make separate geom handling optional
Browse files Browse the repository at this point in the history
  • Loading branch information
internaut committed Jan 28, 2021
1 parent 56c5379 commit cbb2f83
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
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: 3 additions & 1 deletion examples/using_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
coords = points_to_coords(south_am_cities.geometry)

# calculate the regions
poly_shapes, poly_to_pt_assignments = voronoi_regions_from_coords(coords, south_am_shape)
# we set "per_geom=False" so that the whole continent is treated as one area and Voronoi regions
# span over to Tierra del Fuego
poly_shapes, poly_to_pt_assignments = voronoi_regions_from_coords(coords, south_am_shape, per_geom=False)


#%%
Expand Down
13 changes: 7 additions & 6 deletions geovoronoi/_voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def points_to_coords(pts):
return np.array([p.coords[0] for p in pts])


def voronoi_regions_from_coords(coords, geo_shape):
def voronoi_regions_from_coords(coords, geo_shape, per_geom=True):
"""
Calculate Voronoi regions from NumPy array of 2D coordinates `coord` that lie within a shape `geo_shape`. Setting
`shapes_from_diff_with_min_area` fixes rare errors where the Voronoi shapes do not fully cover `geo_shape`. Set this
Expand All @@ -57,20 +57,21 @@ def voronoi_regions_from_coords(coords, geo_shape):
doesn't intersect with `geo_shape`.
"""

logger.info('running Voronoi tesselation for %d points' % len(coords))
logger.info('running Voronoi tesselation for %d points / treating geoms separately: %s' % (len(coords), per_geom))

if isinstance(coords, np.ndarray):
pts = coords_to_points(coords)
else:
pts = coords
coords = points_to_coords(pts)

if isinstance(geo_shape, Polygon):
if not isinstance(geo_shape, (Polygon, MultiPolygon)):
raise ValueError('`geo_shape` must be a Polygon or MultiPolygon')

if not per_geom or isinstance(geo_shape, Polygon):
geoms = [geo_shape]
elif isinstance(geo_shape, MultiPolygon):
else: # Multipolygon
geoms = geo_shape.geoms
else:
raise ValueError('`geo_shape` must be a Polygon or MultiPolygon')

pts_indices = set(range(len(pts)))
region_polys = {}
Expand Down

0 comments on commit cbb2f83

Please sign in to comment.