tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.05k stars 211 forks source link

plot_3d_problem_swiss_map #223

Closed ludovic1977 closed 2 years ago

ludovic1977 commented 2 years ago

Hello Tyler,

I am testing this wonderful package, unfortunately when executing the plot_3d() command, I get the following message that appears:

Error in plot_3d(., swiss_small, heightmap = swiss, zscale = 1, fov = 0,  : 
  Argument `hillshade` must not contain any entries less than 0 or more than 1

I am using the package version "rayshader" 0.27.4. All packages are up to date.

Here is my code :

> To install the latest version from Github: devtools::install_github("tylermorganwall/rayshader")

> install.packages("devtools") library(rayshader) library(raster)

> Source file, format: ESRI ASCII GRID swiss <- raster("C:/Users/ludovic/Documents/R/swisstopo/MTN25_200m/DHM200.asc")

> Transform source file into a regular R matrix swiss_mat<- raster_to_matrix(swiss)

> Create small version of matrix for quick visual prototyping swiss_small<-` resize_matrix(swiss_mat, 0.25)

> Plot the small version of matrix using basic color defaults swiss_small %>% height_shade() %>% plot_map() image

> Add snow peaks snow_palette = "white" snow_hs = height_shade(swiss_mat, texture = snow_palette)

> Enhance spherical aspect base_map <- swiss_small %>% height_shade() %>% add_overlay(sphere_shade(swiss_small, texture ="imhof1", zscale=7, colorintensity = 10, sunangle=45), alphalayer=0.8) %>% add_shadow(lamb_shade(swiss_small,zscale = 50),0.3) %>% add_shadow(ambient_shade(swiss_small), 0.1) %>% add_shadow(texture_shade(swiss_small,detail=10/10,contrast=9,brightness = 20), 0.01) %>% add_overlay(generate_altitude_overlay(height_shade(swiss_small, texture = "#91aaba"), swiss_small,start_transition =300,end_transition = 500)) %>% add_overlay(generate_altitude_overlay(snow_hs, swiss_small, 2000, 4500, lower=FALSE)) %>% add_shadow(cloud_shade(swiss_small,zscale = 10, start_altitude = 1000, end_altitude = 4000, sun_altitude = 45, attenuation_coef = 2, offset_y = 300, cloud_cover = 0.3, frequency = 0.1, scale_y=3, fractal_levels = 32), 0) %>% plot_map() image

> Plot 3d base_map <- swiss_small %>% plot_3d(swiss_small, heightmap=swiss, zscale = 1,fov = 0, baseshape="rectangle", windowsize=c(1200,800), theta = 135, zoom = 0.30, phi = 45, watercolor=TRUE)

Why can't I plot in 3d? I thank you for your precious help

tylermorganwall commented 2 years ago

You are passing the height map swiss_small instead of the base map to plot_3d() (and assigning the output of plot_3d() to base_map), remove the swiss_small part and it should work.

e.g.

base_map <- swiss_small %>% 
height_shade() %>% 
add_overlay(sphere_shade(swiss_small, texture ="imhof1", zscale=7, colorintensity = 10, sunangle=45), alphalayer=0.8) %>% 
add_shadow(lamb_shade(swiss_small,zscale = 50),0.3) %>% 
add_shadow(ambient_shade(swiss_small), 0.1) %>% 
add_shadow(texture_shade(swiss_small,detail=10/10,contrast=9,brightness = 20), 0.01) %>% 
add_overlay(generate_altitude_overlay(height_shade(swiss_small, texture = "#91aaba"), swiss_small,start_transition =300,end_transition = 500)) %>% 
add_overlay(generate_altitude_overlay(snow_hs, swiss_small, 2000, 4500, lower=FALSE)) %>% 
add_shadow(cloud_shade(swiss_small,zscale = 10, start_altitude = 1000, end_altitude = 4000, sun_altitude = 45, attenuation_coef = 2, offset_y = 300, cloud_cover = 0.3, frequency = 0.1, scale_y=3, fractal_levels = 32), 0) 

base_map %>% 
  plot_3d(swiss_small, heightmap=swiss, zscale = 1,fov = 0, baseshape="rectangle", windowsize=c(1200,800), theta = 135, zoom = 0.30, phi = 45, watercolor=TRUE)
ludovic1977 commented 2 years ago

Thank you very much for your quick response.

When copying your code exactly (e.g.), I have another error message that appears:

Error in if (min(heightmap, na.rm = TRUE) != max(heightmap, na.rm = TRUE)) { : argument is not interpretable as logical In addition: Warning messages: 1: In min(new("RasterLayer", file = new(".RasterFile", name = "C:\Users\ludovic\Documents\R\swisstopo\MTN25_200m\DHM200.asc", : Nothing to summarize if you provide a single RasterLayer; see cellStats 2: In max(new("RasterLayer", file = new(".RasterFile", name = "C:\Users\ludovic\Documents\R\swisstopo\MTN25_200m\DHM200.asc", : Nothing to summarize if you provide a single RasterLayer; see cellStats

Is this due to the Raster format? ``

Source file, format: ESRI ASCII GRID

swiss <- raster("C:/Users/ludovic/Documents/R/swisstopo/MTN25_200m/DHM200.asc") `` Thank you for your precious help!