Skip to content
Menu
Hilltop Analytics
  • Sections
    • Football
  • About
  • Members
Hilltop Analytics

5 minutes to Hawaii in 3D with Rayshader and Geoviz

Posted on July 2, 2019July 2, 2019

This one’s a vignette from the geoviz package, but vignettes are easy to miss so I’m putting it on Hilltop Analytics too. A quick geoviz and rayshader code snippet that draws Hawaii with a Stamen Maps overlay.

No API keys needed for this one, run the code below and you’re there!


library(geoviz)
library(rayshader)

# Coordinates for Hawaii
lat = 19.593335
lon = -155.4880287
square_km = 50

# Set max tiles to request from 'mapzen' and 'stamen'.
# Increase this for a higher resolution image.
max_tiles = 10

# Get elevation data. Increase max_tiles for a higher resolution image.
# Set max_tiles = 40 to reproduce the example above.
dem <- mapzen_dem(lat, lon, square_km, max_tiles = max_tiles)

# Get a stamen overlay (or a satellite overlay etc. by changing image_source)
overlay_image <-
  slippy_overlay(dem,
                 image_source = "stamen",
                 image_type = "watercolor",
                 png_opacity = 0.3,
                 max_tiles = max_tiles)

# Render the 'rayshader' scene.
elmat = matrix(
  raster::extract(dem, raster::extent(dem), method = 'bilinear'),
  nrow = ncol(dem),
  ncol = nrow(dem)
)

scene <- elmat %>%
  sphere_shade(sunangle = 270, texture = "bw") %>% 
  add_overlay(overlay_image)  # %>% 

#  For a slower but higher quality render with more realistic shadows (see 'rayshader' documentation)
#  add_shadow(
#    ray_shade(
#      elmat,
#      anglebreaks = seq(30, 60),
#      sunangle = 270,
#      multicore = TRUE,
#      lambert = FALSE,
#      remove_edges = FALSE
#    )
#  ) %>%
#  add_shadow(ambient_shade(elmat, multicore = TRUE, remove_edges = FALSE))

rayshader::plot_3d(
  scene,
  elmat,
  zscale = raster_zscale(dem) / 3,  #exaggerate elevation by 3x 
  solid = TRUE,
  shadow = FALSE,
  soliddepth = -raster_zscale(dem),
  water=TRUE,
  waterdepth = 0,
  wateralpha = 0.5,
  watercolor = "lightblue",
  waterlinecolor = "white",
  waterlinealpha = 0.5
)

rgl::view3d(theta =290, phi = 18, zoom = 0.5, fov = 5)

# Save a picture with depth of field effect
#rayshader::render_depth(
#  focus = 0.3,
#  fstop = 18,
#  filename = "hawaii.png")

share this post

  • Tweet
  • Share
  • LinkedIn
  • Email

Find Me

  • Email
  • GitHub
  • LinkedIn
  • Twitter
Hilltop Analytics Nest Box
See if our resident blue tit is home
Chalkboard
Illustrate and share line-ups, tactics and moves
Wingman
UK Paragliding flight planning and records
Analysts' League Table
When you need more than won/drawn/lost
Gone Paragliding?

Categories

  • Football
  • General
  • Paragliding
  • Player Reports
  • RStats
    • geoviz
  • Uncategorized
©2023 Hilltop Analytics | WordPress Theme by Superbthemes.com