Geocoding is the process of retrieving geographic coordinates based on text, such as an address or the name of a place (Wikipedia page). On the other hand, reverse geocoding is the process of retrieving the name and address from geographic coordinates (Wikipedia page).

geocode(
  place,
  as_coordinates = FALSE,
  only_netherlands = TRUE,
  api_key = read_secret("gis.api_key"),
  api_requests_per_second = 1
)

reverse_geocode(
  sf_data,
  api_key = read_secret("gis.api_key"),
  api_requests_per_second = 1
)

Source

Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright

Arguments

place

a (vector of) names or addresses of places

as_coordinates

a logical to indicate whether the result should be returned as coordinates (i.e., class sfc_POINT)

only_netherlands

a logical to indicate whether only Dutch places should be searched

api_key

free API key created at https://geocode.maps.co

api_requests_per_second

number of requests per second

sf_data

an 'sf' object or an 'sfc' object (i.e., a vector with geometric sfc_POINTs). Can also be a character vector, in which case geocode() will be called first.

Details

These functions use OpenStreetMap (OSM), by using the API of https://geocode.maps.co.

geocode() provides geocoding and returns an 'sf' data.frame at default. In case of multiple results, the distance from the main Certe building in Groningen is leading.

reverse_geocode() provides reversed geocoding and returns a data.frame with the columns "name", "address", "zipcode" and "city".

For both functions, the https://geocode.maps.co API will only be called on unique input values, to increase speed.

Examples

if (FALSE) {

# geocoding: retrieve 'sf' data.frame based on place names
coord <- geocode("Van Swietenlaan 2, Groningen")
coord

# reverse geocoding: get the name and address
reverse_geocode(coord)

# places can be any text, and the results are prioritised based on
# the distance from the main Certe building, so:
reverse_geocode(c("Certe", "IKEA"))

hospitals <- geocode(c("Martini ziekenhuis",
                       "Medisch Centrum Leeuwarden",
                       "Tjongerschans Heerenveen",
                       "Scheper Emmen"))
hospitals

if (require("certeplot2")) {
  geo_gemeenten |>
    crop_certe() |>
    plot2(datalabels = FALSE) |>
    add_sf(hospitals, colour = "certeroze", datalabels = place)
}

}