dtoolkit.geoaccessor.geoseries.filter_geometry#
- dtoolkit.geoaccessor.geoseries.filter_geometry(s: gpd.GeoSeries, /, other: BaseGeometry | gpd.GeoSeries | gpd.GeoDataFrame, predicate: BINARY_PREDICATE, complement: bool = False, **kwargs) gpd.GeoSeries [source]#
Filter GeoSeries via the spatial relationship between GeoSeries and
geometry
.A sugar syntax wraps:
s[s.{predicate}(other, **kwargs)]
- Parameters:
- otherGeometry, GeoSeries, or GeoDataFrame
- predicate{“intersects”, “disjoint”, “crosses”, “overlaps”, “touches”, “covered_by”, “contains”, “within”, “covers”}
Binary predicate.
- complementbool, default False
If True, do operation reversely.
- **kwargs
See the documentation for
GeoSeries.{predicate}
for complete details on the keyword arguments.
- Returns:
- GeoSeries
See also
Examples
>>> import dtoolkit.geoaccessor >>> import geopandas as gpd >>> from shapely import Polygon, LineString, Point, box >>> df = gpd.GeoDataFrame( ... geometry=[ ... Polygon([(0, 0), (1, 1), (0, 1)]), ... LineString([(0, 0), (0, 2)]), ... LineString([(0, 0), (0, 1)]), ... Point(0, 1), ... Point(-1, -1), ... ], ... ) >>> df geometry 0 POLYGON ((0.00000 0.00000, 1.00000 1.00000, 0.... 1 LINESTRING (0.00000 0.00000, 0.00000 2.00000) 2 LINESTRING (0.00000 0.00000, 0.00000 1.00000) 3 POINT (0.00000 1.00000) 4 POINT (-1.00000 -1.00000)
Filter the geometries out of the bounding
box(0, 0, 2, 2)
.>>> df.filter_geometry(box(0, 0, 2, 2), "covered_by", complement=True) geometry 4 POINT (-1.00000 -1.00000)
This method is actually faster than the following one:
def is_in_geometry(s: gpd.GeoSeries, other: BaseGeometry) -> pd.Series: s_bounds, g_bounds = s.bounds, other.bounds return ( (s_bounds.minx >= g_bounds[0]) & (s_bounds.miny >= g_bounds[1]) & (s_bounds.maxx <= g_bounds[2]) & (s_bounds.maxy <= g_bounds[3]) )