Title: | A Collection of Useful R Functions for Spatial Data |
---|---|
Description: | Useful R functions developed at the Research Institute for Nature and Forest (INBO) for dealing with spatial raster or vector data. |
Authors: | Hans Van Calster [aut, cre] (<https://orcid.org/0000-0001-8595-8426>, Research Institute for Nature and Forest (INBO)), Floris Vanderhaeghe [ctb] (<https://orcid.org/0000-0002-6378-6229>, Research Institute for Nature and Forest (INBO)), Research Institute for Nature and Forest (INBO) [cph, fnd] |
Maintainer: | Hans Van Calster <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.2 |
Built: | 2024-11-22 03:21:42 UTC |
Source: | https://github.com/inbo/inbospatial |
The add_wms_*()
functions are shorthand alternatives for a fully specified
leaflet::addWMSTiles()
or leaflet.extras2::addWMS()
statement.
Appropriate attribution is added to the Leaflet map depending on the layer.
add_wms_be_cartoweb( map, layer = c("topo", "topo_grey", "overlay", "crossborder"), options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_be_ortho( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_nl_ortho( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_fl_grbmap( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_fl_habitatmap( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 1200), add_wms_legend = FALSE, ... ) add_wms_fl_habitatlabels( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_fl_forestnature( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 600), add_wms_legend = FALSE, ... ) add_wms_fl_agriculture( map, year, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 1200), add_wms_legend = FALSE, ... ) add_wms_nlnb_nnb( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 600), add_wms_legend = FALSE, ... ) add_wms_nlnb_natte_natuurparels( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_ambitie_landschapstypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_ambitie_natuurtypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_landschapstypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html", minZoom = 13), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_natuurtypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html", minZoom = 13), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... )
add_wms_be_cartoweb( map, layer = c("topo", "topo_grey", "overlay", "crossborder"), options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_be_ortho( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_nl_ortho( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_fl_grbmap( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_fl_habitatmap( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 1200), add_wms_legend = FALSE, ... ) add_wms_fl_habitatlabels( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE), ... ) add_wms_fl_forestnature( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 600), add_wms_legend = FALSE, ... ) add_wms_fl_agriculture( map, year, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 1200), add_wms_legend = FALSE, ... ) add_wms_nlnb_nnb( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 600), add_wms_legend = FALSE, ... ) add_wms_nlnb_natte_natuurparels( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_ambitie_landschapstypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_ambitie_natuurtypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html"), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_landschapstypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html", minZoom = 13), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... ) add_wms_nlnb_natuurtypen( map, options = leaflet::WMSTileOptions(format = "image/png", transparent = TRUE, info_format = "text/html", minZoom = 13), popupOptions = leaflet::popupOptions(maxWidth = 2000), add_wms_legend = FALSE, ... )
map |
a map widget object created from |
layer |
String that defines which layer to use, if the function supports more than one. In such case, the available strings are shown in the Usage section. The first value is used if missing. |
options |
a list of extra options for tile layers, popups, paths (circles, rectangles, polygons, ...), or other map elements |
... |
Further arguments passed to |
popupOptions |
A Vector of |
add_wms_legend |
Logical. Is a legend to be added for this WMS? Note that the legend cannot be toggled on and off in the map; it is plotted as a separate, static legend. |
year |
Year to be applied in selecting the WMS |
On condition that these functions continue being maintained, using the shorthand functions should make your scripts more futureproof.
A leaflet
HTML widget object.
be_
Belgium
fl_
Flanders
nl_
Netherlands
nlnb_
Netherlands: Province 'Noord-Brabant'
_ortho
Orthophotographs
_cartoweb
Topographic Cartoweb layers (Belgium)
_grbmap
GRB basemap (Flanders; 'Grootschalig Referentiebestand')
_habitatmap
Natura 2000 habitat types
_habitatlabels
Natura 2000 habitat type labels
_forestnature
Public forest and nature areas managed by the Flemish Nature & Forest Agency
_agriculture
Parcels with the agricultural use in a specific year
_nnb
Netherlands; 'Natuurnetwerk Brabant (Rijk en Provincie NNB)'
_natte_natuurparels
Netherlands; 'Natte natuurparels'
_ambitie_landschapstypen
Netherlands; 'Natuurbeheerplan - Ambitiekaart Landschaptypen'
_ambitie_natuurtypen
Netherlands; 'Natuurbeheerplan - Ambitiekaart Natuurtypen'
_landschapstypen
Netherlands; 'Natuurbeheerplan - Beheertypenkaart_Landschaptypen'
_natuurtypen
Netherlands; 'Natuurbeheerplan - Beheertypenkaart Natuurtypen'
Floris Vanderhaeghe, https://github.com/florisvdh
Other topics on using web services:
add_wmts_nl_brt()
,
get_coverage_wcs()
,
get_feature_wfs()
library(leaflet) leaflet() |> setView(lng = 4.5, lat = 51.45, zoom = 11) |> add_wmts_nl_brt("grijs") |> add_wms_be_cartoweb("topo_grey") |> add_wms_fl_forestnature() |> add_wms_nlnb_nnb() # It can also use mapview objects, if you first extract the leaflet map slot: library(mapview) kmi_stations <- sf::read_sf( system.file("extdata/kmi_stations.geojson", package = "inbospatial") ) mapview(kmi_stations, map.types = "CartoDB.Positron")@map |> add_wms_be_cartoweb() # alternative syntax: ## Not run: mapview(kmi_stations, map.types = "CartoDB.Positron") |> _@map |> add_wms_be_cartoweb() ## End(Not run)
library(leaflet) leaflet() |> setView(lng = 4.5, lat = 51.45, zoom = 11) |> add_wmts_nl_brt("grijs") |> add_wms_be_cartoweb("topo_grey") |> add_wms_fl_forestnature() |> add_wms_nlnb_nnb() # It can also use mapview objects, if you first extract the leaflet map slot: library(mapview) kmi_stations <- sf::read_sf( system.file("extdata/kmi_stations.geojson", package = "inbospatial") ) mapview(kmi_stations, map.types = "CartoDB.Positron")@map |> add_wms_be_cartoweb() # alternative syntax: ## Not run: mapview(kmi_stations, map.types = "CartoDB.Positron") |> _@map |> add_wms_be_cartoweb() ## End(Not run)
The add_wmts_*()
functions are shorthand alternatives for a fully specified
leaflet::addTiles()
statement.
Appropriate attribution is added to the Leaflet map depending on the layer.
add_wmts_nl_brt(map, layer = c("standaard", "grijs", "pastel", "water"), ...) add_wmts_nl_ortho(map, ...)
add_wmts_nl_brt(map, layer = c("standaard", "grijs", "pastel", "water"), ...) add_wmts_nl_ortho(map, ...)
map |
a map widget object created from |
layer |
String that defines which layer to use, if the function supports more than one. In such case, the available strings are shown in the Usage section. The first value is used if missing. |
... |
Further arguments passed to |
On condition that these functions continue being maintained, using the shorthand functions should make your scripts more futureproof.
A leaflet
HTML widget object.
be_
Belgium
nl_
Netherlands
_ortho
Orthophotographs
_brt
Topographic BRT layers (Netherlands; 'Basisregistratie Topografie')
Floris Vanderhaeghe, https://github.com/florisvdh
Other topics on using web services:
add_wms_be_cartoweb()
,
get_coverage_wcs()
,
get_feature_wfs()
library(leaflet) leaflet() |> setView(lng = 5.5, lat = 52.5, zoom = 9) |> add_wmts_nl_brt() leaflet() |> setView(lng = 5.4, lat = 52.2, zoom = 14) |> add_wmts_nl_ortho()
library(leaflet) leaflet() |> setView(lng = 5.5, lat = 52.5, zoom = 9) |> add_wmts_nl_brt() leaflet() |> setView(lng = 5.4, lat = 52.2, zoom = 14) |> add_wmts_nl_ortho()
The function sends a query to a WCS service, downloads it to a temporary file
from which it is read with terra::rast()
- if needed reprojected -
and returned as a SpatRaster
object
get_coverage_wcs( wcs = c("dtm", "dsm", "omz", "omw"), bbox, layername, resolution, wcs_crs = "EPSG:4258", output_crs = "EPSG:31370", bbox_crs = "EPSG:31370", version = c("1.0.0", "2.0.1"), ... )
get_coverage_wcs( wcs = c("dtm", "dsm", "omz", "omw"), bbox, layername, resolution, wcs_crs = "EPSG:4258", output_crs = "EPSG:31370", bbox_crs = "EPSG:31370", version = c("1.0.0", "2.0.1"), ... )
wcs |
One of |
bbox |
An object of class bbox of length 4. |
layername |
Character string; name of the layer |
resolution |
Output resolution in meters |
wcs_crs |
Native CRS in which the raster layers are stored on the |
output_crs |
Output CRS. May involve reprojection. |
bbox_crs |
CRS in which bbox coordinates are passed |
version |
|
... |
Additional key-value pairs passed on to the |
The following WCS services can currently be used:
"omz"
: orthophotomosaic summer images Flanders
"omw"
: orthophotomosaic winter images Flanders
"dtm"
: digital terrain model Flanders
"dsm"
: digital surface model Flanders
See metadata Vlaanderen for more information # nolint: line_length_linter.
A SpatRaster
object
Other topics on using web services:
add_wms_be_cartoweb()
,
add_wmts_nl_brt()
,
get_feature_wfs()
## Not run: bbox <- sf::st_bbox( c(xmin = 155800, xmax = 155850, ymin = 132700, ymax = 132750), crs = sf::st_crs(31370) ) get_coverage_wcs( wcs = "dsm", bbox = bbox, layername = "EL.GridCoverage.DSM", resolution = 1 ) ## End(Not run)
## Not run: bbox <- sf::st_bbox( c(xmin = 155800, xmax = 155850, ymin = 132700, ymax = 132750), crs = sf::st_crs(31370) ) get_coverage_wcs( wcs = "dsm", bbox = bbox, layername = "EL.GridCoverage.DSM", resolution = 1 ) ## End(Not run)
This function constructs a URL
request from its arguments and either reads
in the resulting vector layer as a sf
object or returns the number of
features that are requested.
The request is made up of key-value pairs and additional key-value pairs can
be passed to the function.
The full documentation for the WFS
standard can be consulted from
https://www.ogc.org/standard/wfs/.
get_feature_wfs( wfs, version = "2.0.0", layername = NULL, crs = NULL, bbox = NULL, filter = NULL, cql_filter = NULL, output_format = NULL, property_name = NULL, result_type = c("results", "hits"), ... )
get_feature_wfs( wfs, version = "2.0.0", layername = NULL, crs = NULL, bbox = NULL, filter = NULL, cql_filter = NULL, output_format = NULL, property_name = NULL, result_type = c("results", "hits"), ... )
wfs |
Web address for the service which you want to query features from |
version |
Version number for the service.
For instance |
layername |
Optional name of a layer hosted by the web feature service |
crs |
Optional coordinate reference system to represent the features.
For instance |
bbox |
Optional bounding box.
Pass this as a named vector with names |
filter |
Optional standard OGC filter specification |
cql_filter |
Optional
Contextual Query Language filter.
This currently only works if the |
output_format |
Optional output format supported by the |
property_name |
Optional character string.
Which fields or columns to return?
If you want to specify multiple columns, separate them by a comma.
The column containing the feature geometry is usually called |
result_type |
For version |
... |
Additional key-value pairs passed on to the WFS query. |
See https://inbo.github.io/tutorials/tutorials/spatial_wfs_services/ for more information.
Other topics on using web services:
add_wms_be_cartoweb()
,
add_wmts_nl_brt()
,
get_coverage_wcs()
## Not run: vlaanderen <- get_feature_wfs( wfs = paste0( "https://eservices.minfin.fgov.be/", "arcgis/services/R2C/Regions/MapServer/WFSServer" ), layername = "regions", crs = "EPSG:31370", filter = paste0( "<Filter><PropertyIsEqualTo><PropertyName>", "regions:NameDUT</PropertyName><Literal>'Vlaams Gewest'", "</Literal></PropertyIsEqualTo></Filter>" ) ) ## End(Not run)
## Not run: vlaanderen <- get_feature_wfs( wfs = paste0( "https://eservices.minfin.fgov.be/", "arcgis/services/R2C/Regions/MapServer/WFSServer" ), layername = "regions", crs = "EPSG:31370", filter = paste0( "<Filter><PropertyIsEqualTo><PropertyName>", "regions:NameDUT</PropertyName><Literal>'Vlaams Gewest'", "</Literal></PropertyIsEqualTo></Filter>" ) ) ## End(Not run)
The scale factor is the distance distortion. In this case (a conic conformal projection) it holds in any direction.
scalefactor_lcc(par_deg, par1_deg, par2_deg)
scalefactor_lcc(par_deg, par1_deg, par2_deg)
par_deg |
Numeric vector. The latitude in decimal degrees of one or more parallels for which to do the calculation. |
par1_deg |
Latitude of the first standard parallel of the LCC projection in decimal degrees. |
par2_deg |
Latitude of the second standard parallel of the LCC projection in decimal degrees. |
The function applies to normal LCC projections, not the oblique ones. The applied formulas are for the sphere, hence approximate.
Numeric vector, same length as par_deg
.
Floris Vanderhaeghe, https://github.com/florisvdh
Snyder J.P. (1987). Map Projections - A Working Manual. U.S. Geological Survey Professional Paper, Nr. 1395. United States Government Printing Office, Washington, 397 p.
Other functions to explore properties of coordinate reference systems:
scalefactor_tcyl()
sf::st_crs(31370) x <- seq(49.5, 52.5, 0.1) y <- scalefactor_lcc(x, 51.1666672333333, 49.8333339) data.frame(latitude = x, scalefactor = y)
sf::st_crs(31370) x <- seq(49.5, 52.5, 0.1) y <- scalefactor_lcc(x, 51.1666672333333, 49.8333339) data.frame(latitude = x, scalefactor = y)
The scale factor is the distance distortion in the direction of the meridians. In the case of the Transverse Mercator projection (a cylindrical conformal projection) it holds in any direction.
scalefactor_tcyl(ang_dist)
scalefactor_tcyl(ang_dist)
ang_dist |
Numeric vector. One or more angular distances from the central meridian, in decimal degrees. |
The formulas applied are for the sphere, hence approximate.
The best known map projection system in this context is the Universal Transverse Mercator (UTM), which superposes a central meridian scale factor of 0.9996.
Numeric vector, same length as ang_dist
.
Floris Vanderhaeghe, https://github.com/florisvdh
Iliffe J. & Lott R. (2008). Datums and Map Projections. For Remote Sensing, GIS and Surveying. 2nd edn. Whittles Publishing, Caithness, UK, 208 p.
Other functions to explore properties of coordinate reference systems:
scalefactor_lcc()
x <- seq(0, 3, 0.1) y <- scalefactor_tcyl(x) data.frame(ang_dist = x, scalefactor = y)
x <- seq(0, 3, 0.1) y <- scalefactor_tcyl(x) data.frame(ang_dist = x, scalefactor = y)