gemgis.vector.clip_by_polygon#

gemgis.vector.clip_by_polygon(gdf: geopandas.geodataframe.GeoDataFrame, polygon: shapely.geometry.polygon.Polygon, reset_index: bool = True, drop_index: bool = True, drop_id: bool = True, drop_points: bool = True, drop_level0: bool = True, drop_level1: bool = True) geopandas.geodataframe.GeoDataFrame#

Clipping 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

  • polygon (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]])

  • reset_index (bool) – Variable to reset the index of the resulting GeoDataFrame. 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_level1 (bool) – Variable to drop the level_1 column. 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_id (bool) – Variable to drop the id column. 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

Returns

gdf – GeoDataFrame containing vector data clipped by a bounding box

Return type

gpd.geodataframe.GeoDataFrame

New in version 1.0.x.

Example

>>> # Loading Libraries and File
>>> import gemgis as gg
>>> 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)
>>> # Returning the length of the original gdf
>>> len(gdf)
50
>>> # Creating Shapely Polygon
>>> from shapely.geometry import Polygon
>>> polygon = Polygon([(0,0),(972, 0), (972,1069), (0, 1069)])
>>> polygon.wkt
'POLYGON ((0 0, 972 0, 972 1069, 0 1069, 0 0))'
>>> # 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
>>> # Returning the length of the clipped gdf
>>> len(gdf_clipped)
25

See also

clip_by_bbox

Clipping vector data with a bbox