Created
November 5, 2021 12:32
-
-
Save marcosci/5cba144a8bd782d4d8fdec619a56e444 to your computer and use it in GitHub Desktop.
Hexagonal Rayshading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(LaCroixColoR) | |
library(sf) | |
library(fasterize) | |
library(rayshader) | |
library(raster) | |
library(exactextractr) | |
library(rayrender) | |
# load raster into R | |
elevation <- raster("Olympus_Mons_ortho-image.tif") | |
# make hexagonal grid | |
hexbin_vec <- st_make_grid(st_as_sfc(st_bbox(elevation)), cellsize = 25, square = FALSE) | |
# get mean raster values for each hexagon | |
values <- exact_extract(elevation, hexbin_vec, "mean") | |
# turn it into a simple feature and give it values | |
hexbin_vec <- st_sf(geom=hexbin_vec) | |
hexbin_vec$elevation <- values | |
# turn hexbin into raster | |
hexbins <- fasterize(hexbin_vec, | |
elevation, | |
field = "elevation", | |
fun="sum") | |
# and the raster into a rayrender! | |
elevation_matrix <- raster_to_matrix(hexbins) | |
elevation_matrix %>% | |
height_shade(texture = lacroix_palette("Pamplemousse", 256)) %>% | |
add_shadow(ray_shade(elevation_matrix, zscale=2, sunaltitude=25, sunangle=315), 0.3) %>% | |
plot_3d(elevation_matrix, zscale = 0.3, shadow = FALSE, fov = 0, theta = -45, baseshape = "hex") | |
render_highquality(filename = "hexagons_highres.png", | |
samples=500, | |
lightdirection = c(60,120, 240), | |
lightaltitude=c(90,25, 12), | |
lightintensity=c(100, 500, 450), | |
lightcolor = c("white", "#FF9956", "#FF79E7"), | |
progress=TRUE, | |
parallel=TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Image can be found here:
https://www.esa.int/ESA_Multimedia/Images/2008/02/Olympus_Mons_ortho-image