From 2b98c2ce48bb223ef9f87e45009a9984d2670aa4 Mon Sep 17 00:00:00 2001 From: davide-f <67809479+davide-f@users.noreply.github.com> Date: Fri, 17 Sep 2021 16:18:02 +0200 Subject: [PATCH 1/2] Fixing empty areas with low number of points --- vresutils/graph.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/vresutils/graph.py b/vresutils/graph.py index e897233..35df323 100644 --- a/vresutils/graph.py +++ b/vresutils/graph.py @@ -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` @@ -496,13 +497,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)): From a12bb4ea8956235ae43e9b73a732031456096b67 Mon Sep 17 00:00:00 2001 From: davide-f <67809479+davide-f@users.noreply.github.com> Date: Fri, 17 Sep 2021 21:59:29 +0200 Subject: [PATCH 2/2] Update description Updated description of the function voronoi_partition_pts --- vresutils/graph.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vresutils/graph.py b/vresutils/graph.py index 35df323..ea0741a 100644 --- a/vresutils/graph.py +++ b/vresutils/graph.py @@ -481,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 -------