vibespatial.api.tools.clip

Module to clip vector data using GeoPandas.

Attributes

Classes

ClipNativeResult

Deferred clip export that preserves native geometry results until the boundary.

Functions

evaluate_geopandas_clip_native(...)

Build a native clip result and defer GeoPandas export to the boundary.

clip(gdf, mask[, keep_geom_type, sort])

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

Module Contents

vibespatial.api.tools.clip.logger
class vibespatial.api.tools.clip.ClipNativeResult

Deferred clip export that preserves native geometry results until the boundary.

source: vibespatial.api.GeoDataFrame | vibespatial.api.GeoSeries
parts: tuple[vibespatial.api._native_results.LeftConstructiveResult, Ellipsis]
ordered_index: pandas.Index
ordered_row_positions: numpy.ndarray
clipping_by_rectangle: bool
has_non_point_candidates: bool
keep_geom_type: bool
to_spatial()
to_geodataframe() vibespatial.api.GeoDataFrame
to_geoseries() vibespatial.api.GeoSeries
vibespatial.api.tools.clip.evaluate_geopandas_clip_native(gdf, mask, *, keep_geom_type: bool = False, sort: bool = False) vibespatial.api._native_results.NativeTabularResult

Build a native clip result and defer GeoPandas export to the boundary.

vibespatial.api.tools.clip.clip(gdf, mask, keep_geom_type=False, sort=False)

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

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

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

If the mask is list-like with four elements (minx, miny, maxx, maxy), a faster rectangle clipping algorithm will be used. Note that this can lead to slightly different results in edge cases, e.g. if a line would be reduced to a point, this point might not be returned. 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.

Parameters

gdfGeoDataFrame or GeoSeries

Vector layer (point, line, polygon) to be clipped to mask.

maskGeoDataFrame, GeoSeries, (Multi)Polygon, list-like

Polygon vector layer used to clip gdf. The mask’s geometry is dissolved into one geometric feature and intersected with gdf. If the mask is list-like with four elements (minx, miny, maxx, maxy), clip will use a faster rectangle clipping (clip_by_rect()), possibly leading to slightly different results.

keep_geom_typeboolean, default False

If True, return only geometries of original type in case of intersection resulting in multiple geometry types or GeometryCollections. If False, return all resulting geometries (potentially mixed-types).

sortboolean, default False

If True, the results will be sorted in ascending order using the geometries’ indexes as the primary key.

Returns

GeoDataFrame or GeoSeries

Vector data (points, lines, polygons) from gdf clipped to polygon boundary from mask.

See Also

GeoDataFrame.clip : equivalent GeoDataFrame method GeoSeries.clip : equivalent GeoSeries method

Examples

Clip points (grocery stores) with polygons (the Near West Side community):

>>> import geodatasets
>>> chicago = geopandas.read_file(
...     geodatasets.get_path("geoda.chicago_health")
... )
>>> near_west_side = chicago[chicago["community"] == "NEAR WEST SIDE"]
>>> groceries = geopandas.read_file(
...     geodatasets.get_path("geoda.groceries")
... ).to_crs(chicago.crs)
>>> groceries.shape
(148, 8)
>>> nws_groceries = geopandas.clip(groceries, near_west_side)
>>> nws_groceries.shape
(7, 8)