tylermorganwall / rayshader

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

Adding image to rayshader with add_overlay #306

Closed richardtc closed 9 months ago

richardtc commented 9 months ago

I'm trying to add an image overlay (from a single-band geotiff file) using rayshader to save as a 2d png file.

I've reviewed various documents and content online but cannot get the image to render properly.

I'd be grateful for any pointers.

I have a single-band geotif file (forest cover) which I would like to overlay on a DEM using the add_overlay function.

Everything works fine with the following code except when including the add_overlay(image) layer.

This is the code (on my latest attempt), which gives the following image (pasted after code):

Calculate shade layers

ambmat <- ambient_shade(v_mat, zscale = 50)
raymat <- ray_shade(v_mat, zscale = 50, lambert = TRUE, sunaltitude = 70, sunangle = 115)
watermap <- detect_water(v_mat)

v_mat %>%
  sphere_shade(texture = "imhof2") %>%
  add_water(watermap, color = "imhof2") %>%
  add_shadow(raymat, max_darken = 0.6) %>%
  add_overlay(mos_tr) %>%
  save_png(filename = "2d_watershed_with_overlay_image.png")

Image output: The output shows the cropped area but there is no colour. image

Some notes about data preparation: I am attemping to overlay a cropped out part of the single-band forest layer raster (mos_rm_rgb) on the v_mat (DEM) matrix layer:

I input and converted the DEM geotif to matrix R (in RStudio): v_mat <- raster_to_matrix("dem_0012deg.tif")

Input forest layers:

r2 <- rast("downloads_data/Hansen_GFC-2022-v1.10_treecover2000_20N_090E.tif")
r3 <- rast("downloads_data/Hansen_GFC-2022-v1.10_treecover2000_20N_100E.tif")

Mosaic files:

rlist <- list(r2,r3)
rsrc <- sprc(rlist)
mos_tc <- mosaic(rsrc)

Crop out the forest layer:

Read in watershed raster for setting extent
cprb <- rast("dem_0012deg.tif")
e <- ext(cprb)
mos_tc_c <- crop(mos_tc, e)

Resample and mask:

mos_rs <- resample(mos_tc_c, cprb)
mos_rm <- mask(mos_rs, cprb)

Save geotif: writeRaster(mos_rm, filename = "mos_rm.tif", overwrite=TRUE)

Using QGIS (to symbolise and export a 3-band tif file) the single-band geotiff file is converted to a 3-band geotif file. From the add-overlay documentation, I see that a 3 or 4-band RGB matrix is needed. I also tried adding a matrix based on a 3-band raster without success (using GIMP to remove the alpha band).

Read 4-band geotif file into R: mos_rm_rgb <- rast("mos_rm_rgb_noalpha.tif")

Convert the forest SpatRaster to a matrix object: mos_mat <- raster_to_matrix(mos_rm_rgb)

I removed negative values in v_mat which seemed to giving an error when running the above rayshader code. v_mat_nas[v_mat_nas < 0] <- 0

I also transposed the forest layer as the image is distorted (and just black and white). mos_tr <- t(mos_mat_rs)

