convertbng
raw JSON → 0.7.11 verified Fri May 01 auth: no python
Convert latitude/longitude to and from British National Grid (OSGB36) / ETRS89 coordinates using the OS OSTN15 transform. The library is powered by Rust FFI for high performance. Current version 0.7.11, requires Python >=3.10. Released irregularly.
pip install convertbng Common errors
error ImportError: No module named 'convertbng.util' ↓
cause Incorrect import path; users try `from convertbng import convert_lonlat_to_bng`.
fix
Use
from convertbng.util import convert_lonlat_to_bng. error TypeError: float() argument must be a string or a number, not 'list' ↓
cause Passing a Python list directly instead of a numpy array.
fix
Wrap inputs in numpy arrays: np.array(lons, dtype=np.float64).
error ValueError: Buffer dtype mismatch, expected 'float64' but got 'float32' ↓
cause Input array dtype is float32; the Rust backend expects float64.
fix
Cast arrays to float64: arr.astype(np.float64).
Warnings
breaking Installation may require Rust toolchain (rustc and cargo) if no precompiled wheel is available for your platform. This can cause cryptic errors on some systems. ↓
fix Install Rust via https://rustup.rs/ or use a conda-forge build: conda install -c conda-forge convertbng
gotcha Input must be numpy arrays of float64 type. Passing lists or other dtypes will raise TypeError or produce wrong results. ↓
fix Always convert inputs to numpy arrays: np.array(values, dtype=np.float64)
gotcha Coordinates outside the British National Grid coverage area (approx. 49°N–61°N, 8°W–2°E) will return NaN. Error handling not built-in; check for NaN yourself. ↓
fix Validate results using np.isnan(easting) or similar.
Imports
- convert_lonlat_to_bng wrong
from convertbng import convert_lonlat_to_bngcorrectfrom convertbng.util import convert_lonlat_to_bng - convert_bng_to_lonlat wrong
from convertbng import convert_bng_to_lonlatcorrectfrom convertbng.util import convert_bng_to_lonlat
Quickstart
import numpy as np
from convertbng.util import convert_lonlat_to_bng, convert_bng_to_lonlat
# Example coordinates (lon, lat)
lon = np.array([-0.1276, -2.0])
lat = np.array([51.5074, 53.0])
# Convert to BNG (returns (easting, northing) tuple of numpy arrays)
easting, northing = convert_lonlat_to_bng(lon, lat)
print(easting, northing)
# Convert back to lon/lat
lon_back, lat_back = convert_bng_to_lonlat(easting, northing)
print(lon_back, lat_back)