-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDivision-Density.R
More file actions
258 lines (197 loc) · 6.13 KB
/
Division-Density.R
File metadata and controls
258 lines (197 loc) · 6.13 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
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
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
bd_hex <-
st_read("Data/Kontur_Population_20231101.gpkg") %>%
st_transform(3106)
# load population by administrative boundary
bd_admin <-
st_read("Data/Bangladesh_boundaries_20230628.gpkg") %>%
st_transform(3106)
# Checking 'name_en' column in bd_admin data frame
distinct_names <- bd_admin %>%
distinct(name_en)
print(distinct_names)
# Creating Dhaka Division Boundary
# Select any District or Division and use it as a filter for plot only that part of the Map
bd_boundary <-
bd_admin %>%
filter(name_en == 'Dhaka Division') %>% # Filtering Dhaka Only
st_geometry %>%
st_union %>%
st_sf %>%
st_make_valid()
bd_boundary %>%
ggplot()+
geom_sf()
dhaka_hex <- st_intersection(bd_hex, bd_boundary) %>%
st_transform(crs = 3106)
# check the plot
ggplot(dhaka_hex) +
geom_sf(aes(fill = population),
color = "gray66",
linewidth = 0) +
geom_sf(
data = bd_boundary,
fill = NA,
color = "black",
linetype = "dashed",
linewidth = 1
)
# setting the bd boundary as a bounding box
bbox <- st_bbox(bd_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 = 1000 * 2.5
size = 1000 * 3.5
pop_raster <- st_rasterize(
dhaka_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 using MetBrewer
# color <- MetBrewer::met.brewer(name="Morgenstern", direction = 1)
# Create color palette using RColorBrewer
library(RColorBrewer)
color <- brewer.pal(n = 9, name = "BuPu")
# Define the range of colors you want (for example, colors 5 to 10)
# exclude_range <- 7
#exclude_indices <- c(1)
# 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:6])
tx <- grDevices::colorRampPalette(color, bias = 4.5)(256)
swatchplot(tx)
swatchplot(color)
# plotting 3D
# library(rgl)
rgl::close3d()
pop_matrix %>%
height_shade(texture = tx) %>%
plot_3d(heightmap = pop_matrix,
zscale = 250 / 5,
solid = F,
shadowdepth = 0)
# Adjusting Camera Angle
render_camera(theta = -10,
phi = 55,
zoom = 0.55,
fov = 100
)
# To interactively view the 3D plot
# rgl::rglwidget()
outfile <- glue::glue("Plots/Dhaka_Benedictus_47.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 = 90, #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 = 1400,
height = 1580,
samples = 500
#samples = 2
)
end_time <- Sys.time()
diff <- end_time - start_time
cat(crayon::cyan(diff), "\n")
}
# ---------------------------Anotate
# Install and load the showtext package
install.packages("showtext")
library(showtext)
install.packages("extrafont")
library(extrafont)
font_import(pattern = "Philosopher")
pop_raster <- image_read("Plots/final_plot_bd_Benedictus_3.png")
text_color <- darken(subset_colors[3], .4)
swatchplot(text_color)
# Automatically enable font support
showtext_auto()
# Download and register the Philosopher font from Google Fonts
font_add_google("Philosopher", regular = "400", bold = "700")
pop_raster %>%
image_annotate("Bangladesh",
gravity = "northeast",
location = "+50+50",
color = text_color,
size = 120,
font = "Philosopher",
weight = 700,
# degrees = 0,
) %>%
image_annotate("POPULATION DENSITY MAP",
gravity = "northeast",
location = "+50+175",
color = text_color,
size = 28.5,
font = "Philosopher", # Corrected font name
weight = 500,
# degrees = 0,
) %>%
image_annotate("Visualization by: Niloy Biswas with Rayshader\nData: Kontur Population 2023",
gravity = "southwest",
location = "+20+20",
color = alpha(text_color, .8),
font = "Philosopher", # Corrected font name
size = 25,
# degrees = 0,
) %>%
image_write("Plots/Annotated_plot_bd_Benedictus_3.png", format = "png", quality = 100)