<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/gb-postcode-polygons-open-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2018/03/28/gb-postcode-polygons-open-data/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div data-shortcode="caption" class="wp-caption"><img data-orig-file="https://scottishsnow.files.wordpress.com/2018/03/postcode_overview.png?w=456" data-orig-size="1753,1240" data-medium-file="https://scottishsnow.files.wordpress.com/2018/03/postcode_overview.png?w=456?w=300" data-attachment-id="1563" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2018/03/postcode_overview.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2018/03/28/gb-postcode-polygons-open-data/postcode_overview/" data-large-file="https://scottishsnow.files.wordpress.com/2018/03/postcode_overview.png?w=456?w=620" alt="postcode_overview" data-image-title="postcode_overview" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1563" data-recalc-dims="1">
<p class="wp-caption-text">Postcode district and area polygons available.</p>
</div>
<p>These days I’m working with a lot of data about people. Often surveys, but also administrative data. I usually want to represent these on a map. To preserve anonymity I aggregate dataset before plotting them. This might be at a local authority level, but more often it is via postcode.</p>
<p>In the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom" target="_blank">UK a postcode</a> is used to define a small group of addresses and is of the form EH1 2NG (Edinburgh Castle), where the first half is the postcode district and the first letters (either 1 or 2) from the postcode district is the postcode area.</p>
<p>The <a rel="nofollow" href="https://www.ordnancesurvey.co.uk/" target="_blank">Ordnance Survey</a> release <a rel="nofollow" href="https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html" target="_blank">postcode point data as part of their Open Data offering</a>, but to access <a rel="nofollow" href="https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-with-polygons.html" target="_blank">postcode polygons</a> one either needs to be an academic with an <a rel="nofollow" href="https://digimap.edina.ac.uk/" target="_blank">Edina</a> account or pay a license fee for the Ordnance Survey product. I don’t often need access to individual postcode polygons, often aggregate ones are best. I sometimes download these from web sources, if I can find them, but the creation method isn’t that transparent – nor their licensing.</p>
<p>This post is announcing a new, derived dataset I’ve produced: postcode area and district polygons for GB. These have been created by making voronoi polygons of all GB postcodes and then dissolving boundaries based on postcode area and district. I’ve published this dataset on Figshare:</p>
<h3><strong><a rel="nofollow" href="https://doi.org/10.6084/m9.figshare.6050105.v2" target="_blank">Download postcode polygons</a></strong></h3>
<p>The Figshare site also includes code to produce the polygons, but I’ll repeat it here too. The first code chunk uses <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">R</a> to prepare the postcode point data. The second code chunk uses <a rel="nofollow" href="https://grass.osgeo.org/" target="_blank">GRASS</a> to create the spatial dataset.</p>
<pre>
library(tidyverse)

df = read_csv(&quot;~/GIS/OS/postcode.csv&quot;)

df = df %>%
   mutate(pc_district = str_sub(Postcode, 1, nchar(Postcode)-3)) %>%
   mutate(pc_district = trimws(pc_district)) %>%
   mutate(pc_area = gsub('[0-9]+', '', pc_district)) %>%
   mutate(pc_area = str_sub(pc_area, 1, 2))

write_csv(df, &quot;~/GIS/OS/postcode_extra.csv&quot;)
</pre>
<p>And the GRASS commands used:</p>
<pre>
# GRASS commands for making postcode areas

v.voronoi input=postcodes@postcode output=pc_voronoi@postcode

v.dissolve input=pc_voronoi@postcode column=str_9 output=pc_area
v.dissolve input=pc_voronoi@postcode column=str_8 output=pc_district

v.overlay ainput=pc_area@postcode binput=GB_coast@postcode operator=and output=pc_area_clip
v.overlay ainput=pc_district@postcode binput=GB_coast@postcode operator=and output=pc_district_clip

v.db.renamecolumn map=pc_district_clip@postcode column=a_str_8,pc_district
v.db.renamecolumn map=pc_area_clip@postcode column=a_str_9,pc_area

db.dropcolumn -f table=pc_area_clip column=a_cat
db.dropcolumn -f table=pc_area_clip column=b_cat
db.dropcolumn -f table=pc_area_clip column=b_ID
db.dropcolumn -f table=pc_area_clip column=b_AREA
db.dropcolumn -f table=pc_area_clip column=b_PERIMETER
db.dropcolumn -f table=pc_area_clip column=b_ACRES
db.dropcolumn -f table=pc_district_clip column=a_cat
db.dropcolumn -f table=pc_district_clip column=b_cat
db.dropcolumn -f table=pc_district_clip column=b_ID
db.dropcolumn -f table=pc_district_clip column=b_AREA
db.dropcolumn -f table=pc_district_clip column=b_PERIMETER
db.dropcolumn -f table=pc_district_clip column=b_ACRES<span>﻿</span>
</pre>
	<p>
<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/2018/03/28/gb-postcode-polygons-open-data/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/dmTMdybDtH8" height="1"></a></div>