<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/spatial-networks-case-study-st-james-centre-edinburgh-23/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2017/11/13/spatial-networks-stjames-2/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is part two in a series I’m writing on network analysis. The first part is <a rel="nofollow" href="https://scottishsnow.wordpress.com/2017/10/08/spatial-networks-stjames-1/" target="_blank">here</a>. In this section I’m going to cover allocating resources, again using the St James’ development in Edinburgh as an example. Most excitingly (for me), the end of this post covers the impact of changes in resource allocation.</p>
<p>Edinburgh (and surrounds) has more than one shopping centre. Many more. I’ve had a stab at narrowing these down to those that are similar to the St James centre, i.e. they’re big, (generally) covered and may have a cinema. You can see a plot of these below. As you can see the majority are concentrated around the population centre of Edinburgh.</p>
<div data-shortcode="caption" class="wp-caption"><img data-orig-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_centres.png?w=456" data-orig-size="1753,1240" data-medium-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_centres.png?w=456?w=300" data-attachment-id="1421" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2017/11/shopping_centres.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2017/11/13/spatial-networks-stjames-2/shopping_centres/" data-large-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_centres.png?w=456?w=620" alt="shopping_centres" data-image-title="shopping_centres" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1421" data-recalc-dims="1">
<p class="wp-caption-text">Location of big shopping centres in and around Edinburgh.</p>
</div>
<p>As with the previous post I’ve used <a rel="nofollow" href="https://grass.osgeo.org/" target="_blank">GRASS GIS</a> for the network analysis, <a rel="nofollow" href="http://qgis.org/" target="_blank">QGIS</a> for cartography and <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">R</a> for some subsequent analysis. I’ve used the Ordnance Survey <a rel="nofollow" href="https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html" target="_blank">code-point open</a> and <a rel="nofollow" href="https://www.ordnancesurvey.co.uk/business-and-government/products/os-open-roads.html" target="_blank">openroads</a> datasets for the analysis and various Ordnance Survey maps for the background.</p>
<p>An allocation map shows how you can split your network to be serviced by different resource centres. I like to think of it as deciding which fire station sends an engine to which road. But this can be extended to any resource with multiple locations: bank branches, libraries, schools, swimming pools. In this case we’re using shopping centres. As always the <a rel="nofollow" href="https://grass.osgeo.org/grass72/manuals/v.net.alloc.html" target="_blank">GRASS manual page</a> contains a full walk through of how to run the analysis. I’ll repeat the steps I took below:</p>
<pre>
# connect points to network
v.net roads_EH points=shopping_centres out=centres_net op=connect thresh=200

# allocate, specifying range of center cats (easier to catch all):
v.net.alloc centres_net out=centres_alloc center_cats=1-100000 node_layer=2

# Create db table
v.db.addtable map=centres_alloc@shopping_centres
# Join allocation and centre tables
v.db.join map=centres_alloc column=cat other_table=shopping_centres other_column=cat

# Write to shp
v.out.ogr -s input=centres_alloc output=shopping_alloc format=ESRI_Shapefile output_layer=shopping_alloc
</pre>
<p>The last step isn’t strictly necessary, as QGIS and R can connect directly to the GRASS database, but old habits die hard! We’ve now got a copy of the road network where all roads are tagged with which shopping centre they’re closest too. We can see this below:</p>
<div data-shortcode="caption" class="wp-caption"><img data-orig-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_allocation.png?w=456" data-orig-size="1753,1240" data-medium-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_allocation.png?w=456?w=300" data-attachment-id="1420" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2017/11/shopping_allocation.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2017/11/13/spatial-networks-stjames-2/shopping_allocation/" data-large-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_allocation.png?w=456?w=620" alt="shopping_allocation" data-image-title="shopping_allocation" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1420" data-recalc-dims="1">
<p class="wp-caption-text">Allocation network of EH shopping centres.</p>
</div>
<p>A few things stand out for me:</p>
<ul>
<li>Ocean terminal is a massive centre but is closest to few people.</li>
<li>Some of the postcodes closest to St James, as really far away.</li>
<li>The split between Fort Kinnaird and St James is really stark just east of the A702.</li>
</ul>
<p>If I was a councillor and I coordinated shopping centres in a car free world, I now know where I’d be lobbying for better public transport!</p>
<p>We can also do a similar analysis using the shortest path, as in the previous post. Instead of looking for the shortest path to a single point, we can get GRASS to calculate the distance from each postcode to its nearest shopping centre (note this is using the postcodes_EH file from the previous post):</p>
<pre>
# connect postcodes to streets as layer 2
v.net --overwrite input=roads_EH points=postcodes_EH output=roads_net1 operation=connect thresh=400 arc_layer=1 node_layer=2

