forked from geopandas/geopandas
-
Notifications
You must be signed in to change notification settings - Fork 1
Limit sliver uses #23
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -180,25 +180,61 @@ def clip(gdf, clip_obj, drop_slivers=False): | |||||
order = pd.Series(range(len(gdf)), index=gdf.index) | ||||||
concat = pd.concat([point_gdf, line_gdf, poly_gdf]) | ||||||
|
||||||
if (concat.geom_type == "GeometryCollection").any() and drop_slivers: | ||||||
concat = concat.explode() | ||||||
if not polys.empty: | ||||||
concat = concat.loc[concat.geom_type.isin(["Polygon", "MultiPolygon"])] | ||||||
elif not lines.empty: | ||||||
concat = concat.loc[ | ||||||
concat.geom_type.isin(["LineString", "MultiLineString", "LinearRing"]) | ||||||
] | ||||||
elif not points.empty: | ||||||
concat = concat.loc[concat.geom_type.isin(["Point", "MultiPoint"])] | ||||||
else: | ||||||
raise TypeError("`drop_sliver` does not support {}.".format(type)) | ||||||
elif (concat.geom_type == "GeometryCollection").any() and not drop_slivers: | ||||||
polys = ["Polygon", "MultiPolygon"] | ||||||
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. You can add a few tactful comments here. |
||||||
lines = ["LineString", "MultiLineString", "LinearRing"] | ||||||
points = ["Point", "MultiPoint"] | ||||||
|
||||||
# Check that the gdf submitted is not a multi type GeoDataFrame | ||||||
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.
Suggested change
|
||||||
orig_types_total = sum( | ||||||
[ | ||||||
gdf.geom_type.isin(polys).any(), | ||||||
gdf.geom_type.isin(lines).any(), | ||||||
gdf.geom_type.isin(points).any(), | ||||||
] | ||||||
) | ||||||
|
||||||
# Check how many geometry types are in the clipped GeoDataFrame | ||||||
clip_types_total = sum( | ||||||
[ | ||||||
concat.geom_type.isin(polys).any(), | ||||||
concat.geom_type.isin(lines).any(), | ||||||
concat.geom_type.isin(points).any(), | ||||||
] | ||||||
) | ||||||
|
||||||
# Check if the clipped geometry is a geometry collection | ||||||
geometry_collection = (concat.geom_type == "GeometryCollection").any() | ||||||
|
||||||
# Check there aren't any additional geometries in the clipped GeoDataFrame | ||||||
more_types = orig_types_total < clip_types_total | ||||||
|
||||||
if orig_types_total > 1 and drop_slivers: | ||||||
warnings.warn( | ||||||
"Drop slivers was called on a mixed type GeoDataFrame. " | ||||||
"Slivers cannot be dropped on mixed type GeoDataFrames. " | ||||||
"The data will be return with slivers present." | ||||||
) | ||||||
elif drop_slivers and not geometry_collection and not more_types: | ||||||
warnings.warn("Drop slivers was called when no slivers existed.") | ||||||
elif drop_slivers and (geometry_collection or more_types): | ||||||
orig_type = gdf.geom_type.iloc[0] | ||||||
if geometry_collection: | ||||||
concat = concat.explode() | ||||||
if orig_type in polys: | ||||||
concat = concat.loc[concat.geom_type.isin(polys)] | ||||||
elif orig_type in lines: | ||||||
concat = concat.loc[concat.geom_type.isin(lines)] | ||||||
elif geometry_collection and not drop_slivers: | ||||||
warnings.warn( | ||||||
"A geometry collection has been returned. Use .explode() to " | ||||||
"remove the collection object or drop_slivers=True to remove " | ||||||
"sliver geometries." | ||||||
) | ||||||
elif drop_slivers: | ||||||
warnings.warn("Drop slivers was called when no slivers existed.") | ||||||
elif more_types and not drop_slivers: | ||||||
warnings.warn( | ||||||
"More geometry types were returned than were in the original " | ||||||
"GeoDataFrame. This is likely due to a sliver being created. " | ||||||
"To remove the slivers set drop_slivers=True. " | ||||||
) | ||||||
concat["_order"] = order | ||||||
return concat.sort_values(by="_order").drop(columns="_order") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 please :)