<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/converting-british-national-grid-references/"></div></div><div><a data-url="https://www.r-bloggers.com/converting-british-national-grid-references/" data-counturl="https://www.r-bloggers.com/converting-british-national-grid-references/" data-count="horizontal" data-text="Converting (British) National Grid references" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2016/10/13/converting-bng/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <a target="_blank" href="https://www.ordnancesurvey.co.uk/docs/support/guide-to-nationalgrid.pdf">this</a>, 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.</p>
<p>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 <a target="_blank" href="https://plus.google.com/u/0/106796523921760669001/posts/WP4cL3my6Ck">this Google+ thread</a>, here I’m going to focus on using R. <a target="_blank" href="https://cran.r-project.org/">R is an open source</a> analytical programming language, you can find instructions for installing it on <a target="_blank" href="https://scottishsnow.wordpress.com/software/">my software page</a>.</p>
<p>There’s no need to write a script from scratch to convert grid references, <a target="_blank" href="https://github.com/cvitolo/rnrfa">someone has done it already</a>! 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:</p>
<pre>
# -------------------------------------
# Install and load the rnrfa package

install.packages(&quot;rnrfa&quot;)
library(rnrfa)

# -------------------------------------
# Read your csv file

df = read.csv(&quot;~/dir/dir/points.csv&quot;, stringsAsFactors=F)

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

# If required, remove spaces in the NGR
df$NGR = gsub(&quot; &quot;, &quot;&quot;, 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, &quot;~/dir/dir/points_clean.csv&quot;, row.names=F)
</pre>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/scottishsnow.wordpress.com/986/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/scottishsnow.wordpress.com/986/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/converting-british-national-grid-references/"></div></div><div><a data-url="https://www.r-bloggers.com/converting-british-national-grid-references/" data-counturl="https://www.r-bloggers.com/converting-british-national-grid-references/" data-count="vertical" data-text="Converting (British) National Grid references" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://scottishsnow.wordpress.com/2016/10/13/converting-bng/"> R – scottishsnow</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Lc-E50UgqEc"></a></div>