/
produce_network_points_map.R
149 lines (148 loc) · 5.26 KB
/
produce_network_points_map.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#' Provide a map (image) of sites in an eLTER Network.
#' @description `r lifecycle::badge("stable")`
#' Return a image map object of all of the eLTER sites belonging
#' to an eLTER Network (e.g.
#' \href{https://deims.org/networks/7fef6b73-e5cb-4cd2-b438-ed32eb1504b3}{LTER
#' Italy network}), as a stored into \href{https://deims.org}{DEIMS-SDR}.
#' @param networkDEIMSID A `character`. The DEIMS ID of the network
#' from DEIMS-SDR website. DEIMS ID information
#' \href{https://deims.org/docs/deimsid.html}{here} and Complete list of ILTER
#' networks \href{https://deims.org/search?f[0]=result_type:network}{here}.
#' @param countryCode A `character` following the ISO 3166-1 alpha-3 codes.
#' This ISO convention consists of three-letter country codes as defined in
#' ISO 3166-1. 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 only to the country indicated in this
#' parameter, if the network has a extraterritorial sites those will not
#' represented.
#' @return The output of the function is a `tmap` plot containing an image of
#' geographic distribution of the network of sites present in the chosen
#' country.
#' @author Alessandro Oggioni, phD (2020) \email{oggioni.a@@irea.cnr.it}
#' @importFrom jsonlite fromJSON
#' @importFrom sf as_Spatial st_as_sf st_crs st_is_valid
#' @importFrom tmap tm_shape tm_borders tm_dots
#' @importFrom dplyr select
#' @importFrom tibble as_tibble
#' @importFrom httr RETRY content
#' @importFrom Rdpack reprompt
#' @references
#' \insertRef{jsonliteR}{ReLTER}
#'
#' \insertRef{sfR}{ReLTER}
#'
#' \insertRef{tmapR}{ReLTER}
#'
#' \insertRef{dplyrR}{ReLTER}
#'
#' \insertRef{tibbleR}{ReLTER}
#'
#' \insertRef{httrR}{ReLTER}
#' @export
#' @examples
#' \dontrun{
#' # Italian sites
#' map <- produce_network_points_map(
#' networkDEIMSID =
#' "https://deims.org/network/7fef6b73-e5cb-4cd2-b438-ed32eb1504b3",
#' countryCode = "ITA"
#' )
#' map
#'
#' # German sites
#' map_LTERGermanSites <- produce_network_points_map(
#' networkDEIMSID =
#' "https://deims.org/networks/e904354a-f3a0-40ce-a9b5-61741f66c824",
#' countryCode = "DEU"
#' )
#' map_LTERGermanSites +
#' tmap::tm_compass(type = "8star", position = c("right", "bottom")) +
#' tmap::tm_scale_bar(position = c("right", "bottom"))
#' }
#'
#' @section The function output:
#' \figure{produce_network_points_map_fig.png}{Map of LTER-D Germany sites}
#'
### function produce_network_points_map
produce_network_points_map <- function(networkDEIMSID, countryCode) {
deimsbaseurl <- get_deims_base_url()
# suppressWarnings({
url <- paste0(
deimsbaseurl,
"api/sites?network=",
sub("^.+/", "", networkDEIMSID)
)
export <- httr::RETRY("GET", url = url, times = 5)
lterNetworkSitesCoords <- jsonlite::fromJSON(
httr::content(export, as = "text", encoding = "UTF-8")
)
if (length(lterNetworkSitesCoords) != 0) {
lterNetworkSitesCoords$uri <- paste0(
lterNetworkSitesCoords$id$prefix,
lterNetworkSitesCoords$id$suffix
)
lterNetworkSitesCoords <- lterNetworkSitesCoords %>%
dplyr::select("title", "uri", "changed", "coordinates")
networkSitesGeo <- sf::st_as_sf(
tibble::as_tibble(lterNetworkSitesCoords),
wkt = "coordinates"
)
sf::st_crs(networkSitesGeo) <- 4326
networkSitesGeo_valid <- sf::st_is_valid(
networkSitesGeo
)
if (any(networkSitesGeo_valid)) {
if (countryCode %in% isoCodes$Alpha_3 == TRUE) {
try({
country <- geodata::gadm(country = countryCode, level = 0) %>%
terra::simplifyGeom(tol = 0.01, topologyPreserve = TRUE
)
})
mapOfSites <- if (exists("country")) {
tmap::tm_shape(country) +
tmap::tm_borders("grey75", lwd = 1)
} else {
NULL
}
mapOfSites <- mapOfSites +
tmap::tm_shape(networkSitesGeo) +
tmap::tm_dots(
col = NA,
size = 0.1,
shape = 16,
title = NA,
legend.show = FALSE
)
return(mapOfSites)
} else {
mapOfSites <- tmap::tm_shape(networkSitesGeo) +
tmap::tm_dots(
col = NA,
size = 0.1,
shape = 16,
title = NA,
legend.show = FALSE
)
message("\n----\nThe map of country cannot be created.
Please check again the Country code.
Compare the code provided with the list of code in
https://en.wikipedia.org/wiki/ISO_3166\n----\n")
return(mapOfSites)
}
} else {
message("\n----\nThe maps cannot be created because coordinates,
provided in DEIMS-SDR, have invalid geometry.
Please check the content and refers this error to DEIMS-SDR contact person
of the network, citing the Network.iD.\n----\n")
mapOfSites <- tmap::tm_shape(country) +
tmap::tm_borders("grey75", lwd = 1)
mapOfSites
}
} else {
message("\n----\nThe requested page could not be found.
Please check again the Network.iD\n----\n")
mapOfSites <- NULL
}
# })
}