BNG
Convert between BNG grid refs (e.g. NT123456) and OSGB36 (EPSG:27700) coords
Coordinates in the Ordnance Survey National Grid or British National Grid are often defined by alphanumeric grid references that refer to grid squares.
These are based on the osgb36 (EPSG:27700) coordinate reference system but are not understood by most GIS software.
This module contains Python functions to convert 4, 6, 8, or 10 figure alphanumeric grid references to/from pure osgb36 coordinates.
This code was originally published on a blog post: Easily change coordinate projection systems in Python with pyproj. The blog post contains more information and instructions for converting between coordinate systems using Python.
Installation
BNG can be installed for Python 2.7 or Python 3 using pip:
pip install bng
Instructions
The to_osbg36 and from_osbg36 functions are used to convert between tuples of OSGB36 (x, y) coordinates and alphanumeric grid references.
to_osbg36
BNG grid references can be converted to osbg36 coordinates as follows.
Single values:
import bng
bng.to_osgb36('NT2755072950')
# (327550, 672950)
The coordinates correspond to the southwest corner of the grid square described by the grid reference.
For multiple values, use Python's zip function and list comprehension:
import bng
gridrefs = ['HU431392', 'SJ637560', 'TV374354']
x, y = zip(*[bng.to_osgb36(g) for g in gridrefs])
x
# (443100, 363700, 537400)
y
# (1139200, 356000, 35400)
from_osbg36
BNG grid references can be created fromosbg36 coordinates as follows.
Single values:
import bng
bng.from_osgb36((327550, 672950), figs=6)
# 'NT275729'
The number of figures in the grid reference can be specified. The coordinates correspond to the southwest corner of the grid square containing the (x, y) coordinates.
For multiple values, use Python's zip function and list comprehension:
import bng
x = [443143, 363723, 537395]
y = [1139158, 356004, 35394]
[bng.from_osgb36(coords, figs=4) for coords in zip(x, y)]
# ['HU4339', 'SJ6356', 'TV3735']
Converting grid references to GPS coordinates
BNG can be combined with pyproj (see blog post) to convert grid references to many different coordinate systems.
BNG grid references can be converted to lat/lon as used by GPS systems (EPSG:4326) as follows:
import bng
import pyproj
# Define coordinate systems
wgs84=pyproj.CRS("EPSG:4326") # LatLon with WGS84 datum used by GPS units and Google Earth
osgb36=pyproj.CRS("EPSG:27700") # UK Ordnance Survey, 1936 datum
# Transform
x, y = bng.to_osgb36('NT2755072950')
pyproj.transform(osgb36, wgs84, x, y)
# (55.94410954187127, -3.1615548049941133i)
Note: older versions of pyproj use pyproj.Proj("+init=EPSG:4326") syntax
and return coordinates in lon, lat
order.
GPS coordinates can be converted to BNG grid references as follows:
import bng
import pyproj
# Define coordinate systems
wgs84=pyproj.CRS("EPSG:4326") # LatLon with WGS84 datum used by GPS units and Google Earth
osgb36=pyproj.CRS("EPSG:27700") # UK Ordnance Survey, 1936 datum
# Transform
lon = -3.1615548588213667
lat = 55.944109545140932
x, y = pyproj.transform(wgs84, osgb36, lat, lon)
bng.from_osgb36((x, y))
# 'NT275729
Note that for surveying work (i.e. < 1 m accuracy) it is necessary to make a geoid correction. Proj uses grid correction files in NTv2 format to make this correction. The Ordnance Survey provide these files (OSTN2 transformation model) on their website.
For Developers
Install developer dependencies:
pip install -r requirements.txt
Run tests:
pytest -vs test_bng.py
Metadata
Release files for bng 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bng-1.0.5.zip | 9.9 kB | Without provenance | Details for bng-1.0.5.zip |
Release files / bng-1.0.5.zip
| Download URL | bng-1.0.5.zip |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
87c83da9cd60ee99b0c9cfa79405df943f0d164dd9920f65df98235c423f597a
|
|
BLAKE2b-256 checksum How to use checksums |
69de423e7302da6f44e63eba2783aa8f20eb36538308057b14e10e51a606a38e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
|
Security for bng 1.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.