#' Provide a map object of a sites LTER.
#' @description `r lifecycle::badge("stable")`
#' This function produces a `map` of the site boundaries
#' as provided by the \href{https://deims.org/}{DEIMS-SDR catalogue}, within
#' a given country and network.
#' @param deimsid A `character`. The DEIMS ID of network from
#' DEIMS-SDR website. DEIMS ID information
#' \href{https://deims.org/docs/deimsid.html}{here}.
#' @param countryCode A `character` following the SO 3166-1 alpha-3 codes.
#' This ISO convention consists of three-letter country codes
#' as defined in ISO 3166-1, part of the ISO 3166 standard published by the
#' International Organization for Standardization (ISO), to represent countries,
#' dependent territories, and special areas of geographical interest.
#' The map produced by this function will be limited to the country
#' indicated in this parameter; if the network has a extraterritorial sites
#' those will not represented.
#' @param listOfSites A `sf`. List of sites of specific network. This list
#' is needed for showing another points on the map.
#' @param gridNx A `double`. A numeric vector or unit object specifying
#' x-location of viewports about country provided by countryCode parameter.
#' @param gridNy A `double`. A numeric vector or unit object specifying
#' y-location of viewports about country provided by countryCode parameter.
#' @param width A `double`. A numeric vector or unit object specifying width
#' of viewports about country provided by countryCode parameter. Default 0.25.
#' @param height A `double`. A numeric vector or unit object specifying height
#' of viewports about country provided by countryCode parameter. Default 0.25.
#' @param bboxXMin A `double`. A numeric for add some unit of a bbox provided
#' by centroid of the site. Default 0.
#' @param bboxYMin A `double`. A numeric for add some unit of a bbox provided
#' by centroid of the site. Default 0.
#' @param bboxXMax A `double`. A numeric for add some unit of a bbox provided
#' by centroid of the site. Default 0.
#' @param bboxYMax A `double`. A numeric for add some unit of a bbox provided
#' by centroid of the site. Default 0.
#' @param show_map A `boolean`. When TRUE the immage of map will be plotted.
#' Default FALSE.
#' @return The output of the function is an `image` of the boundary of the
#' site, OSM as base map and all country sites map.
#' @author Alessandro Oggioni, phD (2020) \email{oggioni.a@@irea.cnr.it}
#' @importFrom sf as_Spatial st_as_sfc st_bbox st_crs st_simplify
#' @importFrom jsonlite fromJSON
#' @importFrom tibble tribble
#' @importFrom rosm osm.raster
#' @importFrom tmap tm_shape tm_rgb tm_dots tm_compass tm_scale_bar tm_layout
#' @importFrom tmap tm_credits tm_basemap tm_borders tm_fill tm_lines
#' @importFrom grid viewport
#' @importFrom Rdpack reprompt
#' @references
#' \insertRef{sfR}{ReLTER}
#'
#' \insertRef{jsonliteR}{ReLTER}
#'
#' \insertRef{tibbleR}{ReLTER}
#'
#' \insertRef{rasterR}{ReLTER}
#'
#' \insertRef{rgeosR}{ReLTER}
#'
#' \insertRef{rosmR}{ReLTER}
#'
#' \insertRef{tmapR}{ReLTER}
#'
#' \insertRef{gridR}{ReLTER}
#' @export
#' @examples
#' \dontrun{
#' # Example of Lange Bramke site
#' sitesNetwork <- get_network_sites(
#' networkDEIMSID =
#' "https://deims.org/networks/e904354a-f3a0-40ce-a9b5-61741f66c824"
#' )
#' map <- produce_site_map(
#' deimsid = "https://deims.org/8e24d4f8-d6f6-4463-83e9-73cac2fd3f38",
#' countryCode = "DEU",
#' listOfSites = sitesNetwork,
#' gridNx = 0.2,
#' gridNy = 0.7
#' )
#'
#' # Example of Eisenwurzen site
#' sitesNetwork <- get_network_sites(
#' networkDEIMSID =
#' "https://deims.org/networks/d45c2690-dbef-4dbc-a742-26ea846edf28"
#' )
#' map <- produce_site_map(
#' deimsid = "https://deims.org/d0a8da18-0881-4ebe-bccf-bc4cb4e25701",
#' countryCode = "AUT",
#' listOfSites = sitesNetwork,
#' gridNx = 0.2,
#' gridNy = 0.7
#' )
#'
#' # Example of Lake Maggiore site
#' sitesNetwork <- get_network_sites(
#' networkDEIMSID =
#' "https://deims.org/network/7fef6b73-e5cb-4cd2-b438-ed32eb1504b3"
#' )
#' # In the case of Italian sites are selected only true sites and excluded the
#' # macrosites.
#' sitesNetwork <- (sitesNetwork[!grepl('^IT', sitesNetwork$title),])
#' sf::st_crs(sitesNetwork) = 4326
#' siteMap <- produce_site_map(
#' deimsid = "https://deims.org/f30007c4-8a6e-4f11-ab87-569db54638fe",
#' countryCode = "ITA",
#' listOfSites = sitesNetwork,
#' gridNx = 0.7,
#' gridNy = 0.35,
#' show_map = TRUE
#' )
#' siteMap
#'
#' # with show_map = FALSE
#' siteMap <- produce_site_map(
#' deimsid = "https://deims.org/f30007c4-8a6e-4f11-ab87-569db54638fe",
#' countryCode = "ITA",
#' listOfSites = sitesNetwork,
#' gridNx = 0.7,
#' gridNy = 0.35
#' )
#' siteMap
#' }
#'
#' @section The function output:
#' \figure{produce_site_map_fig.png}{Lake Maggiore site map}
#'
### function produce_site_map
produce_site_map <-
function(deimsid,
countryCode,
listOfSites,
gridNx,
gridNy,
width = 0.25,
height = 0.25,
bboxXMin = 0,
bboxXMax = 0,
bboxYMin = 0,
bboxYMax = 0,
show_map = FALSE) {
deimsbaseurl <- get_deims_base_url()
deimsidExa <- sub("^.+/", "", deimsid)
q <- '{title: .title,
uri: "\\(.id.prefix)\\(.id.suffix)",
boundaries: .attributes.geographic.coordinates
}'
jj <- get_id(deimsid, "sites")
if (is.na(attr(jj, "status"))) {
invisible(
utils::capture.output(
coordinates <- dplyr::as_tibble(do_Q(q, jj))
)
)
siteSelected <- sf::as_Spatial(
sf::st_as_sfc(
coordinates$boundaries,
crs = 4326
)
)
}
biomeColor <- tibble::tribble(
~ geoBonBiome,
~ fill,
~ border,
"Marine",
"#055ca8",
"#057ae1",
"Coastal",
"#43903f",
"#5ecc58",
"Fresh water lakes",
"#03a3b8",
"#04d0eb",
"Fresh water rivers",
"#03a3b8",
"#04d0eb",
"Terrestrial",
"#b07c03",
"#e8a303"
)
geoBonBiome <- jsonlite::fromJSON(
paste0(deimsbaseurl,
"api/sites/",
deimsidExa)
)$attributes$environmentalCharacteristics$geoBonBiome
color <- biomeColor$fill[biomeColor$geoBonBiome == geoBonBiome[[1]]]
colorBorder <-
biomeColor$border[biomeColor$geoBonBiome == geoBonBiome[[1]]]
geoBoundaries <- jsonlite::fromJSON(
paste0(deimsbaseurl,
"api/sites/",
deimsidExa)
)$attributes$geographic$boundaries
if (countryCode %in% isoCodes$Alpha_3 == TRUE) {
try({
country <- geodata::gadm(
country = countryCode,
level = 0
) %>%
terra::simplifyGeom(tolerance = 0.01,
preserveTopology = TRUE)
}, silent = TRUE)
if (is.null(geoBoundaries)) {
lterCoords <- siteSelected
lterSitesFeaturePointDEIMS <- sf::st_as_sfc(
lterCoords, crs = 4326
)
baseMap <-
rosm::osm.raster(lterSitesFeaturePointDEIMS, zoomin = -8)
newBaseMap <- raster::reclassify(baseMap, cbind(NA, 255))
mapOfSite <-
tmap::tm_shape(
newBaseMap,
raster.downsample = TRUE
) +
tmap::tm_rgb() +
tmap::tm_shape(lterSitesFeaturePointDEIMS) +
tmap::tm_dots(
size = 1,
shape = 16,
col = color,
title = NA,
legend.show = TRUE
) +
tmap::tm_compass(type = "8star",
position = c("right", "bottom")) +
tmap::tm_scale_bar(position = c("right", "bottom")) +
tmap::tm_layout(
main.title = paste0(jsonlite::fromJSON(
paste0(deimsbaseurl,
"api/sites/",
deimsidExa)
)$title,
"\n",
deimsid),
main.title.position = "center",
main.title.color = "black",
main.title.fontfamily = "sans",
main.title.size = 0.6,
legend.bg.color = "white",
legend.position = c(0.75, 0.9),
legend.width = -0.24
) +
tmap::tm_credits(
"Leaflet | © OpenStreetMap contributors -
https://www.openstreetmap.org/",
size = 0.3,
fontfamily = "sans",
position = c("left", "bottom")
) +
tmap::tm_basemap(leaflet::providers$Stamen.Watercolor)
if (exists("country")) {
mapOfCentroids <- tmap::tm_shape(country) +
tmap::tm_borders("grey75", lwd = 1) +
tmap::tm_shape(listOfSites) +
tmap::tm_dots(
col = NA,
size = 0.01,
shape = 16,
title = NA,
legend.show = FALSE
) +
tmap::tm_shape(siteSelected) +
tmap::tm_dots(
col = color,
size = 0.1,
shape = 16,
title = NA,
legend.show = FALSE
)
}
# based on the value of show_map param
if (show_map == TRUE) {
if (exists("mapOfCentroids")) {
print(mapOfSite)
print(mapOfCentroids,
vp = grid::viewport(gridNx,
gridNy,
width = width,
height = height))
} else {
print(mapOfSite)
}
} else {
return(mapOfSite)
return(mapOfCentroids,
vp = grid::viewport(gridNx,
gridNy,
width = width,
height = height))
}
} else {
geoBoundaries_sf <- sf::st_as_sfc(geoBoundaries)
sf::st_crs(geoBoundaries_sf) <- sf::st_crs(4326)
lterSitesFeatureDEIMS <-
sf::as_Spatial(geoBoundaries_sf, )
bboxlterItalySitesFeature <- sf::st_bbox(lterSitesFeatureDEIMS)
bboxlterItalySitesFeature[1] <-
sf::st_bbox(lterSitesFeatureDEIMS)[1] +
bboxXMin
bboxlterItalySitesFeature[3] <-
sf::st_bbox(lterSitesFeatureDEIMS)[3] +
bboxXMax
bboxlterItalySitesFeature[2] <-
sf::st_bbox(lterSitesFeatureDEIMS)[2] +
bboxYMin
bboxlterItalySitesFeature[4] <-
sf::st_bbox(lterSitesFeatureDEIMS)[4] +
bboxYMax
baseMap <- rosm::osm.raster(bboxlterItalySitesFeature)
newBaseMap <- raster::reclassify(baseMap, cbind(NA, 255))
mapOfSite <-
tmap::tm_shape(newBaseMap) +
tmap::tm_rgb() +
tmap::tm_shape(lterSitesFeatureDEIMS) +
if (class(lterSitesFeatureDEIMS)[1] == "SpatialLines") {
tmap::tm_lines(col = color)
} else {
tmap::tm_borders(col = colorBorder) +
tmap::tm_fill(col = color, alpha = 0.5)
} +
tmap::tm_compass(type = "8star",
position = c("right", "bottom")) +
tmap::tm_scale_bar(position = c("right", "bottom")) +
tmap::tm_layout(
main.title = paste0(
jsonlite::fromJSON(paste0(
deimsbaseurl,
"api/sites/",
substring(deimsid,
19)
))$title,
"\nDEIMS ID ",
deimsid
),
main.title.position = "center",
main.title.color = "black",
main.title.fontfamily = "sans",
main.title.size = 0.7,
legend.bg.color = "white",
legend.position = c(0.75, 0.9),
legend.width = -0.24
) +
tmap::tm_credits(
"Leaflet | © OpenStreetMap contributors -
https://www.openstreetmap.org/",
size = 0.5,
fontfamily = "sans",
position = c("left", "bottom")
) +
tmap::tm_basemap(leaflet::providers$Stamen.Watercolor)
if (exists("country")) {
mapOfCentroids <- tmap::tm_shape(country) +
tmap::tm_borders("grey75", lwd = 1) +
tmap::tm_shape(listOfSites) +
tmap::tm_dots(
col = NA,
size = 0.01,
shape = 16,
title = NA,
legend.show = FALSE
) +
tmap::tm_shape(siteSelected) +
tmap::tm_dots(
col = color,
size = 0.1,
shape = 16,
title = NA,
legend.show = FALSE
)
}
if (show_map == TRUE) {
if (exists("mapOfCentroids")) {
print(mapOfSite)
print(mapOfCentroids,
vp = grid::viewport(
gridNx,
gridNy,
width = width,
height = height
)
)
} else {
print(mapOfSite)
}
} else {
return(mapOfSite)
return(mapOfCentroids,
vp = grid::viewport(gridNx,
gridNy,
width = width,
height = height))
}
}
} else {
message(
"\n----\nThe map of site cannot be made properly.
Please check again the Country code.
Compare the code provided with the list of code in Wikipage
https://en.wikipedia.org/wiki/ISO_3166\n----\n"
)
mapOfCentroids <- NULL
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.