Parsing OS Grid References

If you have ever been to an agile coding session you may have come across the concept of the coding kata. It is an exercise designed to improve coding skill by making you more aware of the different ways of building the same solution. It also tends to lend itself extremely well to TDD.

I was just looking at ways of converting OS National Grid References from their alphanumeric form to a purely numeric form and it occurred to me that it might make an excellent project for a coding kata.

So, what’s the deal with OS National Grid References. Well, they consist of two letters followed by a number of digits. For example NT2474. NT relates to a square 100km along each side. The first two digits represent eastings within that square, and the second two represent northings within the square. The complete reference gives you a square that is one kilometre along each side. Of course, you can modify this to produce larger or smaller squares. NT, NT27, NT245746. As The actual coordinate the grid reference resolves to is the south west corner of the square. Also, there are optional spaces between parts, so NT245746 could be written as NT 245 746.

There is a more detailed guide to the national grid on the Ordnance Survey website.


  1. Gary Short says:

    What would be even cooler is a conversion between OS and lat & long so you can use it with geocoding software.

  2. Ah, yes. But using what projection for geodetic coordinates. GPS software uses WGS84, other sources of data may use WGS72 or GRS80. There may be other projections in use also, these three are, as far as I’m aware, the most common. For SQL Server 2008 to work all the data must be in the same projection so it is important to know which you are using. If you tell it the wrong one the data won’t match up properly.

  3. I should correct that. If you tell SQL Server the wrong coordinate system for your data the data won’t match up IF you’ve marked it all into the same coordinate system. If you mark the data as coming from different coordinate systems it just won’t match the data as it will only perform spatial queries on data in the same coordinate system.

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s