earthpy.clip

A module to clip vector data using GeoPandas.

earthpy.clip.clip_shp(shp, clip_obj)[source]

Clip points, lines, or polygon geometries to the clip_obj extent.

Both layers must be in the same Coordinate Reference System (CRS) and will be clipped to the full extent of the clip object.

If there are multiple polygons in clip_obj, data from shp will be clipped to the total boundary of all polygons in clip_obj.

Parameters
  • shp (GeoDataFrame) – Vector layer (point, line, polygon) to be clipped to clip_obj.

  • clip_obj (GeoDataFrame) – Polygon vector layer used to clip shp. The clip_obj’s geometry is dissolved into one geometric feature and intersected with shp.

Returns

Vector data (points, lines, polygons) from shp clipped to polygon boundary from clip_obj.

Return type

GeoDataFrame

Examples

Clipping points (glacier locations in the state of Colorado) with a polygon (the boundary of Rocky Mountain National Park):

>>> import geopandas as gpd
>>> import earthpy.clip as cl
>>> from earthpy.io import path_to_example
>>> rmnp = gpd.read_file(path_to_example('rmnp.shp'))
>>> glaciers = gpd.read_file(path_to_example('colorado-glaciers.geojson'))
>>> glaciers.shape
(134, 2)
>>> rmnp_glaciers = cl.clip_shp(glaciers, rmnp)
>>> rmnp_glaciers.shape
(36, 2)

Clipping a line (the Continental Divide Trail) with a polygon (the boundary of Rocky Mountain National Park):

>>> cdt = gpd.read_file(path_to_example('continental-div-trail.geojson'))
>>> rmnp_cdt_section = cl.clip_shp(cdt, rmnp)
>>> cdt['geometry'].length > rmnp_cdt_section['geometry'].length
0    True
dtype: bool

Clipping a polygon (Colorado counties) with another polygon (the boundary of Rocky Mountain National Park):

>>> counties = gpd.read_file(path_to_example('colorado-counties.geojson'))
>>> counties.shape
(64, 13)
>>> rmnp_counties = cl.clip_shp(counties, rmnp)
>>> rmnp_counties.shape
(4, 13)