rayshader icon indicating copy to clipboard operation
rayshader copied to clipboard

`Error in hillshade[, , 2] : subscript out of bounds`

Open matthew-law opened this issue 3 years ago • 0 comments

I've tried to follow the example here with my own data, but have run into a similar error to https://github.com/tylermorganwall/rayshader/issues/46 and https://github.com/tylermorganwall/rayshader/issues/176 running rayshader 0.28.2 with R version 4.2.1.

Full reprex:


temp <- tempfile()
download.file("https://www.dropbox.com/s/bj08h8u0npfb8ew/map_image.tif?dl=1", 
              temp, mode="wb")
map_image <- raster::raster(temp)

temp <- tempfile()
download.file("https://www.dropbox.com/s/hzsrtcezratsg3k/elevation.tif?dl=1", 
              temp, mode="wb")
elevation <- raster::raster(temp)

# double check they're both the same CRS
crs(elevation) # EPSG:3857
crs(map_image) # also EPSG:3857

# get overlapping area of both datasets
image_ext <- terra::as.polygons(ext(map_image)) %>% sf::st_as_sf()
elev_ext <- terra::as.polygons(ext(elevation)) %>% sf::st_as_sf()
crop_area <- st_intersection(elev_ext, image_ext)

# crop both datasets to the same area
map_cropped <- raster::crop(map_image, crop_area)
elevation_cropped <- raster::crop(elevation, crop_area)

# rayshade
map_array = as.array(map_cropped)
elevation_mat <- raster_to_matrix(elevation_cropped)
ray_mat <-ray_shade(elevation_mat, zscale = 3)

(map_array/255) %>%
  add_shadow(ray_mat, 0.3) %>%
  plot_map()
#> Error in hillshade[, , 2]: subscript out of bounds

Created on 2022-09-09 with reprex v2.0.2

dim(map_array) is 2476 3265 1, whereas dim(elevation_mat) is 2053 1554, so I assume this is the root of the problem, but I can't work out how to resize one to fit the other – I've tried a variety of cropping/reprojecting but to no avail. If there are any resources on the best way to get the two images to match I'd be keen to see them!

Full session info

R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6.5

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sf_1.0-8         terra_1.6-7      rayshader_0.28.2 raster_3.5-29    sp_1.5-0        

loaded via a namespace (and not attached):
 [1] styler_1.7.0       progress_1.2.2     xfun_0.32          purrr_0.3.4        lattice_0.20-45    vctrs_0.4.1        htmltools_0.5.3   
 [8] yaml_2.3.5         base64enc_0.1-3    utf8_1.2.2         rlang_1.0.5        R.oo_1.25.0        e1071_1.7-11       pillar_1.8.1      
[15] glue_1.6.2         withr_2.5.0        DBI_1.1.3          R.utils_2.12.0     foreach_1.5.2      R.cache_0.16.0     lifecycle_1.0.1   
[22] R.methodsS3_1.8.2  htmlwidgets_1.5.4  evaluate_0.16      codetools_0.2-18   knitr_1.40         callr_3.7.2        fastmap_1.1.0     
[29] ps_1.7.1           doParallel_1.0.17  parallel_4.2.1     class_7.3-20       fansi_1.0.3        highr_0.9          Rcpp_1.0.9        
[36] KernSmooth_2.23-20 clipr_0.8.0        classInt_0.4-7     jsonlite_1.8.0     fs_1.5.2           hms_1.1.2          digest_0.6.29     
[43] processx_3.7.0     grid_4.2.1         cli_3.4.0          tools_4.2.1        magrittr_2.0.3     rgl_0.109.6        proxy_0.4-27      
[50] tibble_3.1.8       crayon_1.5.1       pkgconfig_2.0.3    ellipsis_0.3.2     prettyunits_1.1.1  reprex_2.0.2       rmarkdown_2.16    
[57] rstudioapi_0.14    iterators_1.0.14   R6_2.5.1           units_0.8-0        compiler_4.2.1  

matthew-law avatar Sep 08 '22 23:09 matthew-law