A Python library for Ordnance Survey's British National Grid (BNG) index system. This library provides tools for working with the BNG, a rectangular Cartesian grid system used to identify and index locations across Great Britain into grid squares at various resolutions.
The osbng
Python package provides a programmatic interface to the BNG, supporting efficient grid-based indexing and spatial analysis. This enables applications such as statistical aggregation, data visualisation, and data interoperability using BNG grid references. Designed for developers working with geospatial data in the context of Great Britain, the library offers tools to simplify working with the BNG, facilitating both technical integration into geospatial workflows and practical exploration of the index system's structure.
The package supports the 'standard' BNG metre-based resolutions, which represent powers of ten from 1m to 100km (1m, 10m, 100m, 1km, 10km, 100km
). It also supports the 'intermediate' quadtree resolutions (5m, 50m, 500m, 5km, 50km
), identified by an ordinal (NE, SE, SW, NW
) BNG reference direction suffix.
Install osbng
from GitHub using pip
:
pip install git+https://github.com/OrdnanceSurvey/osbng-py.git
osbng-r
, an R package with broad parity to the osbng
Python package.osbng-grids
, for BNG grid data in GeoParquet and GeoPackage (GPKG) formats.mosaic
, a Databricks package providing geospatial grid indexing using the BNG for Apache Spark.The osbng
package is structured into modules supporting different interactions with the BNG index system (e.g. indexing, hierarchy, traversal). A high-level summary of each module is provided below:
osbng
implements a custom BNGReference
object. This object validates and encapsulates a BNG reference, providing properties and methods to access and manipulate the reference.
>>> from osbng.bng_reference import BNGReference >>> bng_ref = BNGReference(bng_ref_string="ST57SE") >>> bng_ref.bng_ref_formatted 'ST 5 7 SE' >>> bng_ref.resolution_metres 5000 >>> bng_ref.resolution_label '5km' >>> bng_ref.__geo_interface__ {'type': 'Feature', 'properties': {'bng_ref': 'ST57SE'}, 'geometry': {'type': 'Polygon', 'coordinates': (((360000.0, 170000.0), (360000.0, 175000.0), (355000.0, 175000.0), (355000.0, 170000.0), (360000.0, 170000.0)),)}}
Provides the ability to index and work with coordinates and geometries against the BNG index system. This includes:
BNGReference
objects at a specified resolution.BNGReference
objects back into coordinates, bounding boxes and grid squares as Shapely
geometries.Shapely
geometries into grid squares at a specified resolution for spatial analysis.The following example demonstrates a round trip of constructing a BNGReference
object from easting northing coordinates, and then decoding back into coordinates, bounding box and Shapely geometry:
>>> from osbng.indexing import xy_to_bng >>> bng_ref = xy_to_bng(easting=356976, northing=171421, resolution="5km") >>> bng_ref.bng_to_xy(position="lower-left") (355000, 170000) >>> bng_ref.bng_to_bbox() (355000, 170000, 360000, 175000) >>> bng_ref.bng_to_grid_geom().wkt 'POLYGON ((360000 170000, 360000 175000, 355000 175000, 355000 170000, 360000 170000))'
Optional functionality is available when the GeoPandas
package is installed. This enables indexing of geometries in a GeoDataFrame
against the BNG index system. Includes:
GeoDataFrame
into grid squares at a specified resolution, and explode the resulting lists of indexed objects into a flattened GeoDataFrame
for further analysis.Provides functionality to navigate the hierarchical structure of the BNG index system. This includes:
BNGReference
objects at specified resolutions.The following example returns the parent of a BNGReference
:
>>> bng_ref = BNGReference(bng_ref_string="ST5671SE") >>> bng_ref.resolution_label '500m' >>> bng_ref.bng_to_parent(resolution="10km") BNGReference(bng_ref_formatted=ST 5 7, resolution_label=10km)
Provides functionality for traversing and calculating distances within the BNG index system. It supports spatial analyses such as distance-constrained nearest neighbour searches and 'distance within' queries by offering:
The following example returns a k-disc of a BNGReference
object:
>>> bng_ref = BNGReference(bng_ref_string="ST5671SE") >>> bng_ref.bng_kdisc(k=1) [BNGReference(bng_ref_formatted=ST 56 71 NW, resolution_label=500m), BNGReference(bng_ref_formatted=ST 56 71 NE, resolution_label=500m), BNGReference(bng_ref_formatted=ST 57 71 NW, resolution_label=500m), BNGReference(bng_ref_formatted=ST 56 71 SW, resolution_label=500m), BNGReference(bng_ref_formatted=ST 56 71 SE, resolution_label=500m), BNGReference(bng_ref_formatted=ST 57 71 SW, resolution_label=500m), BNGReference(bng_ref_formatted=ST 56 70 NW, resolution_label=500m), BNGReference(bng_ref_formatted=ST 56 70 NE, resolution_label=500m), BNGReference(bng_ref_formatted=ST 57 70 NW, resolution_label=500m)]
Provides functionality to generate BNG grid square data within specified bounds. This includes:
__geo_interface__
protocol supporting integration with other tools in the Python geospatial ecosystem.The following example constructs a GeoPandas
GeoDataFrame from one of the iterators:
>>> import geopandas as gpd >>> from osbng.grids import bng_grid_10km >>> gdf = gpd.GeoDataFrame.from_features(bng_grid_10km, crs=27700)
Please raise an issue to discuss features, bugs or ask general questions.
The osbng
package is licensed under the terms of the MIT License.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4