Package: convertbng
DescriptionA utility library for converting decimalWGS84longitude and latitude coordinates into ETRS89 (EPSG:25830) and/or British National Grid (More correctly: OSGB36, orEPSG:27700) Eastings and Northings, and vice versa.Conversion is handled by aRust binaryusing FFI, and is quite fast. Some benchmarks can be foundhere.Installationpip install convertbngPlease use an up-to-date version of pip (8.1.2as of June 2016)Supported PlatformsThe package has been built for and tested on the following platforms:Linux x86_64 and aarch64 Python 3.{7, 8, 9, 10, 11} (Manylinux2014)macOS x86_64 and arm64 Python 3.{7, 8, 9, 10, 11}Windows 64-bit Python 3.{7, 8, 9, 10, 11}Windows BinariesThe Rust DLL and the Cython extension used by this package have been built with an MSVC 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.UsageThe 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,ora 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 OSTN15 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:Longitude:East: 1.7800West: -7.5600Latitude:North: 60.8400South: 49.9600All 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.fromconvertbng.utilimportconvert_bng,convert_lonlat# convert a single valueres=convert_bng(lon,lat)# convert longitude and latitude to OSGB36 Eastings and Northings using OSTN15 correctionslons=[lon1,lon2,lon3]lats=[lat1,lat2,lat3]res_list=convert_bng(lons,lats)# convert lists of BNG Eastings and Northings to longitude, latitudeeastings=[easting1,easting2,easting3]northings=[northing1,northing2,northing3]res_list_en=convert_lonlat(eastings,northings)# assumes numpy imported as nplons_np=np.array(lons)lats_np=np.array(lats)res_list_np=convert_bng(lons_np,lats_np)Cython ModuleIf you're comfortable with restricting yourself toNumPy f64arrays, you may use the Cython functions instead. These are identical to those listed below, but performance on large datasets is better. They are selected by changing the import statementfrom convertbng.util importtofrom convertbng.cutil importThe conversion functions will accept most sequences which implement__iter__, as above (list,tuple,float,array.array,numpy.ndarray), butwill always returnNumPy f64 ndarray. In addition, you must ensure that your inputs arefloat,f64, ordin the case ofarray.array.But I Have a List of Coordinate Pairscoords=[[1.0,2.0],[3.0,4.0]]a,b=list(zip(*coords))# a is (1.0, 3.0)# b is (2.0, 4.0)But I haveShapelyGeometriesfromconvertbng.utilimportconvert_etrs89_to_llfromshapely.geometryimportLineStringfromshapely.opsimporttransformfrommathimportisnanfromfunctoolsimportpartialdeftransform_protect_nan(f,xs,ys):# This function protects Shapely against NaN values in the output of the# transform, which would otherwise case a segfault.xs_t,ys_t=f(xs,ys)assertnotany([isnan(x)forxinxs_t]),"Transformed xs contains NaNs"assertnotany([isnan(y)foryinys_t]),"Transformed ys contains NaNs"returnxs_t,ys_tconvert_etrs89_to_lonlat_protect_nan=partial(transform_protect_nan,convert_etrs89_to_ll)line=LineString([[651307.003,313255.686],[651307.004,313255.687]])new_line=transform(convert_etrs89_to_lonlat_protect_nan,line)Available Conversions (AKA I Want To…)transform longitudes and latitudes to OSGB36 Eastings and Northingsvery accurately:useconvert_bng()transform OSGB36 Eastings and Northings to longitude and latitude,very accurately:useconvert_lonlat()transform longitudes and latitudes to ETRS89 Eastings and Northings,very quickly(without OSTN15 corrections):useconvert_to_etrs89()transform ETRS89 Eastings and Northings to ETRS89 longitude and latitude,very quickly(the transformation does not use OSTN15):useconvert_etrs89_to_lonlat()convert ETRS89 Eastings and Northings to their most accurate real-world representation, using the OSTN15 corrections:useconvert_etrs89_to_osgb36()Provided for completeness:transform accurate OSGB36 Eastings and Northings toless-accurateETRS89 Eastings and Northings:useconvert_osgb36_to_etrs89()Relationship between ETRS89 and WGS84FromTransformations and OSGM02™ User guide, p7. Emphasis mine.[…] 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.In essence, this means that anywhere you see ETRS89 in this README, you can substitute WGS84.What CRS Are My Data Inif you have latitude and longitude coordinates:They're probablyWGS84. Everything's fine!if you got your coordinates from a smartphone or a consumer GPS:They're probablyWGS84. Everything's fine!if you have x and y coordinates, or you got your coordinates from Google Maps or Bing Maps and they look something like(-626172.1357121646, 6887893.4928337997), or the phrase "Spherical Mercator" is mentioned anywhere:they're probably inWeb Mercator. Youmustconvert them to WGS84 first. Useconvert_epsg3857_to_wgs84([x_coordinates], [y_coordinates])to do so.Accuracyconvert_bngandconvert_lonlatfirst use the standard seven-stepHelmert transformto 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 OSTN15 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 forconvert_lonlat.OSTN15OSTN15data 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 theNational GPS Network:Accuracy ofYourDataConversion of your coordinates using OSTN15 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 becauseaccurate surveying is difficult.Accuracy of the OSTN15 transformation used in this libraryETRS89 longitude and latitude / Eastings and Northings to OSGB36 conversion agrees with the provided Ordnance Survey test data in39 of the 40test coordinates (excluding two coordinates designed to return no data; these correctly fail).The only discrepancy – in pointTP31–  is1mm.OSGB36 to ETRS89 longitude and latitude conversion is accurate to within 8 decimal places, or 1.1mm.A Note on EllipsoidsWGS84 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.ImplementationThe main detail of interest is the FFI interface between Python and Rust, the Python side of which can be found inutil.py(thectypesimplementation),cutil.pyx(thecythonimplementation), and the Rust side of which can be found inffi.rs.Thectypeslibrary 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 fromutil.pyagain. Note theerrcheckassignments, which convert the FFI-compatible ctypes data structures to tuple lists.Building the binary for local developmentensure you have Rust 1.x and Cargoinstalleddownload the Rust extension for your platform fromgithubcopy the binary into theconvertbngdirectoryrunpython setup.py build_ext --inplaceTestsinstallpytestrunpytestLicenseMITCitingConvertbngIf Convertbng has been significant in your research, and you would like to acknowledge the project in your academic publication, we suggest citing it as follows (example in APA style, 7th edition):Hügel, S. (2021). Convertbng (Version X.Y.Z) [Computer software].https://doi.org/10.5281/zenodo.5774931In Bibtex format:@software{Hugel_Convertbng_2021,
author = {Hügel, Stephan},
doi = {10.5281/zenodo.5774931},
license = {MIT},
month = {12},
title = {{Convertbng}},
url = {https://github.com/urschrei/convertbng},
version = {X.Y.Z},
year = {2021}
}