Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / C#

Converting Latitude and Longitude to British National Grid in C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (21 votes)
28 Jun 2017CPOL10 min read 32.4K   21   5
Presents an explanation of Datum transformations with examples shown in C#

Introduction

This article aims to demystify the concepts of transforming from one coordinate system to another. The article will walk through some C# examples, including how to convert from GPS coordinates to British National Grid and back again. The freely available GeoUK NuGet package is used for these examples. GeoUK was built as a Portable Class Library project (Profile 78) and as such can be added to Visual Studio or Xamarin and runs against .Net 4.5+, Windows Phone 8+, Xamarin.IOS/Android, Windows 8 Store Apps and so on.

One thing I should mention is that this article is a greatly simplified discussion of a complex topic. Ordnance Survey provides detailed documentation in relation to coordinate systems used in the UK.

Latitude and Longitude

The earth is almost spherical. If you took at an inflated beach ball, placed your foot on it and gently pressed, you would have a squashed sphere, referred to as an ellipsoid. The earth is ellipsoidal, albeit with a few lumps and bumps.

The Latitude and Longitude coordinate system can describe any point on earth, however, it can only do that if we have first defined the details of the earth such as the origins of the Latitude and Longitude system and the size and shape of the ellipsoid to be used. These definitions are referred to as a Datum. The thing is, different ellipsoids, and therefore different datums, are often used depending upon where in the world you are. In some cases, several datums can be in use at the same location depending upon specific requirements. This means that Latitude and Longitude can only describe a point on the earth if we know which datum is being used. It can be shown that if a Latitude and Longitude based on the OSGB36 datum were entered into a GPS using a WGS84 datum, it would point to a different place. Therefore, being able to transform coordinates to and from different datums is essential, the examples shown later in the article will show how to do this in C# using the GeoUK package.

Why So Many Datums

Image 1

Before moving on to discuss coordinate systems, it is worth explaining why we use different ellipsoids, and therefore different datums. WGS84 is a standard datum used as the default in most GPS systems, so why not simply use that? The figure below shows a rough image of the earth with all of its lumps and bumps. The large ellipsoid whilst not fitting perfectly gives us a general ellipsoid that can be used all over the world, think of this as WGS84. However, the smaller ellipsoid can be seen to be a better fit for a small part of the earth. Therefore, if you happen to be on this small part of the earth, using this smaller ellipsoid would give greater accuracy. Hence, multiple ellipsoids and the need to convert between them. If you are in the UK, this smaller ellipsoid would be the Airy Ellipsoid, part of the OSGB36 datum. The ellipsoid was originally defined by Sir George Airy in 1830.

 

Something else to consider is that the WGS84 position of any particular point on the Earth’s surface is changing continuously due to various effects, the most important of which is tectonic motion. So WGS84 itself is unsuitable for mapping as the ground would keep sliding across the surface of any WGS84 based mapping grid. This is not the case with a system that covers a small region as the mapping grid can move also. As a result, there are datums, in various parts of the world, that were based on the WGS84 datum at one point in time (Epoch), but have diverged from it slightly in order to account for land mass movement. One such datum in Europe is called ETRS89. More of that later.

Different Coordinate Systems

The Latitude and Longitude system is probably the most well known coordinate system. However, here in the UK, Ordnance Survey maps often use Eastings and Northings (Map References) as a coordinate system. Cartesian coordinates (X, Y and Z) can also be very useful. It is not the intention to describe Cartesian coordinates here, however, the code detailed below will use them in intermediate steps.

It is important to understand that converting between coordinates occurs within the same datum. Converting an OSGB36 Easting and Northing to Latitude and Longitude, returns a Latitude and Longitude based on the same OSGB36 datum. To convert these coordinates to Latitude and Longitude for a GPS using WGS84, a datum transformation is also required. The examples shown below will show how to do this in C# using the GeoUK package.

There is no accurate method for transforming datums, however, the Helmert transformation can be used as long as the accuracy required is not less than 5 metres. For centimetre accuracy, an altogether different approach needs to be taken. This is described in the section Obtaining Greater Accuracy.

Map Projections

Map showing Easting and Northing Coordinates

Before we get into the actual code, it would be worth quickly discussing Projections.

A projection can be thought of any function that transfers points on an ellipsoid onto a plane surface such as a Map. For our purposes, we can consider this to mean converting a Latitude and Longitude to an Easting and Northing. What this means is that any conversion to or from Easting/Northing coordinates, needs a Projection specifying.

