Skip to content

Commit

Permalink
Format clip_by_polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderJuestel committed Jul 30, 2024
1 parent 70296a0 commit 4235824
Showing 1 changed file with 83 additions and 37 deletions.
120 changes: 83 additions & 37 deletions gemgis/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4333,50 +4333,80 @@ def clip_by_polygon(
drop_level0: bool = True,
drop_level1: bool = True,
) -> gpd.geodataframe.GeoDataFrame:
"""Clipping vector data contained in a GeoDataFrame to a provided bounding box/extent
"""Clip vector data contained in a GeoDataFrame to a provided bounding box/extent.
Parameters
__________
gdf : gpd.geodataframe.GeoDataFrame
GeoDataFrame containing vector data that will be clipped to a provided bounding box/extent
GeoDataFrame containing vector data that will be clipped to a provided bounding box/extent.
polygon : polygon: shapely.geometry.polygon
+----+-----------------------------+
| ID | geometry |
+----+-----------------------------+
| 0 | POINT (281.526 902.087) |
+----+-----------------------------+
| 1 | POINT (925.867 618.577) |
+----+-----------------------------+
| 2 | POINT (718.131 342.799) |
+----+-----------------------------+
| 3 | POINT (331.011 255.684) |
+----+-----------------------------+
| 4 | POINT (300.083 600.535) |
+----+-----------------------------+
polygon : shapely.geometry.Polygon
Shapely Polygon defining the extent of the data,
e.g. ``polygon = Polygon([[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]])``
e.g. ``polygon = Polygon([[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]])``.
reset_index : bool
Variable to reset the index of the resulting GeoDataFrame.
Options include: ``True`` or ``False``, default set to ``True``
reset_index : bool, default: ``True``
Variable to reset the index of the resulting GeoDataFrame, e.g. ``reset_index=True``.
Options include: ``True`` or ``False``, default set to ``True``.
drop_level0 : bool
Variable to drop the level_0 column.
Options include: ``True`` or ``False``, default set to ``True``
drop_level0 : bool, default: ``True``
Variable to drop the level_0 column, e.g. ``drop_level0=True``.
Options include: ``True`` or ``False``, default set to ``True``.
drop_level1 : bool
Variable to drop the level_1 column.
Options include: ``True`` or ``False``, default set to ``True``
drop_level1 : bool, default: ``True``
Variable to drop the level_1 column, e.g. ``drop_level1=True``.
Options include: ``True`` or ``False``, default set to ``True``.
drop_index : bool
Variable to drop the index column.
Options include: ``True`` or ``False``, default set to ``True``
drop_index : bool, default: ``True``
Variable to drop the index column, e.g. ``drop_index=True``.
Options include: ``True`` or ``False``, default set to ``True``.
drop_id : bool
Variable to drop the id column.
Options include: ``True`` or ``False``, default set to ``True``
drop_id : bool, default: ``True``
Variable to drop the id column, e.g. ``drop_id=True``.
Options include: ``True`` or ``False``, default set to ``True``.
drop_points : bool
Variable to drop the points column.
Options include: ``True`` or ``False``, default set to ``True``
drop_points : bool, default: ``True``
Variable to drop the points column, e.g. ``drop_points=True``.
Options include: ``True`` or ``False``, default set to ``True``.
Returns
_______
gdf : gpd.geodataframe.GeoDataFrame
GeoDataFrame containing vector data clipped by a bounding box
GeoDataFrame containing vector data clipped by a bounding box.
+----+-----------------------------+---------+---------+
| | geometry | X | Y |
+----+-----------------------------+---------+---------+
| 0 | POINT (281.526 902.087) | 281.53 | 902.09 |
+----+-----------------------------+---------+---------+
| 1 | POINT (925.867 618.577) | 925.87 | 618.58 |
+----+-----------------------------+---------+---------+
| 2 | POINT (718.131 342.799) | 718.13 | 342.80 |
+----+-----------------------------+---------+---------+
| 3 | POINT (331.011 255.684) | 331.01 | 255.68 |
+----+-----------------------------+---------+---------+
| 4 | POINT (300.083 600.535) | 300.08 | 600.54 |
+----+-----------------------------+---------+---------+
.. versionadded:: 1.0.x
.. versionchanged:: 1.2
Example
_______
Expand All @@ -4385,12 +4415,21 @@ def clip_by_polygon(
>>> import geopandas as gpd
>>> gdf = gpd.read_file(filename='file.shp')
>>> gdf
id geometry
0 None POINT (281.526 902.087)
1 None POINT (925.867 618.577)
2 None POINT (718.131 342.799)
3 None POINT (331.011 255.684)
4 None POINT (300.083 600.535)
+----+-----------------------------+
| ID | geometry |
+----+-----------------------------+
| 0 | POINT (281.526 902.087) |
+----+-----------------------------+
| 1 | POINT (925.867 618.577) |
+----+-----------------------------+
| 2 | POINT (718.131 342.799) |
+----+-----------------------------+
| 3 | POINT (331.011 255.684) |
+----+-----------------------------+
| 4 | POINT (300.083 600.535) |
+----+-----------------------------+
>>> # Returning the length of the original gdf
>>> len(gdf)
Expand All @@ -4405,12 +4444,20 @@ def clip_by_polygon(
>>> # Clipping data by the polygon
>>> gdf_clipped = gg.vector.clip_by_polygon(gdf=gdf, polygon=polygon)
>>> gdf_clipped
geometry X Y
0 POINT (281.526 902.087) 281.53 902.09
1 POINT (925.867 618.577) 925.87 618.58
2 POINT (718.131 342.799) 718.13 342.80
3 POINT (331.011 255.684) 331.01 255.68
4 POINT (300.083 600.535) 300.08 600.54
+----+-----------------------------+---------+---------+
| | geometry | X | Y |
+----+-----------------------------+---------+---------+
| 0 | POINT (281.526 902.087) | 281.53 | 902.09 |
+----+-----------------------------+---------+---------+
| 1 | POINT (925.867 618.577) | 925.87 | 618.58 |
+----+-----------------------------+---------+---------+
| 2 | POINT (718.131 342.799) | 718.13 | 342.80 |
+----+-----------------------------+---------+---------+
| 3 | POINT (331.011 255.684) | 331.01 | 255.68 |
+----+-----------------------------+---------+---------+
| 4 | POINT (300.083 600.535) | 300.08 | 600.54 |
+----+-----------------------------+---------+---------+
>>> # Returning the length of the clipped gdf
>>> len(gdf_clipped)
Expand All @@ -4419,10 +4466,9 @@ def clip_by_polygon(
See Also
________
clip_by_bbox : Clipping vector data with a bbox
clip_by_bbox : Clip vector data with a bbox
"""

# Checking if the gdf is of type GeoDataFrame
if not isinstance(gdf, gpd.geodataframe.GeoDataFrame):
raise TypeError("gdf must be of type GeoDataFrame")
Expand Down

0 comments on commit 4235824

Please sign in to comment.