-
Notifications
You must be signed in to change notification settings - Fork 357
feat(GridIntersect): add optional kwarg to .intersect() to keep all c… #1214
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,6 +223,8 @@ def intersect(self, shp, **kwargs): | |
| keepzerolengths : bool | ||
| boolean method to keep zero length intersections for | ||
| linestring intersection | ||
| keep_stacked_cells : bool | ||
| For shapely only, keeps all intersected cells even if cell vertices are already in the results. | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -233,6 +235,7 @@ def intersect(self, shp, **kwargs): | |
| shp = gu.shapely | ||
| sort_by_cellid = kwargs.pop("sort_by_cellid", True) | ||
| keepzerolengths = kwargs.pop("keepzerolengths", False) | ||
| keep_stacked_cells = kwargs.pop("keep_stacked_cells", False) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to keep this syntax with **kwargs, or should we add the keyword arguments to the intersect method explicitly? (Even if they're sometimes not used, depending on the GridIntersect settings).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would vote for an actual argument rather than a |
||
|
|
||
| if gu.shapetype in ("Point", "MultiPoint"): | ||
| if ( | ||
|
|
@@ -241,7 +244,9 @@ def intersect(self, shp, **kwargs): | |
| ): | ||
| rec = self._intersect_point_structured(shp) | ||
| else: | ||
| rec = self._intersect_point_shapely(shp, sort_by_cellid) | ||
| rec = self._intersect_point_shapely( | ||
| shp, sort_by_cellid, keep_stacked_cells | ||
| ) | ||
| elif gu.shapetype in ("LineString", "MultiLineString"): | ||
| if ( | ||
| self.method == "structured" | ||
|
|
@@ -252,7 +257,7 @@ def intersect(self, shp, **kwargs): | |
| ) | ||
| else: | ||
| rec = self._intersect_linestring_shapely( | ||
| shp, keepzerolengths, sort_by_cellid | ||
| shp, keepzerolengths, sort_by_cellid, keep_stacked_cells | ||
| ) | ||
| elif gu.shapetype in ("Polygon", "MultiPolygon"): | ||
| if ( | ||
|
|
@@ -456,7 +461,9 @@ def sort_key(o): | |
| shapelist.sort(key=sort_key) | ||
| return shapelist | ||
|
|
||
| def _intersect_point_shapely(self, shp, sort_by_cellid=True): | ||
| def _intersect_point_shapely( | ||
| self, shp, sort_by_cellid=True, keep_stacked_cells=False | ||
| ): | ||
| """intersect grid with Point or MultiPoint. | ||
|
|
||
| Parameters | ||
|
|
@@ -468,6 +475,8 @@ def _intersect_point_shapely(self, shp, sort_by_cellid=True): | |
| sort_by_cellid : bool, optional | ||
| flag whether to sort cells by id, used to ensure node | ||
| with lowest id is returned, by default True | ||
| keep_stacked_cells : bool, optional | ||
| For shapely only, keeps all intersected cells even if cell vertices are already in the results. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
also ensure <80 characters per line in docsstring
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like |
||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -505,7 +514,7 @@ def _intersect_point_shapely(self, shp, sort_by_cellid=True): | |
| for c in collection: | ||
| verts = c.__geo_interface__["coordinates"] | ||
| # avoid returning multiple cells for points on boundaries | ||
| if verts in parsed_points: | ||
| if not keep_stacked_cells and verts in parsed_points: | ||
| continue | ||
| parsed_points.append(verts) | ||
| cell_shps.append(c) # collect only new points | ||
|
|
@@ -534,7 +543,11 @@ def _intersect_point_shapely(self, shp, sort_by_cellid=True): | |
| return rec | ||
|
|
||
| def _intersect_linestring_shapely( | ||
| self, shp, keepzerolengths=False, sort_by_cellid=True | ||
| self, | ||
| shp, | ||
| keepzerolengths=False, | ||
| sort_by_cellid=True, | ||
| keep_stacked_cells=False, | ||
| ): | ||
| """intersect with LineString or MultiLineString. | ||
|
|
||
|
|
@@ -547,6 +560,8 @@ def _intersect_linestring_shapely( | |
| sort_by_cellid : bool, optional | ||
| flag whether to sort cells by id, used to ensure node | ||
| with lowest id is returned, by default True | ||
| keep_stacked_cells : bool, optional | ||
| For shapely only, keeps all intersected cells even if cell vertices are already in the results. | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -580,7 +595,7 @@ def _intersect_linestring_shapely( | |
| for c in collection: | ||
| verts = c.__geo_interface__["coordinates"] | ||
| # test if linestring was already processed (if on boundary) | ||
| if verts in vertices: | ||
| if not keep_stacked_cells and verts in vertices: | ||
| continue | ||
| # if keep zero don't check length | ||
| if not keepzerolengths: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docstring <80 chars