Geocode Dutch addresses

Willy Tadema, Edwin de Jonge

2018-10-01

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"])
  Simple feature collection with 1 feature and 1 field
  geometry type:  POINT
  dimension:      XY
  bbox:           xmin: 6.568567 ymin: 53.21927 xmax: 6.568567 ymax: 53.21927
  epsg (SRID):    4326
  proj4string:    +proj=longlat +datum=WGS84 +no_defs
                          weergavenaam              centroide_ll
  1 Martinikerkhof 3, 9712JG Groningen POINT (6.568567 53.21927)

For addresses a lot of information is available:

colnames(res)
   [1] "bron"                   "woonplaatscode"        
   [3] "type"                   "woonplaatsnaam"        
   [5] "wijkcode"               "huis_nlt"              
   [7] "openbareruimtetype"     "buurtnaam"             
   [9] "gemeentecode"           "weergavenaam"          
  [11] "straatnaam_verkort"     "id"                    
  [13] "gekoppeld_perceel"      "gemeentenaam"          
  [15] "buurtcode"              "wijknaam"              
  [17] "identificatie"          "openbareruimte_id"     
  [19] "waterschapsnaam"        "provinciecode"         
  [21] "postcode"               "provincienaam"         
  [23] "nummeraanduiding_id"    "waterschapscode"       
  [25] "adresseerbaarobject_id" "huisnummer"            
  [27] "provincieafkorting"     "centroide_rd"          
  [29] "straatnaam"             "score"                 
  [31] "centroide_ll"

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)
query result
Martinikerkhof 3 Martinikerkhof 3, 9712JG Groningen
st jansstr 4 groningen Sint Jansstraat 4, 9712JN Groningen
9726AE 4 Stationsplein 4, 9726AE Groningen
9711 ME 1 Museumeiland 1, 9711ME Groningen

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")] 
weergavenaam type centroide_ll
Gemeente Groningen gemeente c(6.5835489, 53.22344423)

You can also restrict the output to places:

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

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
  [1] "HOOFDSTRAAT, Finsterwolde"
res$provincienaam
  [1] "Groningen"

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"]
  $epsg
  [1] 4326

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"]
  $epsg
  [1] 28992

You can also use the nl_geocode_rd function instead:

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

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)
  [1] "data.frame"

Plot the output on a map

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

data("addresses")
POI Address
Martinitoren Martinikerkhof 3 Groningen
Provinciehuis st. jansstr 4 groningen
Groninger Museum 9711 ME 1
Centraal Station 9726 AE 4
Groninger Forum Hereplein 73 Groningen
library(leaflet)
poi_geocoded <- nl_geocode(addresses$Address)

leaflet(width = "100%", height=600) %>% 
  addPdokTiles("gray") %>% 
  addCircleMarkers(data = poi_geocoded, popup = ~weergavenaam)