Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing empty areas with low number of points #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions vresutils/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ def get_voronoi_regions(G, outline=None):
G = voronoi_partition(G, Polygon(outline))
return list(get_node_attributes(G, 'region').values())

def voronoi_partition_pts(points, outline, no_multipolygons=False):
def voronoi_partition_pts(points, outline, no_multipolygons=False,
add_bounds_shape=True, multiplier=5):
"""
Compute the polygons of a voronoi partition of `points` within the
polygon `outline`
Expand All @@ -480,6 +481,12 @@ def voronoi_partition_pts(points, outline, no_multipolygons=False):
outline : Polygon
no_multipolygons : bool (default: False)
If true, replace each MultiPolygon by its largest component
add_bounds_shape : bool (default: True)
If true, the maximum size of the Voronoi cell is extended
to possibly match the outline shape
multiplier : float (default: 5)
default of the multiplier to choose the size of the Voronoi cell
with respect to the points and the polygon considered

Returns
-------
Expand All @@ -496,13 +503,21 @@ def voronoi_partition_pts(points, outline, no_multipolygons=False):
xspan = xmax - xmin
yspan = ymax - ymin

if add_bounds_shape:
# check bounds of the shape
minx_o, miny_o, maxx_o, maxy_o = outline.boundary.bounds
xmin = min(xmin, minx_o)
ymin = min(ymin, miny_o)
xmax = min(xmax, maxx_o)
ymax = min(ymax, maxy_o)

# to avoid any network positions outside all Voronoi cells, append
# the corners of a rectangle framing these points
vor = Voronoi(np.vstack((points,
[[xmin-3.*xspan, ymin-3.*yspan],
[xmin-3.*xspan, ymax+3.*yspan],
[xmax+3.*xspan, ymin-3.*yspan],
[xmax+3.*xspan, ymax+3.*yspan]])))
[[xmin-multiplier*xspan, ymin-multiplier*yspan],
[xmin-multiplier*xspan, ymax+multiplier*yspan],
[xmax+multiplier*xspan, ymin-multiplier*yspan],
[xmax+multiplier*xspan, ymax+multiplier*yspan]])))

polygons = []
for i in range(len(points)):
Expand Down