Getting Started

The GeoUK package is a PCL78 assembly and can be used with .NET and Mono. Add the package to Xamarin or Visual Studio using the NuGet Package Manager prompt.

C#
> Install-Package GeoUK

The first example takes an Ordnance Survey map Easting/Northing and transforms it to a GPS (WGS84) Latitude and Longitude. Therefore, a coordinate conversion is required (Easting/Northing to Latitude/Longitude) and a datum transformation is also required (OSGB36 to WGS84). As previously mentioned, the ETRS89 datum is the European version of WGS84. In 1989, these datums were aligned, since then they have diverged very slightly, however, for our purposes we can consider them to be the same.

First the coordinate conversion. The Easting/Northing is from an Ordnance Survey map and is therefore using the OSGB36 datum with the Airy ellipsoid and the British National Grid projection (BNG).

Step 1: Convert to Cartesian

C#
using GeoUK;
using GeoUK.Projections;
using GeoUK.Coordinates;
using GeoUK.Ellipsoids;
...

// Given an easting and northing in metres (see text)
const double easting = 651409.903;
const double northing = 313177.270;

// Convert to Cartesian
var cartesian = Convert.ToCartesian (new Airy1830 (),
        new BritishNationalGrid (),
        new EastingNorthing (
            easting, 
            northing));

These new Cartesian coordinates are still with reference to the OSGB datum. Once we have the coordinates as Cartesian, we can make the transformation to ETRS89 (WGS84).

Step 2: Transform from OSBB36 datum to ETRS89 datum (see article text)

C#
var wgsCartesian = Transform.OSBB36ToEtrs89 (cartesian); //ETRS89 is effectively WGS84   

Now we have Cartesian results referencing the WGS84 datum (using the WGS84 ellipsoid) and can simply convert those to Latitude/Longitude.

Step 3: Convert back to Latitude/Longitude

C#
var wgsLatLong = Convert.ToLatitudeLongitude (new Wgs84 (), 
                                                wgsCartesian);

To summarise, for a datum change of Easting Northing coordinates from datum A to datum B, first convert to Cartesian coordinates using the ellipsoid of datum A. Then, apply a transformation from datum A to datum B before converting back to latitude/longitude using the ellipsoid of datum B. The same process is used going the other way.

British National Grid Example

Transformation

The code below transforms from WGS84/ETRS89 Latitude/Longitude to OSGB36 Easting/Northing in the manner described above.

C#
var latLong = new LatitudeLongitude(52.6579785974266, 1.71605194571289);

var cartesian = Convert.ToCartesian (new Wgs84 (), latLong);
var bngCartesian = Transform.Etrs89ToOsgb36 (cartesian);
var bngEN = Convert.ToEastingNorthing (new Airy1830 (), new BritishNationalGrid (), bngCartesian);

OS Map References

The map references (Easting/Northing) used in Ordnance Survey maps are divided into 500km squares which are sub-divided into 100km squares. These squares are given a two letter code. The first letter represents the 500km square and the second represents the 100km square within it. A six digit map reference would look something like TL123456 where the first two characters represents the 100km square as indicated on the map with the first three digits of the six representing the easting and the last three digits representing the northing. Using this system means that a map reference is quoted as an easting/northing (in metres) from the square's origin. An EastingNorthing coordinate object, as returned from the transformation described above, can be converted to an OS map reference by using the Osgb36 class as follows:

C#
// Convert to Osgb36 coordinates by creating a new object passing  
// in the EastingNorthing object to the constructor.
var osgb36EN = new Osgb36(bngEN);
var mapReference = osgb36EN.MapReference;

Height

Height is a complex topic and there are many ways to measure it. The term 'sea level' is often used in conversation but the sea changes all of the time, large land mass has an effect on gravity and this affects the sea. Consider also that some of the earths highest mountains can be found under the sea. As I said, it all gets very tricky very quickly.

As we have seen above, we use an ellipsoid to represent the earth in our conversions, this means that height when represented within the LatitudeLongitude coordinates object is usually ellipsoid height. Ellipsoid height is not very useful as point A can have a greater ellipsoid height than point B while being downhill of B. This is due to the fact that an ellipsoid is not a level surface as it does not take account of the irregularities that exist with the earths surface.

Image showing Geoid model compared to the ellipsoid.

