knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE )
The geofi package provides tools for geocoding and reverse geocoding Finnish place names, street addresses, and geographic coordinates using the National Land Survey of Finland (NLS) geocoding REST API. This vignette demonstrates how to use the geocode()
and geocode_reverse()
functions to:
These functions are designed for researchers, analysts, and developers working with spatial data in Finland, offering robust error handling and integration with the sf package for spatial data manipulation.
To use the NLS geocoding API, you need an API key from the National Land Survey of Finland. Follow these steps:
Install the geofi
package from GitHub (or CRAN, if available):
# Install from GitHub devtools::install_github("rOpenGov/geofi") # Install from CRAN install.packages("geofi")
Load the package and required dependencies:
library(geofi) library(sf) library(ggplot2)
Set your API key using the options()
function. Replace "your_api_key_here
" with your actual API key:
options(geofi_mml_api_key = "your_api_key_here")
Alternatively, you can pass the API key directly to the api_key
parameter in each function call, but setting it globally is more convenient.
geocode()
The geocode()
function converts place names or street addresses into spatial coordinates, returning an sf
object with point geometries. It supports multiple data sources (e.g., geographic names, addresses) and output coordinate reference systems (CRS: EPSG:3067 or EPSG:4326).
Let's geocode the place name "Suomenlinna," a famous sea fortress in Helsinki, using the geographic-names
source:
suomenlinna <- geocode( search_string = "Suomenlinna", source = "geographic-names", crs = 4326 ) print(suomenlinna)
This returns an sf
object with the coordinates of Suomenlinna in EPSG:4326 (WGS84). If no results are found, an empty sf
object is returned with a warning.
Geocode a specific street address, "Mannerheimintie 100, Helsinki" with a limit of 5 results:
address <- geocode( search_string = "Mannerheimintie 100, Helsinki", source = "addresses", crs = 3067, size = 5 ) print(address)
This returns an sf
object in EPSG:3067 (ETRS-TM35FIN), suitable for Finnish spatial data analysis.
Visualize the geocoded address on a map using ggplot2
:
ggplot(data = address) + geom_sf(color = "blue", size = 3) + labs( title = "Geocoded Location: Mannerheimintie 100, Helsinki", subtitle = "CRS: ETRS-TM35FIN (EPSG:3067)" ) + theme_minimal()
You can refine the geocoding results using additional parameters:
lang
: Set the response language ("fi
", "sv
", or "en
").options
: Pass custom API options, such as focusing the search near a specific point:focused <- geocode( search_string = "Helsinki", source = "geographic-names", options = "focus.point.lat=60.1699&focus.point.lon=24.9384" )
The geocode_reverse()
function converts geographic coordinates (as sf
POINT objects in EPSG:4326) into place names or addresses. It supports multiple points, customizable search radii, and different output formats (sf
or JSON).
Reverse geocode the coordinates of Parliament House, Helsinki (approximately 60.1725°N, 24.933333°E):
# Create an sf point parliament_point <- data.frame(lon = 24.933333, lat = 60.1725) |> sf::st_as_sf(coords = c("lon", "lat"), crs = 4326) # Reverse geocode places <- geocode_reverse( point = parliament_point, sources = "geographic-names" ) print(places)
This returns an sf
object with the place name and coordinates in EPSG:4326.
For debugging or custom processing, return raw JSON responses:
json_results <- geocode_reverse( point = parliament_point, boundary_circle_radius = 1, return = "json" ) print(json_results)
Plot the reverse geocoded results alongside the input points:
ggplot() + geom_sf(data = parliament_point, color = "red", size = 3, shape = 17) + geom_sf(data = places, color = "blue", size = 3) + labs( title = "Reverse Geocoded Location: Eduskuntatalo", subtitle = "Red: Input Point, Blue: Geocoded Result (EPSG:4326)" ) + theme_minimal()
size
and boundary_circle_radius
to refine results.geofi
Package Repository: https://github.com/rOpenGov/geofiThe geofi
package simplifies geocoding and reverse geocoding for Finnish spatial data, leveraging the NLS geocoding API. With geocode()
and geocode_reverse()
, users can seamlessly convert between textual locations and coordinates, enabling applications in urban planning, geographic analysis, and more. Try the examples above with your own data, and explore the package's flexibility to suit your needs.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.