# connect shops to streets as layer 3
v.net --overwrite input=roads_net1 points=shopping_centres output=roads_net2 operation=connect thresh=400 arc_layer=1 node_layer=3

# inspect the result
v.category in=roads_net2 op=report

# shortest paths from postcodes (points in layer 2) to nearest stations (points in layer 3)
v.net.distance --overwrite in=roads_net2 out=pc_2_shops flayer=2 to_layer=3

# Join postcode and distance tables
v.db.join map=postcodes_EH column=cat other_table=pc_2_shops other_column=cat
# Join station and distance tables
v.db.join map=postcodes_EH column=tcat other_table=shopping_centres other_column=cat subset_columns=Centre

# Make a km column
# Really short field name so we can output to shp
v.db.addcolumn map=postcodes_EH columns=&quot;dist_al_km double precision&quot;
v.db.update map=postcodes_EH column=dist_al_km qcol=&quot;dist/1000&quot;

# Make a st james vs column
# Uses results from the previous blog post
v.db.addcolumn map=postcodes_EH columns=&quot;diff_km double precision&quot;
v.db.update map=postcodes_EH column=diff_km qcol=&quot;dist_km-dist_al_km&quot;

# Write to shp
v.out.ogr -s input=postcodes_EH output=pc_2_shops format=ESRI_Shapefile output_layer=pc_2_shops
</pre>
<p>Again we can plot these up in QGIS (below). These are really similar results to the road allocation previously, but give us a little more detail on where the population are as each postcode is show. However, the eagle eyed of you will have noticed we pulled out the distance for each postcode in the code above and then compared it to the distance to St James alone. We can use this for considering the impact of resource allocation.</p>
<div data-shortcode="caption" class="wp-caption"><img data-orig-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_nearest.png?w=456" data-orig-size="1753,1240" data-medium-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_nearest.png?w=456?w=300" data-attachment-id="1422" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2017/11/shopping_nearest.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2017/11/13/spatial-networks-stjames-2/shopping_nearest/" data-large-file="https://scottishsnow.files.wordpress.com/2017/11/shopping_nearest.png?w=456?w=620" alt="shopping_nearest" data-image-title="shopping_nearest" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1422" data-recalc-dims="1">
<p class="wp-caption-text">Closest shopping centre for each EH postcode.</p>
</div>
<p>Switching to R, we can interrogate the postcode data further. Using R’s gdal library we can read in the shp file and generate some summary statistics:</p>
<table>
<tbody>
<tr>
<td><strong>Centre</strong></td>
<td><strong>No. of postcodes closest<br>
</strong></td>
</tr>
<tr>
<td>Almondvale</td>
<td>4361</td>
</tr>
<tr>
<td>Fort Kinnaird</td>
<td>7813</td>
</tr>
<tr>
<td>Gyle</td>
<td>3437</td>
</tr>
<tr>
<td>Ocean terminal</td>
<td>1321</td>
</tr>
<tr>
<td>St James</td>
<td>7088</td>
</tr>
</tbody>
</table>
<pre>
# Package
install.packages(&quot;rgdal&quot;)
library(rgdal)

# Read file
postcodes = readOGR(&quot;/home/user/dir/dir/network/data/pc_2_shops.shp&quot;)