Session info 
 setting  value
 version  R version 4.3.2 (2023-10-31)
 os       Ubuntu 22.04.3 LTS
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 date     2024-01-27
 rstudio  2023.09.1+494 Desert Sunflower (server)
 pandoc   3.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages 
 package       * version   date (UTC) lib source
 abind         * 1.4-5     2016-07-21 [1] RSPM (R 4.3.0)
 base64enc       0.1-3     2015-07-28 [1] RSPM (R 4.3.0)
 bookdown        0.36      2023-10-16 [1] RSPM (R 4.3.1)
 cachem          1.0.8     2023-05-01 [1] RSPM (R 4.3.0)
 class           7.3-22    2023-05-03 [1] RSPM (R 4.3.0)
 classInt        0.4-10    2023-09-05 [1] RSPM (R 4.3.0)
 cli             3.6.2     2023-12-11 [1] RSPM (R 4.3.0)
 codetools       0.2-19    2023-02-01 [1] RSPM (R 4.3.0)
 colorspace      2.1-0     2023-01-23 [1] RSPM (R 4.3.0)
 crayon          1.5.2     2022-09-29 [1] RSPM (R 4.3.0)
 data.table      1.14.10   2023-12-08 [1] RSPM (R 4.3.0)
 DBI             1.2.0     2023-12-21 [1] RSPM (R 4.3.0)
 devtools      * 2.4.5     2022-10-11 [1] RSPM (R 4.3.0)
 digest          0.6.34    2024-01-11 [1] RSPM (R 4.3.0)
 doParallel      1.0.17    2022-02-07 [1] RSPM (R 4.3.0)
 dplyr           1.1.4     2023-11-17 [1] RSPM (R 4.3.0)
 e1071           1.7-14    2023-12-06 [1] RSPM (R 4.3.0)
 ellipsis        0.3.2     2021-04-29 [1] RSPM (R 4.3.0)
 evaluate        0.23      2023-11-01 [1] RSPM (R 4.3.0)
 fansi           1.0.6     2023-12-08 [1] RSPM (R 4.3.0)
 fastmap         1.1.1     2023-02-24 [1] RSPM (R 4.3.0)
 foreach         1.5.2     2022-02-02 [1] RSPM (R 4.3.0)
 fs              1.6.3     2023-07-20 [1] RSPM (R 4.3.0)
 gdalUtilities * 1.2.5     2023-08-10 [1] RSPM (R 4.3.0)
 generics        0.1.3     2022-07-05 [1] RSPM (R 4.3.0)
 glue            1.7.0     2024-01-09 [1] RSPM (R 4.3.0)
 hms             1.1.3     2023-03-21 [1] RSPM (R 4.3.0)
 htmltools       0.5.7     2023-11-03 [1] RSPM (R 4.3.0)
 htmlwidgets     1.6.4     2023-12-06 [1] RSPM (R 4.3.0)
 httpuv          1.6.12    2023-10-23 [1] RSPM (R 4.3.0)
 iterators       1.0.14    2022-02-05 [1] RSPM (R 4.3.0)
 jsonlite        1.8.8     2023-12-04 [1] RSPM (R 4.3.0)
 KernSmooth      2.23-22   2023-07-10 [1] RSPM (R 4.3.0)
 knitr           1.45      2023-10-30 [1] RSPM (R 4.3.0)
 later           1.3.1     2023-05-02 [1] RSPM (R 4.3.0)
 lattice         0.22-5    2023-10-24 [1] RSPM (R 4.3.0)
 lifecycle       1.0.4     2023-11-07 [1] RSPM (R 4.3.0)
 lubridate       1.9.3     2023-09-27 [1] RSPM (R 4.3.0)
 magick          2.8.1     2023-10-22 [1] RSPM (R 4.3.0)
 magrittr        2.0.3     2022-03-30 [1] RSPM (R 4.3.0)
 memoise         2.0.1     2021-11-26 [1] RSPM (R 4.3.0)
 mime            0.12      2021-09-28 [1] RSPM (R 4.3.0)
 miniUI          0.1.1.1   2018-05-18 [1] RSPM (R 4.3.0)
 munsell         0.5.0     2018-06-12 [1] RSPM (R 4.3.0)
 osmdata       * 0.2.5.005 2023-12-08 [1] Github (ropensci/osmdata@3843133)
 pillar          1.9.0     2023-03-22 [1] RSPM (R 4.3.0)
 pkgbuild        1.4.3     2023-12-10 [1] RSPM (R 4.3.0)
 pkgconfig       2.0.3     2019-09-22 [1] RSPM (R 4.3.0)
 pkgload         1.3.3     2023-09-22 [1] RSPM (R 4.3.0)
 png           * 0.1-8     2022-11-29 [1] RSPM (R 4.3.0)
 prettyunits     1.2.0     2023-09-24 [1] RSPM (R 4.3.0)
 profvis         0.3.8     2023-05-02 [1] RSPM (R 4.3.0)
 progress        1.2.3     2023-12-06 [1] RSPM (R 4.3.0)
 promises        1.2.1     2023-08-10 [1] RSPM (R 4.3.0)
 proxy           0.4-27    2022-06-09 [1] RSPM (R 4.3.0)
 purrr           1.0.2     2023-08-10 [1] RSPM (R 4.3.0)
 R6              2.5.1     2021-08-19 [1] RSPM (R 4.3.0)
 raster          3.6-26    2023-10-14 [1] RSPM (R 4.3.0)
 rayimage      * 0.10.0    2024-01-06 [1] Github (tylermorganwall/rayimage@8497883)
 rayrender     * 0.31.2    2024-01-02 [1] Github (tylermorganwall/rayrender@3b200c4)
 rayshader     * 0.35.7    2023-06-13 [1] RSPM (R 4.3.0)
 RColorBrewer  * 1.1-3     2022-04-03 [1] RSPM (R 4.3.0)
 Rcpp            1.0.12    2024-01-09 [1] RSPM (R 4.3.0)
 remotes         2.4.2.1   2023-07-18 [1] RSPM (R 4.3.0)
 renv            1.0.3     2023-09-19 [1] RSPM (R 4.3.1)
 rgl             1.2.10    2024-01-17 [1] Github (dmurdoch/rgl@d5ac25f)
 rlang           1.1.3     2024-01-10 [1] RSPM (R 4.3.0)
 rmarkdown       2.25      2023-09-18 [1] RSPM (R 4.3.0)
 rstudioapi      0.15.0    2023-07-07 [1] RSPM (R 4.3.0)
 scales          1.3.0     2023-11-28 [1] RSPM (R 4.3.0)
 sessioninfo     1.2.2     2021-12-06 [1] RSPM (R 4.3.0)
 sf            * 1.0-15    2023-12-18 [1] RSPM (R 4.3.0)
 shiny           1.8.0     2023-11-17 [1] RSPM (R 4.3.0)
 sp              2.1-2     2023-11-26 [1] RSPM (R 4.3.0)
 stars         * 0.6-4     2023-09-11 [1] RSPM (R 4.3.0)
 stringi         1.8.3     2023-12-11 [1] RSPM (R 4.3.0)
 stringr         1.5.1     2023-11-14 [1] RSPM (R 4.3.0)
 suncalc       * 0.5.1     2022-09-29 [1] RSPM (R 4.3.0)
 terra         * 1.7-67    2024-01-01 [1] https://rspatial.r-universe.dev (R 4.3.2)
 tibble          3.2.1     2023-03-20 [1] RSPM (R 4.3.0)
 tidyselect      1.2.0     2022-10-10 [1] RSPM (R 4.3.0)
 timechange      0.2.0     2023-01-11 [1] RSPM (R 4.3.0)
 units           0.8-5     2023-11-28 [1] RSPM (R 4.3.0)
 unix          * 1.5.6     2023-12-04 [1] RSPM (R 4.3.0)
 urlchecker      1.0.1     2021-11-30 [1] RSPM (R 4.3.0)
 usethis       * 2.2.2     2023-07-06 [1] RSPM (R 4.3.0)
 utf8            1.2.4     2023-10-22 [1] RSPM (R 4.3.0)
 vctrs           0.6.5     2023-12-01 [1] RSPM (R 4.3.0)
 xfun            0.41      2023-11-01 [1] RSPM (R 4.3.0)
 xtable          1.8-4     2019-04-21 [1] RSPM (R 4.3.0)
 yaml            2.3.8     2023-12-11 [1] RSPM (R 4.3.0)
richardtc commented 9 months ago

I resolved this by converting the forest layer to PNG from the TIF file (in GIMP) and modified the add_overlay layer as:

add_overlay(mos_rm_rgb_png, alphalayer = 0.6) %>%