knitr::opts_chunk$set(
  collapse = TRUE,
  comment = " "
)
library(leaflet)
library(sf)
library(nlgeocoder)

nl_geocode provides a quick and easy way to geocode labels of locations in The Netherlands. It will return points or centroids with metadata, but no polylines or polygons. nl_geocode uses the pdok webservice for geo location and is designed to be similar to ggmap: for each (address) label the most probable location (i.e. according to the pdok service) will be returned.

Geocode addresses

By default, nl_geocode will search for addresses.

library(nlgeocoder)

res <- nl_geocode("Martinikerkhof 3 Groningen")
print(res["weergavenaam"])

For addresses a lot of information is available:

colnames(res)

You can also search for multiple addresses at once.

locations <- c("Martinikerkhof 3", "st jansstr 4 groningen", "9726AE 4", "9711 ME 1")
res <- nl_geocode(locations)
data.frame(query = locations, result = res$weergavenaam)

As you can see, the address doesn't need to be an exact match. Uppercases are ignored.

Search for different type of locations

Besides addresses, you can also search for places, municipalities, provinces, roads and many more. See the LocatieServer API documentation complete list of types you can choose from.

You can restrict the output of nl_geocode to municipalities only:

res <- nl_geocode("Groningen", type = "gemeente") 
res[c("weergavenaam","type")] 

You can also restrict the output to places:

res <- nl_geocode("Groningen", type = "woonplaats") 
res$weergavenaam
res$type

If you are only interested in results in a particular area (for instance a province or municipality) you can use the fq parameter.

res <- nl_geocode("Hoofdstraat", fq = "provincienaam:Groningen") 
res$weergavenaam
res$provincienaam

Return coordinates in a spatial dataframe with CRS WGS84 or RD_New

By default, nl_geocode will return a sf object with coordinates in the coordinate reference system WGS84 (EPSG:4326).

res <- nl_geocode("Martinikerkhof 3 Groningen") 
st_crs(res)["epsg"]

It also possible to specify that the coordinates should be in RD_New (EPSG:28992), the Dutch coordinate reference system.

res <- nl_geocode("Martinikerkhof 3 Groningen", output = "rd") 
st_crs(res)["epsg"]

You can also use the nl_geocode_rd function instead:

res <- nl_geocode_rd("Martinikerkhof 3 Groningen") 
st_crs(res)["epsg"]

Return coordinates in a dataframe

You can use the parameter output to indicate that you only want the labels, and not the geometry.

res <- nl_geocode("Martinikerkhof 3 Groningen", output = "data.frame") 
class(res)

Plot the output on a map

Plotting the output on a map, because by default nl_geocode returns an sf object.

data("addresses")
knitr::kable(addresses)
library(leaflet)
poi_geocoded <- nl_geocode(addresses$Address)

leaflet(width = "100%", height=600) %>% 
  addPdokTiles("gray") %>% 
  addCircleMarkers( data = poi_geocoded
                  , popup = paste("<b>",addresses$POI, "</b><br/>", poi_geocoded$weergavenaam)
                  )


uRos2018/nlgeocoder documentation built on May 29, 2019, 9:16 a.m.