# How many postcodes for each centre?
table(postcodes$Centre)
</pre>
<p>We can also look at the distribution of distances for each shopping centre using a box and whisker plot. As in the map we can see that Fort Kinnaird and St James are closest to the most distant postcodes, and that Ocean terminal has a small geographical catchment. The code for this plot is a the end of this post.<img data-orig-file="https://scottishsnow.files.wordpress.com/2017/11/all-shops_distance_boxplot.png?w=456" data-orig-size="800,600" data-medium-file="https://scottishsnow.files.wordpress.com/2017/11/all-shops_distance_boxplot.png?w=456?w=300" data-attachment-id="1418" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2017/11/all-shops_distance_boxplot.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2017/11/13/spatial-networks-stjames-2/all-shops_distance_boxplot/" data-large-file="https://scottishsnow.files.wordpress.com/2017/11/all-shops_distance_boxplot.png?w=456?w=620" alt="all-shops_distance_boxplot" data-image-title="all-shops_distance_boxplot" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1418" data-recalc-dims="1"></p>
<p>We can also repeat the plot from the previous blog post and look at how many postcodes are within walking and cycling distance of their nearest centre. In the previous post I showed the solid line and circle points for the St James centre. We can now compare those results to the impact of people travelling to their closest centre (below). The number of postcodes within walking distance of their nearest centre is nearly double that of St James alone, and <strong>those within cycling distance rises to nearly 50%!</strong> Code at the end of the post.</p>
<p><img data-orig-file="https://scottishsnow.files.wordpress.com/2017/11/all-shops_postcode-distance.png?w=456" data-orig-size="800,600" data-medium-file="https://scottishsnow.files.wordpress.com/2017/11/all-shops_postcode-distance.png?w=456?w=300" data-attachment-id="1419" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2017/11/all-shops_postcode-distance.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2017/11/13/spatial-networks-stjames-2/all-shops_postcode-distance/" data-large-file="https://scottishsnow.files.wordpress.com/2017/11/all-shops_postcode-distance.png?w=456?w=620" alt="all-shops_postcode-distance" data-image-title="all-shops_postcode-distance" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1419" data-recalc-dims="1"></p>
<p>We also now have two curves on the above plot, and the area between them is the distance saved if each postcode travelled to its closest shopping centre instead of the St James.</p>
<h3><strong>The total distance is a whopping 123,680 km!</strong></h3>
<p>This impact analysis is obviously of real use in these times of reduced public services. My local council, Midlothian, is considering closing all its libraries bar one. What impact would this have on users? How would the road network around the kept library cope? Why have they just been building new libraries? It’s also analysis I really hope the DWP undertook before <a rel="nofollow" href="https://stv.tv/news/west-central/1375030-half-of-glasgow-job-centres-to-close-under-uk-government-plans/" target="_blank">closing job centres across Glasgow</a>. Hopefully the work of this post helps people investigate these impacts themselves.</p>
<pre>
# distance saved
# NA value is one postcode too far to be joined to road - oops!
sum(postcodes$diff_km, na.rm=T)

# Boxplot
png(&quot;~/dir/dir/network/figures/all-shops_distance_boxplot.png&quot;, height=600, width=800)
par(cex=1.5)
boxplot(dist_al_km ~ Centre, postcodes, lwd=2, range=0,
        main=&quot;Box and whiskers of EH postcodes to their nearest shopping centre&quot;,
        ylab=&quot;Distance (km)&quot;)
dev.off()

# Line plot
# Turn into percentage instead of postcode counts
x = sort(postcodes$dist_km)
x = quantile(x, seq(0, 1, by=0.01))
y = sort(postcodes$dist_al_km)
y = quantile(y, seq(0, 1, by=0.01))

png(&quot;~/dir/dir/network/figures/all-shops_postcode-distance.png&quot;, height=600, width=800)
par(cex=1.5)
plot(x, type=&quot;l&quot;,
     main=&quot;EH postcode: shortest road distances to EH shopping centres&quot;,
     xlab=&quot;Percentage of postcodes&quot;,
     ylab=&quot;Distance (km)&quot;,
     lwd=3)
lines(y, lty=2, lwd=3)

points(max(which(x&lt;2)), 2, pch=19, cex=2, col=&quot;purple4&quot;)
points(max(which(x&lt;5)), 5, pch=19, cex=2, col=&quot;darkorange&quot;)
points(max(which(y&lt;2)), 2, pch=18, cex=2.5, col=&quot;purple4&quot;)
points(max(which(y&lt;5)), 5, pch=18, cex=2.5, col=&quot;darkorange&quot;)
legend(&quot;topleft&quot;,
       c(&quot;St James&quot;,
         &quot;Nearest centre&quot;,
         paste0(max(which(x&lt;2)),
               &quot;% postcodes within 2 km (walking) of St James&quot;),
         paste0(max(which(x&lt;5)),
               &quot;% postcodes within 5 km (cycling) of St James&quot;),
         paste0(max(which(y&lt;2)),
                &quot;% postcodes within 2 km (walking) of nearest centre&quot;),
         paste0(max(which(y&lt;5)),
                &quot;% postcodes within 5 km (cycling) of nearest centre&quot;)),
       col=c(&quot;black&quot;, &quot;black&quot;, &quot;purple4&quot;, &quot;darkorange&quot;, &quot;purple4&quot;, &quot;darkorange&quot;),
       pch=c(NA, NA, 19, 19, 18, 18),
       lwd=c(3),
       lty=c(1, 2, NA, NA, NA, NA),
       pt.cex=c(NA, NA, 2, 2, 2.5, 2.5))
dev.off()
</pre>
<p> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/scottishsnow.wordpress.com/1408/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/scottishsnow.wordpress.com/1408/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<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/2017/11/13/spatial-networks-stjames-2/"> 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/yqWnmkr9Ztw" height="1"></a></div>