Indexing GeoPandas#
Index geometries in a GeoPandas GeoDataFrame against the BNG index system.
Note
This module requires the GeoPandas package to be installed.
To install the required package, use:
pip install osbng[geopandas]
- osbng.indexing_gpd.gdf_to_bng_intersection_explode(gdf: GeoDataFrame, resolution: int | str, *, reset_index: bool = True) GeoDataFrame[source]#
Applies
geom_to_bng_intersectionto aGeoDataFrameat a given resolution.Decomposes each geometry in the input
GeoDataFramebounded by their presence in grid squares at the specified resolution. The resultingBNGIndexedGeometryobjects are exploded into individual rows, with each row containing a new column for eachBNGIndexedGeometryobject property:bng_ref,is_core, andgeom.Notes
Decomposition is achieved by applying the
geom_to_bng_intersectionfunction to each geometry in the inputGeoPandasGeoDataFrame, returning a flattenedGeoDataFrameby exploding the resultingBNGIndexedGeometrylists.The input
GeoDataFramegeometry column is replaced with the geom property of theBNGIndexedGeometryobjects. The input geometry column can be retrieved if required by joining the resultingGeoDataFramewith the originalGeoDataFrameon the index (if not reset), or using a feature identifier. Dropping the original geometry column reduces memory usage and simplifies the resultingGeoDataFrame.All non-geometry columns from the original
GeoDataFrameare retained in the resultingGeoDataFrame.Exploding the resulting
GeoDataFrameallows for easier analysis and manipulation of theBNGIndexedGeometryobject properties. This is otherwise a more complex operation.Warning
The active geometry column of the input
GeoDataFrameis passed togeom_to_bng_intersection, which is expected to be set and in the OSGB36 / British National Grid coordinate reference system (CRS) (EPSG:27700).- Parameters:
- Keyword Arguments:
reset_index (bool) – Whether to reset the index of the resulting
GeoDataFrame, defaults to True.- Returns:
A new
GeoDataFramewith one row perBNGIndexedGeometryobject, containing three columns bng_ref, is_core, and geometry corresponding to theBNGIndexedGeometryobject properties.- Return type:
gpd.GeoDataFrame
- Raises:
BNGResolutionError – If an invalid resolution is provided.
BNGExtentError – If the coordinates of a Point geometry are outside of the BNG index system extent.
TypeError – If the input is not a
GeoPandasGeoDataFrame.ValueError – If the
GeoDataFrameCRS is not equal to “EPSG:27700”ValueError – If an active geometry column is not set in the
GeoDataFrame.ValueError – If the geometry type is not supported.
Examples
>>> import geopandas as gpd >>> import pandas as pd >>> df = pd.DataFrame({ ... "id": ["0", "1", "2"], ... "geometry": [ ... "POLYGON((530000 180000, 535000 180000, 535000 185000, 530000 185000, 530000 180000))", ... "POLYGON((540000 190000, 545000 190000, 545000 195000, 540000 195000, 540000 190000))", ... "POLYGON((550000 200000, 555000 200000, 555000 205000, 550000 205000, 550000 200000))" ... ] ... }) >>> gdf = gpd.GeoDataFrame(df, geometry=gpd.GeoSeries.from_wkt(df["geometry"]), crs="EPSG:27700") >>> gdf_to_bng_intersection_explode(gdf, resolution="1km") id bng_ref 0 0 BNGReference(bng_ref_formatted=TQ 31 80, resol... 1 0 BNGReference(bng_ref_formatted=TQ 30 82, resol... 2 0 BNGReference(bng_ref_formatted=TQ 33 84, resol... 3 0 BNGReference(bng_ref_formatted=TQ 34 80, resol... 4 0 BNGReference(bng_ref_formatted=TQ 33 80, resol... .. ... ... 70 2 BNGReference(bng_ref_formatted=TL 52 01, resol... 71 2 BNGReference(bng_ref_formatted=TL 52 03, resol... 72 2 BNGReference(bng_ref_formatted=TL 50 04, resol... 73 2 BNGReference(bng_ref_formatted=TL 50 03, resol... 74 2 BNGReference(bng_ref_formatted=TL 54 01, resol... is_core geometry 0 True POLYGON ((532000 180000, 532000 181000, 531000... 1 True POLYGON ((531000 182000, 531000 183000, 530000... 2 True POLYGON ((534000 184000, 534000 185000, 533000... 3 True POLYGON ((535000 180000, 535000 181000, 534000... 4 True POLYGON ((534000 180000, 534000 181000, 533000... .. ... ... 70 True POLYGON ((553000 201000, 553000 202000, 552000... 71 True POLYGON ((553000 203000, 553000 204000, 552000... 72 True POLYGON ((551000 204000, 551000 205000, 550000... 73 True POLYGON ((551000 203000, 551000 204000, 550000... 74 True POLYGON ((555000 201000, 555000 202000, 554000... [75 rows x 4 columns]
See also
geom_to_bng_intersectionto indexShapelygeometries directly.