A level surface is a surface that is at right angles to gravity at all points. This means that it must take account of the various bumps and lumps that affect gravity. A Geoid is a level surface and can be useful to measure height. A height measured in this way is called an Orthometric Height. A Geoid can be global (usually stated with a capital 'G') or can be local to a country or region (usually stated with a lower case 'g').

Ordnance Survey mapping on the British mainland uses the Ordnance Datum Newlyn (ODN) vertical coordinate system. ODN corresponds to the average sea level measured by the tide-gauge at Newlyn, Cornwall between 1915 and 1921. Heights that refer to this particular 'mean sea level' as the point of zero height are called ODN heights. ODN is therefore a local geoid definition as mentioned above. ODN heights are used for all British mainland Ordnance Survey contours, spot heights and bench mark heights. Many islands around Britain have their own 'mean sea level' (local geoid) based on local tide-gauges.

In terms of the examples detailed here relating to the transformation from WGS84 to OSGB36, the WGS ellipsoid height is converted to something like ODN heights, accuracy is around 5 meters in all directions.

Obtaining Greater Accuracy

In order to obtain greater accuracy when transforming ETRS89 (WGS84) coordinates to British National Grid, the Ordnance Survey Geoid Model (OSGM02) needs to be used. The OSGM02 can be thought of as a large rubber sheet covering Great Britain, Northern Island and the Republic of Ireland. Special transformations are applied to the data within the OSGM02 to transform from ETRS89 and OSGB36. For Great Britain, the transformation is called OSTN02. The OSTN02 transformations combined with the ETRS89 positions of active GPS network stations represents the official definition of OSGB36 and can give very accurate transformations.

This rubber sheet geoid is effectively a lookup table that can be used to determine Othometric (geoid) heights and, via the OSTN transformation, accurate Easting and Northing coordinates. It is worth noting that Northern Island and the Republic of Ireland use the same geoid model but with a different transformation (OSi/OSNI) which, for now at least, is outside of the scope of this article.

The GeoUK.OSTN Nuget package extends the GeoUK package to include OSGM02/OSTN0 and OSTN15 functionality and provides a simple method to make an accurate one-way transformation from ETRS89 to BNG. The package can be added to a project using the following Package Manager command. The package is dependent upon the GeoUK package and will add it as required.

C#
> Install-Package GeoUK.OSTN

It should be noted that the GeoUK.OSTN package contains the OSGM02 geoid and OSTN02 OSTN15 transformations, as a result is fairly large, in addition, transformations will be slower than using the Helmert transformations as used in the examples above.

The example below converts an ETRS89 Latitude/Longitude/Ellipsoid height to BNG Easting and Northing and ODN Height to within 10 centimetres or so.

C#
var latLong = new LatitudeLongitude(52.658007833, 1.716073973, 108.05);
var bng = GeoUK.OSTN.Transform.Etrs89ToOsgb (latLong);  

Summary

This article discussed the various elements to be considered when converting from GPS coordinates to British National Grid. The Helmert transformation was demonstrated with an accuracy of around 5 meters in all directions and a more accurate method, that followed the Ordnance Survey recommended approach by using the OSGM02 geoid model, was also shown.

The library is provided free of charge via NuGet with the primary aim of simplifying an otherwise complex problem. 

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
John is the author of the free Genetic Algorithm Framework for .Net (GAF) and the series of GeoUK NuGet packages. John studied at Leicester De Montfort University and gained a Distinction for the highly regarded Masters Degree in Computational Intelligence and Robotics. John can provide commercial product support for the GAF or GAF based projects and can be contacted via the Code Project, LinkedIn or the usual social channels.

Comments and Discussions

 
Questionhow to get latitude of London? Pin
Southmountain13-Nov-23 12:43
Southmountain13-Nov-23 12:43 
Question8-digit OS grid ref? Pin
DerekT-P12-Feb-21 22:48
professionalDerekT-P12-Feb-21 22:48 
AnswerRe: 8-digit OS grid ref? Pin
Member 1515501116-Apr-21 0:51
Member 1515501116-Apr-21 0:51 
GeneralRe: 8-digit OS grid ref? Pin
DerekT-P17-Apr-21 10:50
professionalDerekT-P17-Apr-21 10:50 
PraiseGreat informative article. Pin
Ocean Airdrop28-Aug-17 18:58
Ocean Airdrop28-Aug-17 18:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.