# Converting (British) National Grid references

October 13, 2016
By

[This article was first published on R – scottishsnow, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I was supplied spatial point data this summer where the locations were detailed in Ordnance Survey’s National Grid (NGR) format. If you’re not familiar with it, rather than using eastings and northings (in metres) to define location – it splits the Great Britain into lettered grid squares and then defines locations within each lettered grid square. The best explanation I’ve see is this, from the Ordnance Survey (appropriately). The NGR is really useful if you’re working/walking in the same grid square and only need a ‘local’ reference, but they’re pretty useless when you want to map locations in a GIS (e.g. QGIS, GRASS, ArcGIS, MapInfo, etc.). In this post I’ll show you how to convert from NGR to eastings and northings.

NGR coordinates are of the form LL XXX YYY (or LLXXXYYY), where LL are letters, XXX are the easting and YYY are the northing. This ‘6 figure’ grid reference will give you a location to the nearest 100 metres. XXX and YYY can vary in length, to give you different levels of precision. What we want are two separate numbers – XXXXXX, YYYYYY – which describe a location to the nearest metre. There are a few tools out there which do this, some are detailed in this Google+ thread, here I’m going to focus on using R. R is an open source analytical programming language, you can find instructions for installing it on my software page.

There’s no need to write a script from scratch to convert grid references, someone has done it already! Incidentally, this was the first R package I contributed to. There is some legwork to do in getting your NGR coordinates in a format ready for the conversion. The script that follows does just that, taking a csv file as your start and end point:

```# -------------------------------------
# Install and load the rnrfa package

install.packages("rnrfa")
library(rnrfa)

# -------------------------------------

df = read.csv("~/dir/dir/points.csv", stringsAsFactors=F)

# -------------------------------------
# Get easting and northing

# If required, remove spaces in the NGR
df\$NGR = gsub(" ", "", df\$NGR, fixed=T)

# Convert NGR to easting and northing
x = osg_parse(df\$NGR)

# Extract easting and northing from the list, x
df\$east = x[[1]]
df\$north = x[[2]]

# -------------------------------------
# Write clean file

write.csv(df, "~/dir/dir/points_clean.csv", row.names=F)
```

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...