-
Notifications
You must be signed in to change notification settings - Fork 5
/
Maldives-Population-Density.R
194 lines (148 loc) · 4.34 KB
/
Maldives-Population-Density.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
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
install.packages("sf",dependencies=TRUE)
install.packages("tmap",dependencies=TRUE)
install.packages("mapview",dependencies=TRUE)
install.packages("stars",dependencies=TRUE)
install.packages("rayshader",dependencies=TRUE)
install.packages("MetBrewer",dependencies=TRUE)
install.packages("rayrender")
install.packages("extrafont",dependencies=TRUE)
install.packages("magick",dependencies=TRUE)
options(rgl.useNULL = FALSE)
# Packages
require(tidyverse)
require(sf)
require(tmap)
require(ggplot2)
require(mapview)
require(stars)
require(rayshader)
require(MetBrewer)
require(colorspace)
require(rayrender)
require(magick)
require(extrafont)
# Data
# load population 400m H3 hexagon
mv_hex <-
st_read("Data/kontur_population_MaldiVes_20231101.gpkg") %>%
st_transform(3106)
# load population by administrative boundary
mv_admin <-
st_read("Data/kontur_boundaries_MV_20230628.gpkg") %>%
st_transform(3106)
# Checking 'name_en' column in mv_admin data frame
distinct_names <- mv_admin %>%
distinct(name_en)
print(distinct_names)
# Creating MV Boundary
mv_boundary <-
mv_admin %>%
st_geometry %>%
st_union %>%
st_sf %>%
st_make_valid()
# check the boundary plot
ggplot(mv_hex) +
geom_sf(aes(fill = population),
color = "gray66",
linewidth = 0) +
geom_sf(
data = mv_boundary,
fill = NA,
color = "black",
linetype = "dashed",
linewidth = 1
)
# setting the bd boundary as a bounding box
bbox <- st_bbox(mv_boundary)
# finding the aspect ratio
bottom_left <- st_point(c(bbox[["xmin"]], bbox[["ymin"]])) %>%
st_sfc(crs = 3106)
bottom_right <- st_point(c(bbox[["xmax"]], bbox[["ymin"]])) %>%
st_sfc(crs = 3106)
top_left <- st_point(c(bbox[["xmin"]], bbox[["ymax"]])) %>%
st_sfc(crs = 3106)
top_right <- st_point(c(bbox[["xmin"]], bbox[["ymax"]])) %>%
st_sfc(crs = 3106)
width <- st_distance(bottom_left, bottom_right)
height <- st_distance(bottom_left, top_left)
if(width > height) {
w_ratio = 1
h_ratio = height / width
} else {
h_ratio = 1.1
w_ratio = width / height
}
# convert to raster to convert to matrix
# For interactively checking the 3D plot set the size low it'll help to render in real time.
# For saving the 3D image in better Quality change it to higher.
size = 2800
# size = 1000 * 3.5
pop_raster <- st_rasterize(
mv_hex,
nx = floor(size * w_ratio) %>% as.numeric(),
ny = floor(size * h_ratio) %>% as.numeric()
)
pop_matrix <- matrix(pop_raster$population,
nrow = floor(size * w_ratio),
ncol = floor(size * h_ratio))
----------------------------------
# Create color palette from MetBrewer Library
# color <- MetBrewer::met.brewer(name="Benedictus", direction = -1)
library(RColorBrewer)
color <- brewer.pal(n = 12, name = "PiYG")
# Define the range of colors you want to exclude (for example, colors 5 to 10)
# exclude_range <- 7
exclude_indices <- c(7)
# Create a subset of colors excluding the specified indices
subset_colors <- color[-exclude_indices]
# Create a subset of colors excluding the specified range
subset_colors <- color[setdiff(seq_along(color), exclude_range)]
# subset_colors <- color[6:8]
subset_colors <- rev(color[1:11])
tx <- grDevices::colorRampPalette(subset_colors, bias = 4.5)(256)
swatchplot(tx)
swatchplot(subset_colors)
# plotting 3D
# Close any existing 3D plot before plotting another
rgl::close3d()
pop_matrix %>%
height_shade(texture = tx) %>%
plot_3d(heightmap = pop_matrix,
zscale = 250 / 4.5,
solid = T,
shadowdepth = 0.2)
# Adjusting Camera Angle
render_camera(theta = 95,
phi = 45,
zoom = 0.60,
fov = 100
)
# To interactively view the 3D plot
rgl::rglwidget()
outfile <- glue::glue("Plots/MalDives_BuPu2.png")
{
start_time <- Sys.time()
cat(crayon::cyan(start_time), "\n")
if(!file.exists(outfile)) {
png::writePNG(matrix(1), target = outfile)
}
render_highquality(
filename = outfile,
interactive = F,
lightdirection = 230, #Degree
lightaltitude = c(30, 80),
#lightcolor = c(subset_colors[4], "white"),
lightcolor = c("white", "white"), # Set both lights to white
lightintensity = c(600, 100),
# width = 1980,
# height = 1180,
width = 1500,
height = 1080,
samples = 500
#samples = 2
)
end_time <- Sys.time()
diff <- end_time - start_time
cat(crayon::cyan(diff), "\n")
}