Getting started with happign

Paul Carteron

2023-08-03

Before starting

We can load the happign package, and some additional packages we will need (sf to manipulate spatial data and tmap to create maps)

library(happign)
library(sf)
library(tmap)

WFS and WMS service

happign use two web service from IGN :

More detailed information are available here for WMS and here for WFS.

To download data from IGN web services at least three elements are needed :

API key

API keys can be directly retrieved on the IGN website from the expert web services (I recommend you at this point to go and have a look) or with get_apikeys() function.

get_apikeys()
#>  [1] "administratif" "adresse"       "agriculture"   "altimetrie"   
#>  [5] "cartes"        "cartovecto"    "clc"           "economie"     
#>  [9] "enr"           "environnement" "geodesie"      "lambert93"    
#> [13] "ocsge"         "ortho"         "orthohisto"    "parcellaire"  
#> [17] "satellite"     "sol"           "topographie"   "transports"

Layer name

As for API key, it is possible to find the names of available layers from the expert web services of the IGN. For example, the first layer name in WFS format for “Administratif” category is “ADMINEXPRESS-COG-CARTO.LATEST:arrondissement”

Again, all layer’s name can be accessed from R with the get_layers_metadata() function. This one connects directly to the IGN site which allows to have the last updated resources. It can be used for WMS and WFS :

administratif_wfs <- get_layers_metadata(apikey = "administratif", data_type = "wfs")
administratif_wms <- get_layers_metadata(apikey = "administratif", data_type = "wms")

head(administratif_wfs)
#>                                                                 Name
#> 1                       ADMINEXPRESS-COG-CARTO.LATEST:arrondissement
#> 2             ADMINEXPRESS-COG-CARTO.LATEST:arrondissement_municipal
#> 3                               ADMINEXPRESS-COG-CARTO.LATEST:canton
#> 4     ADMINEXPRESS-COG-CARTO.LATEST:chflieu_arrondissement_municipal
#> 5                      ADMINEXPRESS-COG-CARTO.LATEST:chflieu_commune
#> 6 ADMINEXPRESS-COG-CARTO.LATEST:chflieu_commune_associee_ou_deleguee
#>                                                                      Title
#> 1                       ADMINEXPRESS-COG-CARTO.LATEST:arrondissement.title
#> 2             ADMINEXPRESS-COG-CARTO.LATEST:arrondissement_municipal.title
#> 3                               ADMINEXPRESS-COG-CARTO.LATEST:canton.title
#> 4     ADMINEXPRESS-COG-CARTO.LATEST:chflieu_arrondissement_municipal.title
#> 5                      ADMINEXPRESS-COG-CARTO.LATEST:chflieu_commune.title
#> 6 ADMINEXPRESS-COG-CARTO.LATEST:chflieu_commune_associee_ou_deleguee.title
#>       Abstract
#> 1 édition 2023
#> 2 édition 2023
#> 3 édition 2023
#> 4 édition 2023
#> 5 édition 2023
#> 6 édition 2023

Downloading the data

Now that we know how to get an API key and layer name, it only takes a few lines to get plethora of resources. For the example we will look at the beautiful town of Penmarch in France. A part of this town is stored as a shape in happign.

penmarch <- read_sf(system.file("extdata/penmarch.shp", package = "happign"))

WFS

get_wfs can be used to download borders :


penmarch_borders <- get_wfs(x = penmarch,
                            apikey = "administratif",
                            layer = "LIMITES_ADMINISTRATIVES_EXPRESS.LATEST:commune")
#> Features downloaded : 1

# Checking result
tm_shape(penmarch_borders)+
   tm_polygons(alpha = 0, lwd = 2)+
tm_shape(penmarch)+
   tm_polygons(col = "red")+
tm_add_legend(type = "fill", border.col = "black", border.lwd =2, 
              col = NA, labels = "border from get_wfs")+
tm_add_legend(type = "fill", col = "red", labels = "penmarch shape from happign package")+
tm_layout(main.title = "Penmarch borders from IGN",
          main.title.position = "center",
          legend.position = c(0.7, -0.1),
          outer.margins = c(0.1, 0,0,0),
          frame = FALSE)
#> Legend labels were too wide. The labels have been resized to 0.63. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
plot of chunk unnamed-chunk-6

plot of chunk unnamed-chunk-6

It’s as simple as that! Now you have to rely on your curiosity to explore the multiple possibilities that IGN offers. For example, who has never wondered how many road junctions there are in Penmarch?

Spoiler : there are 192 of them !

dikes <- get_wfs(x = penmarch_borders,
                 apikey = get_apikeys()[6], #cartovecto
                 layer = "BDCARTO_BDD_WLD_WGS84G:noeud_routier",
                 spatial_filter = "intersects")
#> Features downloaded : 192

# Checking result
tm_shape(penmarch_borders) + # Borders of penmarch
   tm_borders(lwd = 2) +
tm_shape(dikes) + # Point use to retrieve data
   tm_symbols(col = "red", shape = 13, size = 0.3) +
   tm_add_legend(type = "symbol", shape = 13, label = "Road junction", col = "red") +
   tm_layout(main.title = "Road nodes recorded by the IGN in Penmarch",
             main.title.position = "center",
             legend.position = c("right", "bottom"),
             frame = FALSE)
plot of chunk unnamed-chunk-7

plot of chunk unnamed-chunk-7

WMS raster

For raster, the process is the same, but with the function get_wms_raster(), but you need to specify the resolution (note that it must be in the same coordinate system as the crs parameter). There’s plenty of elevation resources inside “altimetrie” category. A basic one is the Digital Elevation Model (DEM or MNT in French). Borders of Penmarch are used to download the DEM.

layers_metadata <- get_layers_metadata("altimetrie", "wms")
dem_layer <- layers_metadata[2, 1] #LEVATION.ELEVATIONGRIDCOVERAGE

mnt <- get_wms_raster(x = penmarch_borders,
                      apikey = "altimetrie",
                      layer = dem_layer,
                      res = 25,
                      crs = 2154)
#> 0...10...20...30...40...50...60...70...80...90...100 - done.
#> Raster is saved at :
#> C:\Users\PAUL\AppData\Local\Temp\RtmpK29L2a\file12e45e89658a.tif

mnt[mnt < 0] <- NA # remove negative values in case of singularity

tm_shape(mnt) +
   tm_raster(title = "Elevation [m]") +
tm_shape(penmarch_borders)+
   tm_borders(lwd = 2)+
tm_layout(main.title = "DEM of Penmarch",
          main.title.position = "center",
          legend.position = c("right", "bottom"),
          legend.bg.color = "white", legend.bg.alpha = 0.7)
plot of chunk unnamed-chunk-8

plot of chunk unnamed-chunk-8

Rq :

  • Raster from get_wms_raster() are SpatRaster object from the terra package. To learn more about conversion between other raster type in R go check this out.