Fast lon, lat to and from ETRS89 and BNG (OSGB36) using Rust FFI
A utility library for converting decimal WGS84 longitude and latitude coordinates into ETRS89 (EPSG:25830) and/or British National Grid (More correctly: OSGB36, or EPSG:27700) Eastings and Northings, and vice versa.
pip install convertbng
Please use an up-to-date version of pip (8.1.2 as of June 2016)
The package has been built for and tested on the following platforms:
The Rust DLL and the Cython extension used by this package have been built with a MinGW toolchain. You shouldn’t need to install any additional runtimes in order for the wheel to work, but please open an issue if you encounter any errors.
The functions accept either a sequence (such as a list or numpy array) of longitude or easting values and a sequence of latitude or northing values, or a single longitude/easting value and single latitude/northing value. Note the return type: "returns a list of two lists containing floats, respectively"
NOTE: Coordinate pairs outside the BNG bounding box, or without OSTN02 coverage will return a result of [[nan], [nan]], which cannot be mapped. Since transformed coordinates are guaranteed to be returned in the same order as the input, it is trivial to check for this value. Alternatively, ensure your data fall within the bounding box before transforming them:
Latitude: East: 1.7800 West: -7.5600 Longitude: North: 60.8400 South: 49.9600
All functions try to be liberal about what containers they accept: list, tuple, array.array, numpy.ndarray, and pretty much anything that has the __iter__ attribute should work, including generators.
from convertbng.util import convert_bng, convert_lonlat # convert a single value res = convert_bng(lon, lat) # convert lists of longitude and latitude values to OSGB36 Eastings and Northings, using OSTN02 corrections lons = [lon1, lon2, lon3] lats = [lat1, lat2, lat3] res_list = convert_bng(lons, lats) # convert lists of BNG Eastings and Northings to longitude, latitude eastings = [easting1, easting2, easting3] northings = [northing1, northing2, northing3] res_list_en = convert_lonlat(eastings, northings) # assumes numpy imported as np lons_np = np.array(lons) lats_np = np.array(lats) res_list_np = convert_bng(lons_np, lats_np)
If you’re comfortable with restricting yourself to NumPy f64 arrays, you may use the Cython functions contained in convertbng.cutil instead. These are identical to those listed below, and are selected by changing the import statement to “from convertbng.cutil import“.
The conversion functions will accept most sequences which implement __iter__, as above (list, tuple, float, array.array, numpy.ndarray), but will always return “NumPy f64 ndarray“. In addition, you must ensure that your inputs are float, f64, or d in the case of array.array.
This module is currently experimental, and should not be used in production unless you’re comfortable verifying the results.
Provided for completeness:
[…] In Europe, ETRS89 is a precise version of the better known WGS84 reference system optimised for use in Europe; however, for most purposes it can be considered equivalent to WGS84. Specifically, the motion of the European continental plate is not apparent in ETRS89, which allows a fixed relationship to be established between this system and Ordnance Survey mapping coordinate systems. Additional precise versions of WGS84 are currently in use, notably ITRS; these are not equivalent to ETRS89. The difference between ITRS and ETRS89 is in the order of 0.25 m (in 1999), and growing by 0.025 m per year in UK and Ireland. This effect is only relevant in international scientific applications. For all navigation, mapping, GIS, and engineering applications within the tectonically stable parts of Europe (including UK and Ireland), the term ETRS89 should be taken as synonymous with WGS84.
—Transformations and OSGM02™, User guide, p7. Emphasis mine.
In essence, this means that anywhere you see ETRS89 in this README, you can substitute WGS84.
convert_bng and convert_lonlat first use the standard seven-step Helmert transform to convert coordinates. This is fast, but not particularly accurate – it can introduce positional error up to approximately 5 metres. For most applications, this is not of particular concern – the input data (especially those originating with smartphone GPS) probably exceed this level of error in any case. In order to adjust for this, the OSTN02 adjustments for the kilometer-grid the ETRS89 point falls in are retrieved, and a linear interpolation to give final, accurate coordinates is carried out. This process happens in reverse for convert_lonlat.
OSTN02 data are used for highly accurate conversions from ETRS89 latitude and longitude, or ETRS89 Eastings and Northings to OSGB36 Eastings and Northings, and vice versa. These data will usually have been recorded using the National GPS Network:
Conversion of your coordinates using OSTN02 transformations will be accurate, but if you’re using consumer equipment, or got your data off the web, be aware that you’re converting coordinates which probably weren’t accurately recorded in the first place. That’s because accurate surveying is difficult.
WGS84 and ETRS89 coordinates use the GRS80 ellipsoid, whereas OSGB36 uses the Airy 1830 ellipsoid, which provides a regional best fit for Britain. Positions for coordinates in Great Britain can differ by over 100m as a result. It is thus inadvisable to attempt calculations using mixed ETRS89 and OSGB36 coordinates.
The main detail of interest is the FFI interface between Python and Rust, the Python side of which can be found in util.py, cutil.pyx, and the Rust side of which can be found in ffi.rs. The ctypes library expects C-compatible data structures, which we define in Rust (see above). We then define methods which allow us to receive, safely access, return, and free data across the FFI boundary. Finally, we link the Rust conversion functions from util again. Note the errcheck assignments, which convert the FFI-compatible ctypes data structures to tuple lists.
You can run the Python module tests by running “make test”. Tests require both numpy and nose.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|convertbng-0.5.7-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.macosx_10_11_intel.macosx_10_11_x86_64.whl (14.0 MB) Copy SHA256 Checksum SHA256||cp27||Wheel||May 30, 2017|
|convertbng-0.5.7-cp27-cp27mu-manylinux1_x86_64.whl (14.2 MB) Copy SHA256 Checksum SHA256||cp27||Wheel||May 30, 2017|
|convertbng-0.5.7-cp27-cp27m-win32.whl (14.2 MB) Copy SHA256 Checksum SHA256||cp27||Wheel||May 30, 2017|
|convertbng-0.5.7-cp27-cp27m-win_amd64.whl (14.1 MB) Copy SHA256 Checksum SHA256||cp27||Wheel||May 30, 2017|
|convertbng-0.5.7-cp34-cp34m-win_amd64.whl (14.1 MB) Copy SHA256 Checksum SHA256||cp34||Wheel||May 30, 2017|
|convertbng-0.5.7-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.macosx_10_11_intel.macosx_10_11_x86_64.whl (14.0 MB) Copy SHA256 Checksum SHA256||cp36||Wheel||May 30, 2017|
|convertbng-0.5.7-cp36-cp36m-manylinux1_x86_64.whl (14.2 MB) Copy SHA256 Checksum SHA256||cp36||Wheel||May 30, 2017|