<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/osm-nominatim-with-r-getting-locations-geo-coordinates-by-its-address/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/osm-nominatim-with-r-getting-locations-geo-coordinates-by-its-address/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It is quite likely to get address info when scraping data from the web, but not geo-coordinates which may be required for further analysis like clustering. Thus <code>geocoding</code> is often needed to get a location’s coordinates by its address. </p>
<p>There are several options, including one of the most popular, <a rel="nofollow" href="https://developers.google.com/maps/documentation/geocoding" target="_blank">google geocoding API</a>. This option can be easily implemented into R with the function <a rel="nofollow" href="https://www.rdocumentation.org/packages/ggmap/versions/2.6.1/topics/geocode?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">geocode</a> from the library <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggmap/ggmap.pdf" target="_blank">ggmap</a>. It has the limitation of 2500 request a day (when it’s used free of charge), see details <a rel="nofollow" href="https://developers.google.com/maps/documentation/geocoding/usage-limits" target="_blank">here</a>. </p>
<p>To increase the number of free of charge geocoding requests, <a rel="nofollow" href="http://wiki.openstreetmap.org/wiki/Nominatim" target="_blank">OpenStreetMap (OSM) Nominatim API</a> can be used. OSM allows up to 1 request per second (see <a rel="nofollow" href="https://operations.osmfoundation.org/policies/nominatim/" target="_blank">the usage policy</a>), that gives about 35 times more API calls compared to the google geocoding API.</p>
<p>Here is one of the ways on how to implement OSM nominatim API in R:</p>
<pre>
## geocoding function using OSM Nominatim API
## details: http://wiki.openstreetmap.org/wiki/Nominatim
## made by: D.Kisler 

nominatim_osm &lt;- function(address = NULL)
{
  if(suppressWarnings(is.null(address)))
    return(data.frame())
  tryCatch(
    d &lt;- jsonlite::fromJSON( 
      gsub('\\@addr\\@', gsub('\\s+', '\\%20', address), 
           'http://nominatim.openstreetmap.org/search/@addr@?format=json&amp;addressdetails=0&amp;limit=1')
    ), error = function(c) return(data.frame())
  )
  if(length(d) == 0) return(data.frame())
  return(data.frame(lon = as.numeric(d$lon), lat = as.numeric(d$lat)))
}
</pre>
<p>The function requires the library <a rel="nofollow" href="https://cran.r-project.org/web/packages/jsonlite/index.html" target="_blank">jsonlite</a>. </p>
<p>Function input: the location address as string.<br>
Function output: a data.frame with lon (longitude) and lat (latitude) of the input location, or empty data.frame if no/invalid address provided as the function input.</p>
<p>Let’s test the function.</p>
<pre>
#dplyr will be used to stack lists together into a data.frame and to get the pipe operator '%>%'
suppressPackageStartupMessages(library(dplyr))
#input addresses
addresses &lt;- c(&quot;Baker Street 221b, London&quot;, &quot;Brandenburger Tor, Berlin&quot;, 
               &quot;Platz der Deutschen Einheit 1, Hamburg&quot;, &quot;Arc de Triomphe de l’Etoile, Paris&quot;,
               &quot;Дворцовая пл., Санкт-Петербург, Россия&quot;)
#
d &lt;- suppressWarnings(lapply(addresses, function(address) {
  #set the elapsed time counter to 0
  t &lt;- Sys.time()
  #calling the nominatim OSM API
  api_output &lt;- nominatim_osm(address)
  #get the elapsed time
  t &lt;- difftime(Sys.time(), t, 'secs')
  #return data.frame with the input address, output of the nominatim_osm function and elapsed time
  return(data.frame(address = address, api_output, elapsed_time = t))
  }) %>%
#stack the list output into data.frame
bind_rows() %>% data.frame())
#output the data.frame content into console
d 
<em> address lon lat elapsed_time
1 Baker Street 221b, London -0.1584945 51.52376 0.2216313 secs
2 Brandenburger Tor, Berlin 13.3777025 52.51628 0.1038268 secs
3 Platz der Deutschen Einheit 1, Hamburg 9.9842058 53.54129 0.1253307 secs
4 Arc de Triomphe de l’Etoile, Paris 2.2950372 48.87378 0.1097755 secs
5 Дворцовая пл., Санкт-Петербург, Россия 30.3151066 59.93952 0.1000750 secs
</em>
</pre>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/spark-rdds-vs-dataframes-vs-sparksql-part-3-web-server-log-analysis/" title="Permanent Link toSpark RDDs Vs DataFrames vs SparkSQL – Part 3 : Web Server Log Analysis" target="_blank">Spark RDDs Vs DataFrames vs SparkSQL – Part 3 : Web Server Log Analysis</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/faster-than-excel-painlessly-merge-data-into-actuarial-loss-development-triangles-with-r/" title="Permanent Link toPainlessly Merge Data into Actuarial Loss Development Triangles with R" target="_blank">Painlessly Merge Data into Actuarial Loss Development Triangles with R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/the-mcomp-package/" title="Permanent Link toThe Mcomp Package" target="_blank">The Mcomp Package</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/building-a-hacker-news-scraper-with-8-lines-of-r-code-using-rvest-library/" title="Permanent Link toBuilding a Hacker News scraper with 8 lines of R code using rvest library" target="_blank">Building a Hacker News scraper with 8 lines of R code using rvest library</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/building-a-telecom-dictionary-scraping-web-using-rvest-in-r/" title="Permanent Link toBuilding a Telecom Dictionary scraping web using rvest in R" target="_blank">Building a Telecom Dictionary scraping web using rvest in R</a></li>
</ol>
	<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://datascienceplus.com/osm-nominatim-with-r-getting-locations-geo-coordinates-by-its-address/"> R Programming – DataScience+</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/GSHn_pEuDts" height="1"></a></div>