-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
144 lines (119 loc) · 4.07 KB
/
script.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
# inspiración
# https://x.com/joewdavies/status/1418570318822715397
# paquetes ----------------------------------------------------------------
library(terra)
library(tidyterra)
library(magick)
library(showtext)
library(glue)
library(ggtext)
library(ggplot2)
library(purrr)
# colores & fuentes -------------------------------------------------------
c1 <- "dodgerblue"
c2 <- "violetred"
c3 <- "grey95"
c4 <- "grey90"
font_add(family = "ubuntu", regular = "fuentes/Ubuntu-Regular.ttf")
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <b style='color: {c1};'>Instituto Geográfico Nacional</b>"
)
autor <- glue("<span style='color:{c1};'>**Víctor Gauto**</span>")
icon_twitter <- glue("<span style='font-family:jet;'></span>")
icon_instagram <- glue("<span style='font-family:jet;'></span>")
icon_github <- glue("<span style='font-family:jet;'></span>")
icon_mastodon <- glue("<span style='font-family:jet;'>󰫑</span>")
icon_bsky <- glue("<span style='font-family:jet;'></span>")
usuario <- glue("<span style='color:{c1};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} {icon_instagram} ",
"{icon_mastodon} {icon_bsky} {usuario}"
)
# funciones ---------------------------------------------------------------
# lee vectores y transforma a POSGAR
f_proj <- function(v) {
project(vect(v), "EPSG:5346")
}
# genera figura y almacena
f_gg <- function(x) {
if (x < 10) {
i <- paste0("0", x)
} else {
i <- x
}
g <- ggplot() +
geom_spatvector(data = arg, fill = c4, color = NA) +
geom_spatvector(
data = lista_crop_pro[[x]], aes(color = "pro"), linewidth = .1,
show.legend = TRUE, key_glyph = "path"
) +
geom_spatvector(
data = lista_crop_nac[[x]], fill = NA, aes(color = "nac"), linewidth = .2,
show.legend = TRUE, key_glyph = "path"
) +
scale_color_manual(
breaks = c("nac", "pro"),
labels = c("Ruta Nacional", "Ruta Provincial"),
values = c(c1, c2)
) +
labs(color = NULL, caption = mi_caption) +
guides(
color = guide_legend(override.aes = list(linewidth = 1))
) +
theme_void() +
theme(
plot.background = element_rect(fill = c3, color = NA),
plot.caption = element_markdown(
family = "ubuntu", size = 8, color = c2, lineheight = 1.2,
margin = margin(b = 5)
),
legend.text = element_text(family = "ubuntu"),
legend.position = "inside",
legend.position.inside = c(.7, .3),
legend.key.width = unit(20, "pt")
)
ggsave(
plot = g,
filename = paste0("crecimiento_rutas/fig/", i, ".png"),
width = 1000,
height = 2084,
units = "px"
)
print(glue::glue("\n\n--- Figura {i} generada ---\n\n"))
}
# datos -------------------------------------------------------------------
# límites de Argentina, continental
arg <- f_proj("extras/arg_continental.gpkg")
# rutas nacionales, provinciales y terciarias
r_nac <- f_proj("ign_red_vial/LíneaRed vial nacional.json")
r_pro <- f_proj("ign_red_vial/LíneaRed vial provincial.json")
# coordenadas del obelisco, origen de la expansión
o <- vect(
data.frame(x = -58.38162, y = -34.60376), geom = c("x", "y"),
crs = "EPSG:4326"
) |>
project("EPSG:5346")
# figura ------------------------------------------------------------------
# lista de buffers alrededor del obelisco
# lista de recortes de rutas por cada buffer
lista_buffer_nac <- map(seq(50, 2500, 25)*1e3, ~buffer(o, .x, quadsegs = 250))
lista_crop_nac <- map(lista_buffer_nac, ~terra::crop(r_nac, .x))
lista_crop_pro <- map(lista_buffer_nac, ~terra::crop(r_pro, .x))
# verifico figuras
f_gg(60)
browseURL("crecimiento_rutas/fig/60.png")
# genero todas las figuras
map(1:length(lista_buffer_nac), f_gg)
# animación ---------------------------------------------------------------
# genera la animación (.mp4) a partir de las figuras (.png)
av::av_encode_video(
input = list.files(
path = "crecimiento_rutas/fig",
full.names = TRUE, pattern = ".png"
),
output = "crecimiento_rutas/viz.mp4"
)