<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/r-with-grass-gis-easiest-munros/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’ve been having a more sustained play with <a target="_blank" rel="nofollow" href="https://www.r-project.org/">R</a> and <a target="_blank" rel="nofollow" href="https://grass.osgeo.org/">GRASS</a> together. I’ve previously used them, in tandem, for <a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2014/08/24/many-rastered-beast/">processing satellite images</a>, but haven’t been much further. In this post I’ll look at finding the nearest roads to mountain summits, querying their elevations and presenting summary statistics.</p>
<p>First off, you can run R in GRASS or GRASS in R. I prefer the former as it avoids RAM issues. To do this fire up GRASS, ignore the GUI (for now) and type <code>R</code> at the command line. You can also run RStudio from R by typing <code>rstudio &amp;</code> instead. Amazing! For a full breakdown, see <a target="_blank" rel="nofollow" href="https://grasswiki.osgeo.org/wiki/R_statistics/rgrass7">this wiki</a>.</p>
<p>For this work I’ve used the Ordnance Survey Terrain50 elevation model and the OpenRoads dataset. Before you can use the OpenRoads data you’ll need to change the column names, <a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/03/24/r-colnames-spatial-dataframe/">I’ve already solved this problem</a>. Shamefully, I downloaded a Munro dataset some years ago and have no idea where from…</p>
<p>You can import your vector and raster data (roads, summits and elevation model) into GRASS using the GUI, this is probably the easiest method. Following this, you’ll need to filter the OpenRoads data to remove “Not Classified” roads, as these are generally not public and usually dirt tracks (769633 of them in GB). Do this with:</p>
<pre>
v.extract input=roads@network where=&quot;class != 'Not Classified'&quot; output=roads_public
</pre>
<p>Note, any call using <code>system</code> can be executed at the GRASS console or in terminal, without R. The above has a lot of quotes, so is easiest run in the console or terminal!</p>
<p>Next we can run the following code for our analysis (the <code>v.out.ascii</code> and following cleaning calls would likely be better in <code>readVECT</code>):</p>
<pre>
library(rgrass7)
library(rgdal)

# Add new columns to vector
system(&quot;v.db.addcolumn map=Munros@munro columns='dist INT,to_x INT,to_y INT'&quot;)

# Get distance and coordinates of nearest road point
system(&quot;v.distance from=Munros@munro from_type=point to=roads_public@munro to_type=line upload=dist,to_x,to_y column=dist,to_x,to_y&quot;)

# Read results into R
Munro = execGRASS(&quot;v.out.ascii&quot;, parameters=list(input=&quot;Munros@munro&quot;, type=&quot;point&quot;, columns=&quot;NAME,HT,dist,to_x,to_y&quot;), intern=T)

# Clean results
Munro = strsplit(Munro, split=&quot;|&quot;, fixed=T)
Munro = do.call(&quot;rbind.data.frame&quot;, Munro)[, -3]
colnames(Munro) = c(&quot;Easting&quot;, &quot;Northing&quot;, &quot;Name&quot;, &quot;Height&quot;, &quot;Road_dist&quot;, &quot;Road_x&quot;, &quot;Road_y&quot;)
Munro[, -3] = apply(Munro[, -3], 2, as.numeric)
Munro$Easting = round(Munro$Easting)
Munro$Northing = round(Munro$Northing)

# Write data to csv
# Awkward, but easiest way to get point file of road locations!
write.csv(Munro, &quot;~/Munro.csv&quot;, row.names=F)

# Make point file of nearest road locations
system(&quot;v.in.ascii --overwrite input=/home/melville/Munro.csv output=temp separator=comma x=6 y=7 skip=1&quot;)

# Interogate dtm at nearest road points
# Annoyingly, this writes to another temp file
system(&quot;r.what --overwrite map=Terrain50@PERMANENT points=temp@munro output=temp.csv separator=comma&quot;)

# Read temp file of road heights and extract elevations
x = read.csv(&quot;~/temp.csv&quot;, header=F)
Munro$Road_height = round(x$V4)

# Calculate some statistics
Munro$Diff = Munro$Height - Munro$Road_height
Munro$Gradient = round(100 * Munro$Diff / Munro$Road_dist)

