st_read(): importing WFS data into R using GeoJSON

When a WFS server offers spatial data in GeoJSON format, as some (but not all) of them do, this information can easily be imported into R. The Web Feature Service (WFS) Interface Standard, specified by the Open Geospatial Consortium (OGC), provides an interface allowing requests for geographical features across the web using platform-independent calls.

The web map below - showing regions in Finland - is created with the use of R, with only a few lines of code. And in this blog we will show how to do this.

Open Data

The WFS protocol is often used to deliver geographic information as Open Data. For this demonstration we have chosen a map offered by Tilastokeskus (Statistics Finland).

The default output format of a WFS service is GML version 3.2. Somehow this GML format never managed to become one of my favorites :-)

Fortunately some WFS severs also offers other optional output formats, as for example GeoJOSN. GeoJSON is a geospatial data interchange format based on JavaScript Object Notation (JSON). And this format can be very well used as input to the function st_read to import the data into R.

Click the links below for a quick comparison between the two output formats:

(In case the Finnish statistical authorities ever decide to remove or rename this service: we have stored a backup of the GeoJSON output, as it was delivere at the time of writing of this blog.)

R Code

We start by loading the required packages:

library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
library(mapview)

Next we specify the URL for our WFS request. For readability we specify the address in two parts, which are then pasted together:

baseurl <- "http://geo.stat.fi/geoserver/vaestoalue/wfs?"
wfs_request <- "request=GetFeature&service=WFS&version=2.0.0&typeName=vaestoalue:suuralue_vaki2014&outputFormat=application/json"

fi_regions_wfs <- paste0(baseurl,wfs_request)

This URL is fed as input to the function st_read to create the Simple feature collection fi_regions. (A Simple feature collection is - basically - an R data frame with a geometry column.)

fi_regions <- st_read(fi_regions_wfs)
## Reading layer `OGRGeoJSON' from data source `http://geo.stat.fi/geoserver/vaestoalue/wfs?request=GetFeature&service=WFS&version=2.0.0&typeName=vaestoalue:suuralue_vaki2014&outputFormat=application/json' using driver `GeoJSON'
## Simple feature collection with 5 features and 20 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 83747.59 ymin: 6637032 xmax: 732907.8 ymax: 7776431
## epsg (SRID):    3067
## proj4string:    +proj=utm +zone=35 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# plot(st_geometry(fi_regions))
# View(fi_regions)

Optionally you can plot or view the finnish regions to inspect it’s content.

Or you can immediatelly create an interactive map:

mapview(fi_regions, label = fi_regions$name, color = "darkgreen", col.regions = "green", alpha.regions = .05)

This last line of code creates the web map shown at the top of this page.

All the code shown above is stored in this script: WFS_R.R.

Egge-Jan Pollé