Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created June 19, 2022 18:52
Show Gist options
  • Save marcosci/db92b6461324804d0c61c2a0c799e75a to your computer and use it in GitHub Desktop.
Save marcosci/db92b6461324804d0c61c2a0c799e75a to your computer and use it in GitHub Desktop.
library(rayshader)
library(rayrender)
library(terra)
library(rnaturalearth)
library(MetBrewer)
library(raster)
library(exactextractr)
library(fasterize)
canopy_germany <- rast("germany/canopy_germany.tif")
canopy_germany <- terra::aggregate(canopy_germany, 10)
germany <- vect(ne_countries(scale = 10, country = "germany"))
canopy_germany <- mask(canopy_germany, germany)
raster_matrix <- raster::raster(canopy_germany) %>%
rayshader::raster_to_matrix(verbose = FALSE)%>%
rayshader::resize_matrix(0.1)
## ----calculate_zscale------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
estimated_zscale <- raster::raster(canopy_germany) %>%
geoviz::raster_zscale()
# this can be scaled to exaggerate elevation
scaled_zscale <- estimated_zscale * 10
base_map <- raster_matrix %>%
height_shade(texture = met.brewer("VanGogh3",n=100)) %>%
add_shadow(texture_shade(raster_matrix, detail = 0.9, brightness = 15), 0.7) %>%
add_shadow(ray_shade(raster_matrix, sunaltitude = 60, zscale = scaled_zscale, multicore = TRUE), 0) |>
add_shadow(ambient_shade(raster_matrix, zscale = scaled_zscale))
plot_map(base_map,
asp = 1/cospi(40/180))
plot_3d(base_map,
raster_matrix,
windowsize = c(900, 1600),
zscale = scaled_zscale / 100,
zoom = 0.75,
phi = 89,
theta = 0,
fov = 0)
render_highquality(filename = "canopy_height_2.png",
samples=200,
width = 6000,
height = 6000 * 1.305407,
lightdirection = c(60,120, 240),
lightaltitude=c(90,25, 12),
lightintensity=c(100, 500, 450),
lightcolor = c("white", "#E7E6C9", "#BDD89E"),
progress=TRUE,
parallel=TRUE)
@nahin29
Copy link

nahin29 commented Jun 19, 2022

Really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment