FIX: support Polygon/MultiPolygon in Voronoi graph builder (#688)#868
FIX: support Polygon/MultiPolygon in Voronoi graph builder (#688)#868AliaaNasser7 wants to merge 11 commits into
Conversation
|
Thank you for this, but the changes are breaking the tests. |
Thanks for the feedback! I’m working through the failing tests. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #868 +/- ##
=====================================
Coverage 85.5% 85.5%
=====================================
Files 151 151
Lines 16092 16141 +49
=====================================
+ Hits 13758 13807 +49
Misses 2334 2334
🚀 New features to boost your workflow:
|
|
@martinfleis I reworked the PR completely ,instead of downcasting to centroids, _voronoi now passes geometries directly to voronoi_frames (which handles all geometry types since #678) and forwards segment/shrink kwargs. |
martinfleis
left a comment
There was a problem hiding this comment.
It might be easier to keep the current _voronoi function intact with its coplanarity solutions and write a separate one for non-point geometry types. And just channel to the proper one when needed.
| if coplanar == "jitter": | ||
| coords = shapely.get_coordinates(geoms) | ||
| coords = _jitter_geoms(coords, seed=seed) | ||
| geoms = geopandas.GeoSeries( | ||
| shapely.points(coords), | ||
| index=geoms.index if hasattr(geoms, "index") else None, |
There was a problem hiding this comment.
This is going to get very wrong when non-point geometry is given.
Please ensure that you write test suite alongside your contributions. Many of the issues you'd be able to catch yourself.
Thank you for the guidance! ok, I will do the changes and also add proper tests this time. |
@martinfleis I reworked based on your suggestion, kept |
| heads, tails, weights = _voronoi_polygon(gdf, ids=ids) | ||
| assert len(np.unique(heads)) == 4 |
There was a problem hiding this comment.
The tests should look like those above. Check the exact arrays, all of them.
|
|
||
| @pytest.mark.network | ||
| def test_coplanar_jitter_voronoi(stores, stores_unique): | ||
| def test_coplanar_jitter_voronoi(stores, stores_unique): # edit |
There was a problem hiding this comment.
| def test_coplanar_jitter_voronoi(stores, stores_unique): # edit | |
| def test_coplanar_jitter_voronoi(stores, stores_unique): |
| #### utilities | ||
|
|
||
|
|
||
| def _voronoi_polygon( |
There was a problem hiding this comment.
this should not be under "utilities" but above.
| heads = heads_ix | ||
| tails = tails_ix | ||
| weights = numpy.ones(len(heads_ix), dtype=numpy.int8) | ||
| return heads, tails, weights |
There was a problem hiding this comment.
| heads = heads_ix | |
| tails = tails_ix | |
| weights = numpy.ones(len(heads_ix), dtype=numpy.int8) | |
| return heads, tails, weights | |
| weights = numpy.ones(len(heads_ix), dtype=numpy.int8) | |
| return heads_ix, tails_ix, weights |
| ``method="voronoi"``. Supports ``segment`` (float) and ``shrink`` | ||
| (float). Ignored for other methods. |
|
|
||
| Parameters | ||
| ---------- | ||
| data : numpy.ndarray, geopandas.GeoSeries, geopandas.GeoDataFrame |
There was a problem hiding this comment.
This needs to be updated to also mention other geometry types.
| decay=decay, | ||
| taper=taper, | ||
| ) | ||
| if hasattr(data, "geom_type") and not set(data.geom_type) <= {"Point"}: |
There was a problem hiding this comment.
I think we should use shapely.get_type_id in here to check the geometry types for all inputs, not just geopandas ones.
Description
I've updated the
_voronoibuilder to support polygonal geometries by using their centroids as generators. This proposes a practical resolution for #688 and bypasses the limitations of the current point-based validation.Changes & Technical Implementation
@_validate_coplanardecorator was causing indexing issues and attribute errors with Polygons. I moved the coplanar check inside the function and used centroid extraction to ensure the math stays consistent with the Voronoi logic..centroidto handle GeoDataFrames directly. For other inputs, I'm usingget_points_arraywith a fallback to reshape the array, ensuring we always pass an (n, 2) array tovoronoi_frames.heads_ixandtails_ixback to the originalidsbefore returning the graph.**kwargsdown tovoronoi_framesto support parameters likeshrinkorbuffer.Verification
I tested this with a GeoDataFrame of adjacent polygons. The builder now correctly identifies neighbors and maintains the original index without crashing or throwing indexing errors.
Related to #688