Accessing Spatial Data from NLS OGC apis with geofi

Markus Kainu

2025-04-28

Introduction

The geofi package provides tools to access spatial data from the Maastotietokanta (Topographic Database) and Geographic Names (Nimistö) datasets provided by the National Land Survey of Finland (NLS) via their OGC API. This vignette demonstrates how to use the package’s core functions to:

The package handles API authentication, pagination, spatial filtering, and coordinate reference system (CRS) transformations, making it easy to work with Finnish spatial data in R.

To use the package, you need an API key from the National Land Survey of Finland. Visit their website to obtain one. Once you have the key, set it in R using:

options(geofi_mml_api_key = "your_api_key_here")

Package Overview

The geofi package includes the following key functions:

All functions return spatial data as sf objects, compatible with the sf package for spatial analysis and visualization.

Step 1: Listing Available Collections

To explore the datasets available in the Maastotietokanta, use ogc_get_maastotietokanta_collections(). This function queries the OGC API and returns a data frame with collection IDs and descriptions.

collections <- ogc_get_maastotietokanta_collections()
head(collections)

This list helps you identify the collection parameter needed for ogc_get_maastotietokanta().

Step 2: Downloading a Maastotietokanta Collection

The ogc_get_maastotietokanta() function downloads a specific collection as an sf object. You can customize the output with parameters like:

Example: Downloading Cemeteries

Let’s download the “hautausmaa” collection in the default CRS (EPSG:3067):

cemeteries <- ogc_get_maastotietokanta(collection = "hautausmaa", crs = 4326)
cemeteries

Example: Spatial Filtering with a Bounding Box

To download cemeteries within a specific area (e.g., around Helsinki), use the bbox parameter. Coordinates must be in EPSG:4326 (WGS84).

cemeteries_helsinki <- ogc_get_maastotietokanta(
  collection = "hautausmaa",
  bbox = "24.5,60.1,25.5,60.5",
  crs = 4326
)

Visualize the results using ggplot2:

ggplot(cemeteries_helsinki) +
  geom_sf() +
  theme_minimal() +
  labs(title = "Cemeteries near Helsinki")

Example: Handling Large Collections

For large collections like “suo” (bogs/marshes), you may need to increase max_pages to fetch all features:

bogs <- ogc_get_maastotietokanta(
  collection = "suo",
  max_pages = 15
)

Note: Large collections may take significant time to download. If max_pages is reached, a warning will indicate that additional features may exist.

Step 3: Querying Geographic Names

The ogc_get_nimisto() function retrieves place names from the Geographic Names dataset. You can filter results by:

Example: Searching for Place Names

Search for place names containing “kainu”:

kainu_places <- ogc_get_nimisto(search_string = "kainu")
print(kainu_places)

Visualize the results:

ggplot(kainu_places) +
  geom_sf() +
  geom_sf_text(aes(label = spelling), size = 3, check_overlap = TRUE) +
  theme_minimal() +
  labs(title = "Place Names Containing 'Kainu'")

Example: Combining Search and Spatial Filtering

Search for “kainu*” (with * wildcard) within a bounding box covering most part of Kainuu-region:

kainu_bbox <- ogc_get_nimisto(
  search_string = "kainu*",
  bbox = "27.515259,63.450509,30.531006,64.524823",
  crs = 4326
)

Advanced Features

Error Handling

The package includes robust error handling:

Custom Parameters

Function ogc_get_nimisto() support custom_params for advanced API queries. For example:

swimming_beaches_filtered_by_municipality_number <- ogc_get_nimisto(
  search_string = "*uimaranta*",
  custom_params = "municipality=091"
)
swimming_beaches_filtered_by_municipality_number

Consult the NLS OGC API documentation for valid parameters.

Best Practices

Additional Resources

Conclusion

The geofi package simplifies access to the Maastotietokanta and Geographic Names datasets, enabling spatial analysis of Finnish topographic and place name data. By handling API requests, pagination, and CRS transformations, it allows users to focus on data analysis and visualization. Try the examples above and explore the datasets to uncover insights about Finland’s geography!