--- title: "Getting started with happign" author: "Paul Carteron" date: "2024-05-05" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Getting started with happign} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- # 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) ```r library(happign) library(sf) library(tmap);tmap_mode("plot") #> tmap mode set to plotting ``` # WFS, WMS and WMTS service `happign` use three web service from IGN : * WMS raster : data in raster format e.g. images (.jpg, .png, .tif, ...) * WMTS : same as WMS raster but images are precalculated * WFS : data in vector format (.shp, ...). More detailed information are available [here](https://www.ogc.org/standard/wms/) for WMS, [here](https://www.ogc.org/standard/wmts/) for WMTS and [here](https://www.ogc.org/standard/wfs/) for WFS. To download data from IGN web services at least two elements are needed : * A layer name ; * An input shape read by [`sf`]( https://CRAN.R-project.org/package=sf) package. ## Layer name It is possible to find the names of available layers from the IGN website. For example, the first layer name in **WFS format** for ["Administratif" category](https://geoservices.ign.fr/services-web-experts-administratif) is *"ADMINEXPRESS-COG-CARTO.LATEST:arrondissement"* 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 : ```r administratif_wfs <- get_layers_metadata(data_type = "wfs") administratif_wms <- get_layers_metadata(data_type = "wms-r") administratif_wms <- get_layers_metadata(data_type = "wmts") head(administratif_wfs) #> Name #> 1 OCS-GERS_BDD_LAMB93_2016:oscge_gers_32_2016 #> 2 OCS-GERS_BDD_LAMB93_2019:oscge_gers_32_2019 #> 3 ADMINEXPRESS-COG.LATEST:arrondissement #> 4 ADMINEXPRESS-COG.LATEST:arrondissement_municipal #> 5 ADMINEXPRESS-COG.LATEST:canton #> 6 ADMINEXPRESS-COG.LATEST:chflieu_arrondissement_municipal #> Title Abstract #> 1 OCSGE Gers 2016 OCSGE Gers 2016 #> 2 OCSGE Gers 2019 OCSGE Gers 2019 #> 3 ADMINEXPRESS COG 2023 édition 2023 #> 4 ADMINEXPRESS COG 2023 édition 2023 #> 5 ADMINEXPRESS COG 2023 édition 2023 #> 6 ADMINEXPRESS COG 2023 édition 2023 ``` You can specify an apikey to focus on specific category. API keys can be directly retrieved on the [IGN website from the expert web services](https://geoservices.ign.fr/services-web-experts) or with `get_apikeys()` function. ```r get_apikeys() #> [1] "administratif" "adresse" "agriculture" #> [4] "altimetrie" "cartes" "cartovecto" #> [7] "clc" "economie" "enr" #> [10] "environnement" "geodesie" "lambert93" #> [13] "ocsge" "ortho" "orthohisto" #> [16] "parcellaire" "satellite" "sol" #> [19] "topographie" "transports" administratif_wmts <- get_layers_metadata("wmts", "administratif") head(administratif_wmts) #> Title #> 1 ADMINEXPRESS COG CARTO #> 2 ADMINEXPRESS COG #> 3 Limites administratives mises à jour en continu. #> Abstract #> 1 Limites administratives Express COG code officiel géographique 2023 #> 2 Limites administratives Express COG code officiel géographique. 2023 #> 3 Limites administratives mises à jour en continu ; Edition : 2024-03-25 #> Identifier #> 1 ADMINEXPRESS-COG-CARTO.LATEST #> 2 ADMINEXPRESS-COG.LATEST #> 3 LIMITES_ADMINISTRATIVES_EXPRESS.LATEST ``` ## Downloading the data Now that we know how to get a 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. ```r penmarch <- read_sf(system.file("extdata/penmarch.shp", package = "happign")) ``` ### WFS `get_wfs` can be used to download borders : ```r penmarch_borders <- get_wfs(x = penmarch, 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.61. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger. ```