/
working_imagery.Rmd
273 lines (210 loc) · 6.71 KB
/
working_imagery.Rmd
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
title: "Working with imagery"
author: <a href="https://orcid.org/0000-0001-8457-4658">Diego Hernangómez <img alt="ORCID logo" src="https://info.orcid.org/wp-content/uploads/2019/11/orcid_24x24.png" width="16" height="16" /></a>
description: >
Learn how to include images on your maps.
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
warning = FALSE,
message = FALSE,
dpi = 300,
tidy = "styler",
dev = "ragg_png",
out.width = "100%"
)
```
**mapSpain** provides a powerful interface for working with imagery.
**mapSpain** can download static files as `.png` or `.jpeg` files (depending on
the Web Map Service) and use them along your shapefiles.
**mapSpain** also includes a plugin for **R**
[leaflet](https://rstudio.github.io/leaflet/) package, that allows you to
include several basemaps on your interactive maps.
The services are implemented via the leaflet plugin
[leaflet-providersESP](dieghernan.github.io/leaflet-providersESP/). You can
check a display of each provider on the previous link.
## Static tiles
An example of how you can include several tiles to create a static map. We focus
here on layer provided by La Rioja's [Infraestructura de Datos Espaciales
(IDERioja)](https://www.iderioja.larioja.org/).
**When working with imagery, it is important to set `moveCAN = FALSE`, otherwise
the images for the Canary Islands won't be accurate.**
```{r static1}
library(mapSpain)
library(sf)
library(ggplot2)
library(tidyterra)
# Logroño
lgn_borders <- esp_get_munic(munic = "Logroño")
# Convert to Mercator (EPSG:3857) as a general advice when working with tiles
lgn_borders <- st_transform(lgn_borders, 3857)
tile_lgn <- esp_getTiles(lgn_borders, "IGNBase.Todo", bbox_expand = 0.5)
ggplot(lgn_borders) +
geom_spatraster_rgb(data = tile_lgn) +
geom_sf(fill = NA, linewidth = 2, color = "blue")
```
### Alpha value on tiles
Some tiles could be loaded with or without an alpha value, that controls the
transparency of the object:
```{r static2}
madrid <- esp_get_ccaa("Madrid", epsg = 3857)
# Example without transparency
basemap <- esp_getTiles(madrid, "IGNBase.Gris", zoommin = 1)
tile_opaque <- esp_getTiles(madrid, "UnidadesAdm.Limites", transparent = FALSE)
ggplot() +
geom_spatraster_rgb(data = basemap) +
geom_spatraster_rgb(data = tile_opaque) +
theme_void()
```
Now let's check the same code using the `tranparent = TRUE` option:
```{r static_transp}
# Example with transparency
tile_alpha <- esp_getTiles(madrid, "UnidadesAdm.Limites", transparent = TRUE)
# Same code than above for plotting
ggplot() +
geom_spatraster_rgb(data = basemap) +
geom_spatraster_rgb(data = tile_alpha) +
theme_void()
```
Now the two tiles overlaps with the desired alpha value.
### Masking tiles
Another nice feature is the ability of masking the tiles, so more advanced maps
can be plotted:
```{r static3}
rioja <- esp_get_prov("La Rioja", epsg = 3857)
basemap <- esp_getTiles(rioja, "IGNBase.Gris", bbox_expand = 0.1, zoommin = 1)
masked <- esp_getTiles(rioja, "IGNBase.Todo", mask = TRUE, zoommin = 1)
ggplot() +
geom_spatraster_rgb(data = basemap, maxcell = 10e6) +
geom_spatraster_rgb(data = masked, maxcell = 10e6)
```
## Dynamic maps with Leaflet
**mapSpain** provides a plugin to be used with the `leaflet` package. Here you
can find some quick examples:
### Earthquakes in Tenerife (last year)
```{r leaflet1}
library(leaflet)
tenerife_leaf <- esp_get_nuts(
region = "Tenerife", epsg = 4326,
moveCAN = FALSE
)
bbox <- as.double(round(st_bbox(tenerife_leaf) + c(-1, -1, 1, 1), 2))
# Start leaflet
m <- leaflet(tenerife_leaf,
elementId = "tenerife-earthquakes",
width = "100%", height = "60vh",
options = leafletOptions(minZoom = 9, maxZoom = 18)
)
# Add layers
m <- m %>%
addProviderEspTiles("IDErioja.Relieve") %>%
addPolygons(color = NA, fillColor = "red", group = "Polygon") %>%
addProviderEspTiles("Geofisica.Terremotos365dias",
group = "Earthquakes"
)
# Add additional options
m %>%
addLayersControl(
overlayGroups = c("Polygon", "Earthquakes"),
options = layersControlOptions(collapsed = FALSE)
) %>%
setMaxBounds(bbox[1], bbox[2], bbox[3], bbox[4])
```
### Population density in Spain
A map showing the population density of Spain as of 2019:
```{r leaflet2}
munic <- esp_get_munic_siane(
year = 2019,
epsg = 4326,
moveCAN = FALSE,
rawcols = TRUE
)
# Get area in km2 from siane munic
# Already on the shapefile
munic$area_km2 <- munic$st_area_sh * 10000
# Get population
pop <- mapSpain::pobmun19
# Paste
munic_pop <- merge(munic, pop[, c("cmun", "cpro", "pob19")],
by = c("cmun", "cpro"),
all.x = TRUE
)
munic_pop$dens <- munic_pop$pob19 / munic_pop$area_km2
munic_pop$dens_label <- prettyNum(round(munic_pop$dens, 2),
big.mark = ".",
decimal.mark = ","
)
# Create leaflet
bins <- c(0, 10, 25, 100, 200, 500, 1000, 5000, 10000, Inf)
pal <- colorBin("inferno", domain = munic_pop$dens, bins = bins, reverse = TRUE)
labels <- sprintf(
"<strong>%s</strong><br/><em>%s</em><br/>%s pers. / km<sup>2</sup>",
munic_pop$rotulo,
munic_pop$ine.prov.name,
munic_pop$dens_label
) %>% lapply(htmltools::HTML)
leaflet(elementId = "SpainDemo", width = "100%", height = "60vh") %>%
setView(lng = -3.684444, lat = 40.308611, zoom = 5) %>%
addProviderEspTiles("IGNBase.Gris") %>%
addPolygons(
data = munic_pop,
fillColor = ~ pal(dens),
fillOpacity = 0.6,
color = "#44444",
weight = 0.5,
smoothFactor = .1,
opacity = 1,
highlightOptions = highlightOptions(
color = "white",
weight = 1,
bringToFront = TRUE
),
popup = labels
) %>%
addLegend(
pal = pal, values = bins, opacity = 0.7,
title = "<small>Pop. Density km<sup>2</sup></small><br><small>(2019)</small>",
position = "bottomright"
)
```
## Providers available
The list `esp_tiles_providers` includes the data of the available providers you
can use on functions described above. This list includes all the parameters
needed to replicate the API request. See the static url of each provider:
```{r providers, echo=FALSE}
library(reactable)
prov_names <- names(esp_tiles_providers)
urls <- vapply(esp_tiles_providers, function(x) {
# Create url
allpars <- x$static
q <- allpars$q
rest <- modifyList(
allpars,
list(
attribution = NULL,
q = NULL
)
)
rest_temp <- paste0(names(rest), "=", rest, collapse = "&")
templurl <- paste0(q, rest_temp)
if (grepl("rioja", q, ignore.case = TRUE)) {
templurl <- q
}
templurl
}, FUN.VALUE = character(1))
df <- data.frame(
provider = prov_names,
static_url = urls
)
rownames(df) <- NULL
reactable(df,
filterable = TRUE,
searchable = TRUE,
showPageSizeOptions = TRUE,
striped = TRUE,
paginationType = "jump",
elementId = "leaflet-prov-summ"
)
```