-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Viz_Urban_Growth.R
More file actions
78 lines (63 loc) · 2.35 KB
/
Copy pathData_Viz_Urban_Growth.R
File metadata and controls
78 lines (63 loc) · 2.35 KB
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
library(sf)
library(dplyr)
library(spData)
library(tmap)
library(magick)
# Message indicating start of Global Visualization Project
message("Initializing Global Urbanization Visualization Project...")
# Filter data for key historical and projected milestones
urb_1970_2030 = urban_agglomerations %>%
filter(year %in% c(1970, 1990, 2010, 2030))
# Static Facet Map
tm_shape(world) +
tm_polygons(col = "#333333", border.col = "black") + # Dark Grey Land
tm_shape(urb_1970_2030) +
tm_symbols(col = "cyan", border.col = "white", size = "population_millions", scale = 0.5) +
tm_facets(by = "year", nrow = 2, free.coords = FALSE) +
tm_layout(bg.color = "black", title.color = "white") # Black Background
#Animation Pipeline
message("Starting Custom Animation Rendering Pipeline...")
# Setup: Define temporal range and output directory
years <- sort(unique(urban_agglomerations$year))
dir.create("frames_temp", showWarnings = FALSE)
file_paths <- character(length(years))
# Execute Rendering Loop
for (i in seq_along(years)) {
y <- years[i]
# Build the Map Frame
map <- tm_shape(world) +
tm_polygons(col = "#262626", border.col = "black") + # Dark Land
tm_shape(urban_agglomerations %>% filter(year == y)) +
# Cyan dots for high contrast visibility
tm_dots(size = "population_millions", col = "cyan", alpha = 0.7) +
tm_layout(
title = paste("Year:", y),
title.position = c("left", "bottom"),
title.color = "white", # White Text
bg.color = "black", # Black Ocean/Background
frame = FALSE
)
# Define Output Path
fname <- file.path("frames_temp", paste0("frame_", sprintf("%03d", i), ".png"))
file_paths[i] <- fname
# Manual Device Control
png(filename = fname, width = 1000, height = 600, res = 100)
print(map)
dev.off()
if(i %% 5 == 0) message(paste("Rendered frame for year:", y))
}
# Frame Stitching (Magick Engine)
if (file.exists(file_paths[1])) {
img_anim <- file_paths %>%
image_read() %>%
image_join() %>%
# CRITICAL FIX: Changed from 4 to 1 for better visibility
image_animate(fps = 1)
# Save Final Artifact
image_write(img_anim, "Global_Urbanization_Shift.gif")
# Remove temporary frames
unlink("frames_temp", recursive = TRUE)
message("SUCCESS: Animation saved as 'Global_Urbanization_Shift.gif'")
} else {
message("ERROR: Rendering failed.")
}