# Make columns integer to avoid decimal places when exported to a shp file
Munro = Munro[, c(3:5, 8:10)]
for(i in 2:ncol(Munro)){
   class(Munro[, i]) = &quot;integer&quot;
}

# Convert munro summit and nearest road points into lines
x = lapply(1:nrow(Munro), function(i){
   rbind(paste(Munro[i, c(1, 2)], collapse=&quot;,&quot;),
         paste(Munro[i, c(6, 7)], collapse=&quot;,&quot;),
         paste(&quot;NaN,NaN&quot;))
})
x = do.call(&quot;rbind.data.frame&quot;, x)
write.table(x,
            &quot;~/temp.txt&quot;,
            col.names=F,
            row.names=F,
            quote=F)
system(&quot;v.in.lines input=/home/melville/temp.txt output=Munro_lines separator=comma&quot;)

# Read lines from GRASS
l = readVECT(&quot;Munro_lines&quot;)
# Add data frame to lines
l@data = Munro

# Write shp file
writeOGR(l, &quot;.&quot;, &quot;Munro&quot;, &quot;ESRI Shapefile&quot;, overwrite_layer=T)

# Clean up
system(&quot;rm ~/Munro*&quot;)
system(&quot;rm ~/temp*&quot;)
</pre>
<p>Now we’ve a dataset called Munro in our R environment, we can make some summary plots (code below):</p>
<p><a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_height/"><img sizes="(max-width: 150px) 100vw, 150px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_height.png" data-orig-size="480,480" data-image-title="Munro_height" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1143" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_height/" alt="" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_height.png?w=300" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_height.png?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_height.png?w=150&resize=150%2C150#038;h=150"></a><br>
<a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_dist/"><img sizes="(max-width: 150px) 100vw, 150px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_dist.png" data-orig-size="480,480" data-image-title="Munro_dist" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1141" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_dist/" alt="" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_dist.png?w=300" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_dist.png?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_dist.png?w=150&resize=150%2C150#038;h=150"></a><br>
<a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_diff/"><img sizes="(max-width: 150px) 100vw, 150px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_diff.png" data-orig-size="480,480" data-image-title="Munro_diff" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1139" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_diff/" alt="" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_diff.png?w=300" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_diff.png?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_diff.png?w=150&resize=150%2C150#038;h=150"></a><br>
<a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_road_ht/"><img sizes="(max-width: 150px) 100vw, 150px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_road_ht.png" data-orig-size="480,480" data-image-title="Munro_road_ht" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1144" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_road_ht/" alt="" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_road_ht.png?w=300" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_road_ht.png?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_road_ht.png?w=150&resize=150%2C150#038;h=150"></a><br>
<a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_grad/"><img sizes="(max-width: 150px) 100vw, 150px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_grad.png" data-orig-size="480,480" data-image-title="Munro_grad" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1142" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_grad/" alt="" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_grad.png?w=300" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_grad.png?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_grad.png?w=150&resize=150%2C150#038;h=150"></a><br>
<a target="_blank" rel="nofollow" href="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_diff_dist/"><img sizes="(max-width: 150px) 100vw, 150px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_diff_dist.png" data-orig-size="480,480" data-image-title="Munro_diff_dist" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1140" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_diff_dist/" alt="" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_diff_dist.png?w=300" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_diff_dist.png?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_diff_dist.png?w=150&resize=150%2C150#038;h=150"></a></p>
<p>WordPress helpfully tiled these plots for me, but they were created with:</p>
<pre>
png(&quot;~/Cloud/Michael/websites/blog/Munro_height.png&quot;)
hist(Munro$Height, main=&quot;Munro elevation&quot;, xlab=&quot;Elevation (m)&quot;)
dev.off()

png(&quot;~/Cloud/Michael/websites/blog/Munro_dist.png&quot;)
hist(Munro$Road_dist, main=&quot;Munro distance from road&quot;, xlab=&quot;Distance (m)&quot;)
dev.off()

png(&quot;~/Cloud/Michael/websites/blog/Munro_road_ht.png&quot;)
hist(Munro$Road_height, main=&quot;Nearest road elevation&quot;, xlab=&quot;Elevation (m)&quot;)
dev.off()

