osbng-py

A Python package supporting geospatial grid indexing and interaction with Ordnance Survey's British National Grid index system

https://github.com/ordnancesurvey/osbng-py

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.5%) to scientific vocabulary

Keywords

geospatial gis ordnance-survey-grid ordnancesurvey python spatial spatial-indexing
Last synced: 6 months ago · JSON representation ·

Repository

A Python package supporting geospatial grid indexing and interaction with Ordnance Survey's British National Grid index system

Basic Info
  • Host: GitHub
  • Owner: OrdnanceSurvey
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 23.3 MB
Statistics
  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • Open Issues: 3
  • Releases: 0
Topics
geospatial gis ordnance-survey-grid ordnancesurvey python spatial spatial-indexing
Created over 1 year ago · Last pushed 6 months ago
Metadata Files
Readme License Citation

README.md

osbng

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.

Overview

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.

BNG index system overview at 100km, 10km and 1km resolutions

Installation

Install osbng from GitHub using pip:

shell pip install git+https://github.com/OrdnanceSurvey/osbng-py.git

Complimentary Tools

  • 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.

Usage

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:

BNG Reference

osbng implements a custom BNGReference object. This object validates and encapsulates a BNG reference, providing properties and methods to access and manipulate the reference.

``` python

from osbng.bngreference import BNGReference bngref = BNGReference(bngrefstring="ST57SE") bngref.bngrefformatted 'ST 5 7 SE' bngref.resolutionmetres 5000 bngref.resolutionlabel '5km' bngref.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)),)}} ```

Indexing

Provides the ability to index and work with coordinates and geometries against the BNG index system. This includes:

  • Encoding easting and northing coordinates into BNGReference objects at a specified resolution.
  • Decoding BNGReference objects back into coordinates, bounding boxes and grid squares as Shapely geometries.
  • Indexing bounding boxes and Shapely geometries into grid squares at a specified resolution for spatial analysis.

BNG Grid Squares at 5km Resolution Intersected by London Region Decomposition of the London Region into BNG Grid Squares at 5km Resolution

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:

``` python

from osbng.indexing import xytobng bngref = xytobng(easting=356976, northing=171421, resolution="5km") bngref.bngtoxy(position="lower-left") (355000, 170000) bngref.bngtobbox() (355000, 170000, 360000, 175000) bngref.bngtogrid_geom().wkt 'POLYGON ((360000 170000, 360000 175000, 355000 175000, 355000 170000, 360000 170000))' ```

Indexing GeoPandas (GPD)

Optional functionality is available when the GeoPandas package is installed. This enables indexing of geometries in a GeoDataFrame against the BNG index system. Includes:

  • Indexing geometries in a GeoDataFrame into grid squares at a specified resolution, and explode the resulting lists of indexed objects into a flattened GeoDataFrame for further analysis.

Hierarchy

Provides functionality to navigate the hierarchical structure of the BNG index system. This includes:

  • Returning parents and children of BNGReference objects at specified resolutions.

The following example returns the parent of a BNGReference:

``` python

bngref = BNGReference(bngrefstring="ST5671SE") bngref.resolutionlabel '500m' bngref.bngtoparent(resolution="10km") BNGReference(bngrefformatted=ST 5 7, resolution_label=10km) ```

Traversal

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:

  • Generation of k-discs and k-rings around a given grid square.
  • Identification of neighbouring grid squares and checking adjacency.
  • Calculating the distance between grid square centroids.
  • Retrieving all grid squares within a specified absolute distance.

The following example returns a k-disc of a BNGReference object:

``` python

bngref = BNGReference(bngrefstring="ST5671SE") bngref.bngkdisc(k=1) [BNGReference(bngrefformatted=ST 56 71 NW, resolutionlabel=500m), BNGReference(bngrefformatted=ST 56 71 NE, resolutionlabel=500m), BNGReference(bngrefformatted=ST 57 71 NW, resolutionlabel=500m), BNGReference(bngrefformatted=ST 56 71 SW, resolutionlabel=500m), BNGReference(bngrefformatted=ST 56 71 SE, resolutionlabel=500m), BNGReference(bngrefformatted=ST 57 71 SW, resolutionlabel=500m), BNGReference(bngrefformatted=ST 56 70 NW, resolutionlabel=500m), BNGReference(bngrefformatted=ST 56 70 NE, resolutionlabel=500m), BNGReference(bngrefformatted=ST 57 70 NW, resolutionlabel=500m)] ```

