forked from Pecners/kontur_rayshader_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
126 lines (89 loc) · 2.49 KB
/
main.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
library(sf)
library(tigris)
library(tidyverse)
library(stars)
library(rayshader)
library(MetBrewer)
library(colorspace)
# load kontur data
data <- st_read("data/kontur_population_US_20220630.gpkg")
# load states
st <- states()
# filter for florida
florida <- st |>
filter(NAME == "Florida") |>
st_transform(crs = st_crs(data))
# check with map
florida |>
ggplot() +
geom_sf()
# do intersection on data to limit kontur to florida
st_florida <- st_intersection(data, florida)
# define aspect ratio based on bounding box
bb <- st_bbox(st_florida)
bottom_left <- st_point(c(bb[["xmin"]], bb[["ymin"]])) |>
st_sfc(crs = st_crs(data))
bottom_right <- st_point(c(bb[["xmax"]], bb[["ymin"]])) |>
st_sfc(crs = st_crs(data))
# check by plotting points
florida |>
ggplot() +
geom_sf() +
geom_sf(data = bottom_left) +
geom_sf(data = bottom_right, color = "red")
width <- st_distance(bottom_left, bottom_right)
top_left <- st_point(c(bb[["xmin"]], bb[["ymax"]])) |>
st_sfc(crs = st_crs(data))
height <- st_distance(bottom_left, top_left)
# handle conditions of width or height being the longer side
if (width > height) {
w_ratio <- 1
h_ratio <- height / width
} else {
h_ration <- 1
w_ratio <- width / height
}
# convert to raster so we can then convert to matrix
size <- 5000
florida_rast <- st_rasterize(st_florida,
nx = floor(size * w_ratio),
ny = floor(size * h_ratio))
mat <- matrix(florida_rast$population,
nrow = floor(size * w_ratio),
ncol = floor(size * h_ratio))
# create color palette
c1 <- met.brewer("OKeeffe2")
swatchplot(c1)
texture <- grDevices::colorRampPalette(c1, bias = 2)(256)
swatchplot(texture)
# plot that 3d thing!
rgl::rgl.close()
mat |>
height_shade(texture = texture) |>
plot_3d(heightmap = mat,
zscale = 100 / 5,
solid = FALSE,
shadowdepth = 0)
render_camera(theta = -20, phi = 45, zoom = .8)
outfile <- "images/final_plot.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 = FALSE,
lightdirection = 280,
lightaltitude = c(20, 80),
lightcolor = c(c1[2], "white"),
lightintensity = c(600, 100),
samples = 450,
width = 6000,
height = 6000
)
end_time <- Sys.time()
diff <- end_time - start_time
cat(crayon::cyan(diff), "\n")
}