png(&quot;~/Cloud/Michael/websites/blog/Munro_diff.png&quot;)
hist(Munro$Diff, main=&quot;Difference between Munro and road elevation&quot;, xlab=&quot;Elevation (m)&quot;)
dev.off()

png(&quot;~/Cloud/Michael/websites/blog/Munro_grad.png&quot;)
hist(Munro$Gradient, main=&quot;Average gradient between road and Munro&quot;, xlab=&quot;Gradient (%)&quot;)
dev.off()

png(&quot;~/Cloud/Michael/websites/blog/Munro_diff_dist.png&quot;)
plot(Diff ~ Road_dist, Munro, main=&quot;Elevation difference and distance from road for Munros&quot;, xlab=&quot;Distance to road (m)&quot;, ylab=&quot;Elevation difference (m)&quot;)
dev.off()
</pre>
<p>We can also find the “easiest” hills and those furthest from a road:</p>
<pre>
# Easiest hills
head(Munro[order(Munro$Diff), c(1, 5)])
head(Munro[order(Munro$Gradient), c(1, 6)])

# Furthest from a road
head(Munro[order(Munro$Road_dist, decreasing=T), c(1, 3)])
</pre>
<table>
<tr>
<td> Name </td>
<td> Elev diff (m) </td>
</tr>
<tr>
<td> The Cairnwell </td>
<td> 266 </td>
</tr>
<tr>
<td> Carn Aosda </td>
<td> 308 </td>
</tr>
<tr>
<td> An Socach: W summit </td>
<td> 315 </td>
</tr>
<tr>
<td> Meall a’Choire Leith </td>
<td> 378 </td>
</tr>
<tr>
<td> Tom Buidhe </td>
<td> 430 </td>
</tr>
<tr>
<td> Tolmount </td>
<td> 434 </td>
</tr>
</table>
<table>
<tr>
<td> Name </td>
<td> Distance to road (m) </td>
</tr>
<tr>
<td> Carn an Fhidhleir </td>
<td> 14413 </td>
</tr>
<tr>
<td> An Sgarsoch </td>
<td> 14139 </td>
</tr>
<tr>
<td> Ben Alder </td>
<td> 12844 </td>
</tr>
<tr>
<td> Beinn Bheoil </td>
<td> 12113 </td>
</tr>
<tr>
<td> Beinn Brohtain </td>
<td> 10980 </td>
</tr>
<tr>
<td> Beinn Dearg </td>
<td> 10857 </td>
</tr>
</table>
<p>Finally, I used the Carto plugin for QGIS to upload the dataset to Carto. You can view and interact with it on <strong><a target="_blank" rel="nofollow" href="https://mikerspencer.carto.com/viz/459140d8-1968-11e7-80ad-0e233c30368f/public_map">my Carto map</a></strong>.</p>
<div data-shortcode="caption" class="wp-caption"><img sizes="(max-width: 620px) 100vw, 620px" data-image-description="" data-orig-file="https://scottishsnow.files.wordpress.com/2017/04/munro_to_road.png?w=456" data-orig-size="653,778" data-image-title="munro_to_road" data-comments-opened="1" class="wp-image-1145" data-recalc-dims="1" data-attachment-id="1145" data-permalink="https://scottishsnow.wordpress.com/2017/04/04/r-grass-munros/munro_to_road/" alt="munro_to_road" data-medium-file="https://scottishsnow.files.wordpress.com/2017/04/munro_to_road.png?w=456?w=252" data-large-file="https://scottishsnow.files.wordpress.com/2017/04/munro_to_road.png?w=456?w=620" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://scottishsnow.files.wordpress.com/2017/04/munro_to_road.png?w=456">
<p class="wp-caption-text">View and interact with the live version: <a target="_blank" rel="nofollow" href="https://mikerspencer.carto.com/viz/459140d8-1968-11e7-80ad-0e233c30368f/public_map">https://mikerspencer.carto.com/viz/459140d8-1968-11e7-80ad-0e233c30368f/public_map</a></p>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/scottishsnow.wordpress.com/1091/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/scottishsnow.wordpress.com/1091/" 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>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/04/04/r-grass-munros/"> 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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/4-FVxsz57pY"></a></div>