Grids

Provides functionality to generate BNG grid square data within specified bounds. This includes:

  • Returning a GeoJSON-like mapping for grid squares implementing the __geo_interface__ protocol supporting integration with other tools in the Python geospatial ecosystem.
  • Grid square data covering the BNG index system bounds is provided as an iterator at 100km, 50km, 10km, 5km and 1km resolutions.

The following example constructs a GeoPandas GeoDataFrame from one of the iterators:

``` python

import geopandas as gpd from osbng.grids import bnggrid10km gdf = gpd.GeoDataFrame.fromfeatures(bnggrid_10km, crs=27700) ```

Contributing

Please raise an issue to discuss features, bugs or ask general questions.

License

The osbng package is licensed under the terms of the MIT License.

Owner

  • Name: Ordnance Survey
  • Login: OrdnanceSurvey
  • Kind: organization
  • Location: GB

Britain's mapping agency

Citation (CITATION.cff)

cff-version: 1.2.0
title: >-
  'osbng: Geospatial Grid Indexing and Interaction With the British
  National Grid'
message: 'To cite package osbng in publications use:'
type: software
authors:
  - given-names: Steve
    family-names: Kingston
    email: steve.kingston@os.uk
  - given-names: Kate
    family-names: New
    email: kate.new@os.uk
  - given-names: Tom
    family-names: Peterken
    email: tom.peterken@os.uk
  - given-names: Chris
    family-names: Jochem
    email: chris.jochem@os.uk
repository-code: 'https://github.com/OrdnanceSurvey/osbng-py'
abstract: >-
  Offers a streamlined programmatic interface to Ordnance
  Survey's British National Grid (BNG) index system,
  enabling efficient spatial indexing and analysis based on
  grid references. It supports a range of geospatial
  applications, including statistical aggregation, data
  visualisation, and interoperability across datasets.
  Designed for developers and analysts working with
  geospatial data in Great Britain, osbng simplifies
  integration with geospatial workflows and provides
  intuitive tools for exploring the structure and logic of
  the BNG system.
keywords:
  - python
  - geospatial
  - gis
  - spatial
  - ordnance survey
  - spatial indexing
  - british national grid
  - ordnance survey grid
license: MIT
version: 0.3.2
date-released: 2025-08-15
preferred-citation:
  type: software
  title: 'osbng: Geospatial Grid Indexing and Interaction With the British National Grid'
  authors:
  - name: Ordnance Survey
  institution:
    name: Ordnance Survey Ltd
  year: '2025'
  version: 0.3.2
  repository-code: https://github.com/OrdnanceSurvey/osbng-py

GitHub Events

Total
  • Create event: 10
  • Commit comment event: 1
  • Issues event: 10
  • Watch event: 5
  • Delete event: 9
  • Issue comment event: 17
  • Push event: 64
  • Public event: 1
  • Pull request review comment event: 61
  • Pull request event: 18
  • Pull request review event: 85
Last Year
  • Create event: 10
  • Commit comment event: 1
  • Issues event: 10
  • Watch event: 5
  • Delete event: 9
  • Issue comment event: 17
  • Push event: 64
  • Public event: 1
  • Pull request review comment event: 61
  • Pull request event: 18
  • Pull request review event: 85

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 2
  • Total pull requests: 1
  • Average time to close issues: 15 days
  • Average time to close pull requests: about 20 hours
  • Total issue authors: 2
  • Total pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 4.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 1
  • Average time to close issues: 15 days
  • Average time to close pull requests: about 20 hours
  • Issue authors: 2
  • Pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 4.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • steve-kingston (5)
  • tompeterken-os2 (1)
Pull Request Authors
  • steve-kingston (6)
  • tompeterken-os2 (5)
  • kat-1218 (1)
Top Labels
Issue Labels
enhancement (3) documentation (2) bug (2)
Pull Request Labels
enhancement (4) documentation (4) bug (2)

Dependencies

pyproject.toml pypi
  • shapely >=2.0.0
.github/workflows/ruff-checks.yml actions
  • actions/checkout v4 composite
  • actions/setup-python v4 composite
.github/workflows/run-pytest.yml actions
  • actions/checkout v4 composite
  • actions/setup-python v4 composite