geopandas.GeoSeries.clip_by_rect#

GeoSeries.clip_by_rect(xmin, ymin, xmax, ymax)[source]#

Returns a GeoSeries of the portions of geometry within the given rectangle.

Note that the results are not exactly equal to intersection(). E.g. in edge cases, clip_by_rect() will not return a point just touching the rectangle. Check the examples section below for some of these exceptions.

The geometry is clipped in a fast but possibly dirty way. The output is not guaranteed to be valid. No exceptions will be raised for topological errors.

Note: empty geometries or geometries that do not overlap with the specified bounds will result in GEOMETRYCOLLECTION EMPTY.

Parameters:
xmin: float

Minimum x value of the rectangle

ymin: float

Minimum y value of the rectangle

xmax: float

Maximum x value of the rectangle

ymax: float

Maximum y value of the rectangle

Returns:
GeoSeries

Examples

>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
...     [
...         Polygon([(0, 0), (2, 2), (0, 2)]),
...         Polygon([(0, 0), (2, 2), (0, 2)]),
...         LineString([(0, 0), (2, 2)]),
...         LineString([(2, 0), (0, 2)]),
...         Point(0, 1),
...     ],
... )
>>> bounds = (0, 0, 1, 1)
>>> s
0    POLYGON ((0 0, 2 2, 0 2, 0 0))
1    POLYGON ((0 0, 2 2, 0 2, 0 0))
2             LINESTRING (0 0, 2 2)
3             LINESTRING (2 0, 0 2)
4                       POINT (0 1)
dtype: geometry
>>> s.clip_by_rect(*bounds)
0    POLYGON ((0 0, 0 1, 1 1, 0 0))
1    POLYGON ((0 0, 0 1, 1 1, 0 0))
2             LINESTRING (0 0, 1 1)
3          GEOMETRYCOLLECTION EMPTY
4          GEOMETRYCOLLECTION EMPTY
dtype: geometry