Skip to contents

Download a raster layer from IGN Web Mapping Services (WMS). To do that, it need a location giving by a shape, an apikey and the name of layer. You can find those information from IGN website or with get_apikeys() and get_layers_metadata().

Usage

get_wms_raster(x,
               apikey = "altimetrie",
               layer = "ELEVATION.ELEVATIONGRIDCOVERAGE",
               res = 25,
               filename = tempfile(fileext = ".tif"),
               crs = 2154,
               overwrite = FALSE,
               version = "1.3.0",
               styles = "",
               interactive = FALSE)

Arguments

x

Object of class sf or sfc. Needs to be located in France.

apikey

character; API key from get_apikeys() or directly from IGN website.

layer

character; layer name from get_layers_metadata(apikey, "wms") or directly from IGN website.

res

numeric; resolution in the unit of the coordinate system (e.g. meter for 2154). See detail for more information about res.

filename

character or NULL; filename or a open connection for writing. (ex : "test.tif" or "~/test.tif"). If NULL, layer is used as filename. Default drivers is ".tif" but all gdal drivers are supported, see details for more info.

crs

numeric, character, or object of class sf or sfc. It is set to EPSG:2154 by default. See sf::st_crs() for more detail.

overwrite

If TRUE, output raster is overwrite.

version

character; version of the service used. See details for more info.

styles

character; rendering style of the layer. Set to "" by default. See details for more info.

interactive

logical; If TRUE, interactive menu ask for apikey and layer.

Value

SpatRaster object from terra package.

Details

  • res : Warning, setting res higher than default layer resolution multiplies the number of pixels without increasing the precision. For example, the download of the BD Alti layer from IGN will be optimal for a resolution of 25m.

  • version and styles arguments are detailed on IGN documentation

  • filename : All GDAL supported drivers can be found here

  • overwrite : get_wms raster always checks that filename does not already exist. If it does, it is imported into R without further downloading unless overwrite is set to TRUE.

Examples

if (FALSE) {
library(sf)
library(tmap)

# Shape from the best town in France
penmarch <- read_sf(system.file("extdata/penmarch.shp", package = "happign"))

# For quick testing use interactive = TRUE
raster <- get_wms_raster(x = penmarch, interactive = TRUE)

# For specific data, choose apikey with get_apikey() and layer with get_layers_metadata()
apikey <- get_apikeys()[4]  # altimetrie
metadata_table <- get_layers_metadata(apikey, "wms") # all layers for altimetrie wms
layer <- metadata_table[2,1] # ELEVATION.ELEVATIONGRIDCOVERAGE

# Downloading digital elevation model from IGN
mnt_2154 <- get_wms_raster(penmarch, apikey, layer, res = 25)

# If crs is set to 4326, res is in degrees
mnt_4326 <- get_wms_raster(penmarch, apikey, layer, res = 0.0005, crs = 4326)

# Plotting result
tm_shape(mnt_4326)+
   tm_raster()+
tm_shape(penmarch)+
   tm_borders(col = "blue", lwd  = 3)
}