-
Notifications
You must be signed in to change notification settings - Fork 2
/
martin-guerre-map.R
383 lines (298 loc) · 11.9 KB
/
martin-guerre-map.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
## Return of Martin Guerre map ##
# Recreate the map on page 9 of
# Natalie Zemon Davis, *The Return of Martin Guerre*
# (Cambridge: Harvard University Press, 1983)
library(opencage)
library(sf)
library(elevatr)
library(raster)
library(ggplot2)
library(ggrepel)
library(dplyr)
library(rayshader)
library(here)
# Geocode places ----------------------------------------------------------
places <- c("Gimont", "Lombez", "Le Pin", "Sajas", "Pouy-de-Touges", "Rieux",
"Mane", "Toulouse", "St-Ybars", "Le Fossat", "Le Carla", "Pailhès",
"Le Mas d'Azil", "Artigat", "Pamiers", "Foix")
# Bounds to get correct places
xmin <- 0.8
xmax <- 1.75
ymin <- 42.9
ymax <- 43.7
bbox <- c(xmin = xmin, ymin = ymin, xmax = xmax, ymax = ymax)
bbox_oc <- oc_bbox(xmin, ymin, xmax, ymax)
geo_places <- oc_forward_df(placename = places, countrycode = "FR", bounds = bbox)
sf_places <- st_as_sf(geo_places, coords = c("oc_lng", "oc_lat"), crs = 4326)
# Check that locations are correct
mapview::mapview(sf_places)
# Prep location data for plotting -----------------------------------------
# Larger cities in caps
# Highlight Artigat in red
geo_places <- geo_places %>%
mutate(placename = if_else(placename %in% c("Toulouse", "Pamiers", "Foix"),
toupper(placename),
placename),
color = if_else(placename == "Artigat", "red", "black"))
# Elevation data ----------------------------------------------------------
bbox_sf <- st_bbox(bbox, crs = st_crs(4326)) %>%
st_as_sfc()
elev_raster <- get_elev_raster(bbox_sf, z = 10)
# Find actual extent of raster
extent(elev_raster)
# River data: http://gaia.geosci.unc.edu/rivers/
rivers <- st_read(here("data-raw", "europe", "eurivs.shp")) %>%
st_crop(st_bbox(extent(elev_raster)))
# Check bboxes
extent(elev_raster)
st_bbox(rivers_crop)
# Hill shade --------------------------------------------------------------
# Slope and hill shade
slope_raster <- terrain(elev_raster, opt = "slope")
aspect_raster <- terrain(elev_raster, opt = "aspect")
hill_raster <- hillShade(slope_raster, aspect_raster, 40, 270)
# cast raster to data frame for plotting
hill_df <- hill_raster %>%
rasterToPoints() %>%
data.frame()
names(hill_df) <- c("lng", "lat", "hill")
# Plot --------------------------------------------------------------------
ggplot() +
geom_raster(data = hill_df, aes(lng, lat, fill = hill), alpha = 1) +
scale_fill_gradientn(colors = gray.colors(100)) +
geom_sf(data = rivers, aes(size = a_WIDTH), color = "lightblue1") +
scale_size(range = c(0.1, 0.8)) + # river size by width
geom_sf(data = sf_places, size = 1) +
geom_text_repel(data = geo_places, aes(x = oc_lng, y = oc_lat,
label = placename, color = color),
family = "Times New Roman",
size = 3) +
scale_colour_identity() +
coord_sf(xlim = c(xmin, xmax), ylim = c(ymin, ymax), expand = FALSE, datum = NA) +
labs(title = "The Return of Martin Guerre",
subtitle = "Recreating the map from Natalie Zemon Davis' book",
caption = "NZD, The Return of Martin Guerre (1983), pg 9") +
theme_void() +
theme(legend.position = "none",
text = element_text("Times New Roman"))
ggsave("img/martin-guerre-hill-shade-2.png")
# Rayshader ---------------------------------------------------------------
elmat <- raster_to_matrix(elev_raster)
elmat |>
height_shade() |>
plot_3d(elmat, zscale = 10, fov = 0,
theta = 180, # rotation
phi = 45, # viewing angle
zoom = 0.75, windowsize = c(1000, 800))
rgl::rgl.close()
# Calculate rayshade and ambshade
raymat <- ray_shade(elmat)
ambmat <- ambient_shade(elmat)
# Just height_shade
elmat %>%
height_shade() %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
rgl::rgl.close()
# Height and rayshade
elmat %>%
height_shade() %>%
add_shadow(raymat) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
rgl::rgl.close()
# Height and ambient shade
elmat %>%
height_shade() %>%
add_shadow(ambmat, 0.5) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
rgl::rgl.close()
# Height, ambient, and ray
elmat %>%
height_shade() %>%
add_shadow(raymat, 0.7) %>%
add_shadow(ambmat, 0.5) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
# Sphere shade
elmat %>%
sphere_shade() %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
rgl::rgl.close()
# Overlays: Rivers and points ---------------------------------------------
elmat %>%
height_shade() %>%
add_shadow(ambmat, 0.5) %>%
add_overlay(generate_line_overlay(rivers,
extent = attr(elev_raster, "extent"),
heightmap = elmat, linewidth = 3,
color = "lightblue")) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
rgl::rgl.close()
elmat %>%
height_shade() %>%
add_shadow(ambmat, 0.5) %>%
add_overlay(generate_line_overlay(rivers,
extent = attr(elev_raster, "extent"),
heightmap = elmat, linewidth = 3)) %>%
plot_map()
base_map <- elmat %>%
height_shade() %>%
add_shadow(ambmat, 0.5) %>%
add_shadow(raymat, 0.8)
rivers_layer <- generate_line_overlay(rivers,
extent = attr(elev_raster, "extent"),
heightmap = elmat,
linewidth = 10,
data_column_width = "a_WIDTH",
color = "#454b87")
# Points
pts <- data.frame(place = c("Artigat", "Toulouse", "Sajas"),
lat = c(43.13547, 43.60446, 43.37672),
long = c(1.4402561, 1.4442469, 1.0200673))
pts_sf <- st_as_sf(pts, coords = c("long", "lat"), crs = 4326)
pts_layer <- generate_label_overlay(pts_sf,
extent = attr(elev_raster, "extent"),
heightmap = elmat,
data_label_column = "place",
point_size = 4,
text_size = 4)
base_map %>%
add_overlay(rivers_layer, alphalayer = 0.7) %>%
add_overlay(pts_layer) %>%
plot_map()
# Render ------------------------------------------------------------------
# Only need height shade with render_highquality
elmat %>%
height_shade() %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
render_highquality(
"img/nzd_highres.png",
parallel = TRUE, # use parallel processing
samples = 300,
light = FALSE, # use environmental lighting instead
environment_light = "env/phalzer_forest_01_4k.hdr", # hdr file for lighting
intensity_env = 1.5, # how intense the lighting is
rotate_env = 180, # Where light comes from, default is NNE
# Set width and height of image:
# This is where we increase the resolution by increasing the number of points plotted.
width = 3000,
height = 3000
)
rgl::rgl.close()
# With labels
elmat %>%
height_shade() %>%
add_shadow(ambmat, 0.5) %>%
add_overlay(generate_line_overlay(rivers,
extent = attr(elev_raster, "extent"),
heightmap = elmat, linewidth = 10,
data_column_width = "a_WIDTH",
color = "#454b87")) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
render_points(extent = attr(elev_raster, "extent"),
lat = c(43.13547, 43.60446, 43.37672),
long = c(1.4402561, 1.4442469, 1.0200673),
heightmap = elmat,
size = 5,
zscale = 10)
render_label(elmat, lat = 43.13547, long = 1.4402561, text = "Artigat",
extent = attr(elev_raster, "extent"),
zscale = 10, # same zscale as plot3d
altitude = 1000, # Amount above the ground
alpha = 0)
render_label(elmat, lat = 43.60446, long = 1.4442469, text = "Toulouse",
extent = attr(elev_raster, "extent"),
zscale = 10,
altitude = 1000,
alpha = 0)
render_label(elmat, lat = 43.37672, long = 1.0200673, text = "Sajas",
extent = attr(elev_raster, "extent"),
zscale = 10,
altitude = 1000,
alpha = 0)
render_snapshot("img/martin-guerre-3d.png",
title_text = "Return of Martin Guerre",
width = 1000,
height = 1000)
render_highquality(
"nzd_highres_pts.png",
parallel = TRUE, # use parallel processing
samples = 300,
light = FALSE, # use environmental lighting instead
environment_light = "env/phalzer_forest_01_4k.hdr", # hdr file for lighting
intensity_env = 1.5, # how intense the lighting is
rotate_env = 180, # Where light comes from, default is NNE
# Set width and height of image:
# This is where we increase the resolution by increasing the number of points plotted.
width = 3000,
height = 3000,
point_radius = 10,
text_size = 100
)
rgl::rgl.close()
# With points
lat <- c(43.13547, 43.60446, 43.37672)
long <- c(1.4402561, 1.4442469, 1.0200673)
elmat %>%
height_shade() %>%
add_shadow(ambmat, 0.5) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135,
phi = 45, zoom = 0.75, windowsize = c(1000, 800))
render_points(extent = attr(elev_raster, "extent"),
heightmap = elmat,
lat = lat, long = long,
size = 10)
render_highquality(
"img/nzd_highres_pts.png",
parallel = TRUE, # use parallel processing
samples = 300,
light = FALSE, # use environmental lighting instead
environment_light = "env/phalzer_forest_01_4k.hdr", # hdr file for lighting
intensity_env = 1.5, # how intense the lighting is
rotate_env = 180, # Where light comes from, default is NNE
# Set width and height of image:
# This is where we increase the resolution by increasing the number of points plotted.
width = 3000,
height = 3000,
point_radius = 10
)
# Create gif --------------------------------------------------------------
path <- here("img", "martin-guerre-gif")
n_frames <- 60
# frame transition variables
thetavalues <- -90 + 45 * cos(seq(0, 2*pi, length.out = n_frames))
raymat <- ray_shade(elmat)
ambmat <- ambient_shade(elmat)
# generate .png frame images
img_frames <- paste0(path, "martin-guerre",
seq_len(n_frames), ".png")
for (i in seq_len(n_frames)) {
message(paste(" - image", i, "of", n_frames))
elmat %>%
sphere_shade(sunangle = 45, texture = "imhof1") %>%
add_shadow(raymat, 0.5) %>%
add_shadow(ambmat, 0) %>%
plot_3d(elmat, zscale = 10, fov = 0, phi = 45, zoom = 0.75,
theta = thetavalues[i])
render_label(elmat, lat = 43.13547, long = 1.4402561, text = "Artigat",
extent = attr(elev_raster, "extent"),
altitude = 12000, zscale = 50, clear_previous = TRUE)
render_label(elmat, lat = 43.60446, long = 1.4442469, text = "Toulouse",
extent = attr(elev_raster, "extent"),
altitude = 12000, zscale = 50)
render_label(elmat, lat = 43.37672, long = 1.0200673, text = "Sajas",
extent = attr(elev_raster, "extent"),
altitude = 12000, zscale = 50)
render_snapshot(img_frames[i], title_text = "Return of Martin Guerre")
rgl::clear3d()
}
# build gif
magick::image_write_gif(magick::image_read(img_frames),
path = paste0(path, "img/martin-guerre.gif"),
delay = 6/n_frames)