<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/openrouteservice-geodata/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aghaynes.wordpress.com/2018/05/19/openrouteservice-geodata/"> R – Insights of a PhD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <a rel="nofollow" href="https://openrouteservice.org/" target="_blank">openrouteservice </a>provides a new method to get geodata into R. It has an API (or a set of them) and an R package has been written to communicate with said API(s) and is available from <a rel="nofollow" href="https://github.com/GIScience/openrouteservice-r" target="_blank">GitHub</a>. I’ve just been playing around with the examples on this <a rel="nofollow" href="https://giscience.github.io/openrouteservice-r/articles/openrouteservice.html" target="_blank">page</a>, in the thought of using it for a project (more on that later if I get anywhere with it).</p>
<p>Anyways…onto the code…which is primarily a modification from the examples page I mentioned earlier (see that page for more examples).</p>
<pre>devtools::install_github(&quot;GIScience/openrouteservice-r&quot;)</pre>
<p>Load some libraries</p>
<pre>library(openrouteservice)
library(leaflet)</pre>
<p>Set the API key</p>
<pre>ors_api_key(&quot;your-key-here&quot;)</pre>
<p>Locations of interest and send the request to the API asking for the region that is accessible within a 15 minute drive of the coordinates.</p>
<pre>coordinates &lt;- list(c(8.55, 47.23424), c(8.34234, 47.23424), c(8.44, 47.4))

x &lt;- ors_isochrones(coordinates, 
 range = 60*15, # maximum time to travel (15 mins)
 interval = 60*15, # results in bands of 60*15 seconds (15 mins)
 intersections=FALSE) # no intersection of polygons</pre>
<p>By changing the interval to, say, 60*5, three regions per coordinate are returned representing regions accessible within 5, 10 and 15 minutes drive. Changing the intersections argument would produce a separate polygon for any overlapping regions. The information of the intersected polygons is limited though, so it might be better to do the intersection with other tools afterwards.</p>
<p>The results can be plotted with leaflet…</p>
<pre>leaflet() %>%
 addTiles() %>%
 addGeoJSON(x) %>%
 fitBBox(x$bbox)</pre>
<p>The blue regions are the three regions accessible within 15 minutes. A few overlapping regions are evident, each of which would be saved to a unique polygon had we set intersections to TRUE.</p>
<p><img data-orig-file="https://aghaynes.files.wordpress.com/2018/05/isochrone.png?w=456" data-orig-size="535,408" data-medium-file="https://aghaynes.files.wordpress.com/2018/05/isochrone.png?w=456?w=300" data-attachment-id="354" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2018/05/isochrone.png?w=456" data-permalink="https://aghaynes.wordpress.com/2018/05/19/openrouteservice-geodata/isochrone/" data-large-file="https://aghaynes.files.wordpress.com/2018/05/isochrone.png?w=456?w=497" alt="isochrone" data-image-title="isochrone" sizes="(max-width: 497px) 100vw, 497px" data-comments-opened="1" class="wp-image-354" data-recalc-dims="1"></p>
<p>The results from the API come down in a GeoJSON format which is given a class of, in this case ors_isochrones, which isn’t recognized by so many formats so you might want to convert it to an sp object, giving access to all of the tools for those formats. That’s easy enough to do via the geojsonio package…</p>
<pre>library(geojsonio)
class(x) &lt;- &quot;geo_list&quot;
y &lt;- geojson_sp(x)

library(sp)
plot(y)</pre>
<p>You can also derive coordinates from (partial) addresses. Here is an example for a region of Bern in Switzerland, using the postcode.</p>
<pre>coord &lt;- ors_geocode(&quot;3012, Switzerland&quot;)</pre>
<p>This resulted in 10 hits, the first of which was correct…the others were in different countries…</p>
<pre>unlist(lapply(coord$features, function(x) x$properties$label))</pre>
<pre><span>[1] &quot;3012, Bern, Switzerland&quot; [2] &quot;A1, Bern, Switzerland&quot; [3] &quot;Bremgartenstrasse, Bern, Switzerland&quot; [4] &quot;131 Bremgartenstrasse, Bern, Switzerland&quot; [5] &quot;Briefeinwurf Bern, Gymnasium Neufeld, Bern, Switzerland&quot;
[6] &quot;119 Bremgartenstrasse, Bern, Switzerland&quot; [7] &quot;Gym Neufeld, Bern, Switzerland&quot; [8] &quot;131b Bremgartenstrasse, Bern, Switzerland&quot; [9] &quot;Gebäude Nord, Bern, Switzerland&quot; [10] &quot;113 Bremgartenstrasse, Bern, Switzerland&quot;</span><span>
</span></pre>
<p>The opposite (coordinate to address) is also possible, again returning multiple hits…</p>
<pre><span>address &lt;-ors_geocode(location = c(7.425898, 46.961598))
unlist(lapply(address$features, function(x) x$properties$label))
[1] &quot;3012, Bern, Switzerland&quot; [2] &quot;A1, Bern, Switzerland&quot; [3] &quot;Bremgartenstrasse, Bern, Switzerland&quot; [4] &quot;131 Bremgartenstrasse, Bern, Switzerland&quot; [5] &quot;Briefeinwurf Bern, Gymnasium Neufeld, Bern, Switzerland&quot; [6] &quot;119 Bremgartenstrasse, Bern, Switzerland&quot; [7] &quot;Gym Neufeld, Bern, Switzerland&quot; [8] &quot;131b Bremgartenstrasse, Bern, Switzerland&quot; [9] &quot;Gebäude Nord, Bern, Switzerland&quot; [10] &quot;113 Bremgartenstrasse, Bern, Switzerland&quot; </span></pre>
<p>Other options are distances/times/directions between points and places of interest (POI) near a point or within a region.</p>
<p>Hope that helps someone! Enjoy!</p>
<p> </p>
<p> </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://aghaynes.wordpress.com/2018/05/19/openrouteservice-geodata/"> R – Insights of a PhD</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/kq2Jt7GYoNE" height="1"></a></div>