r-spatial / mapview

Interactive viewing of spatial data in R
https://r-spatial.github.io/mapview/
GNU General Public License v3.0
515 stars 91 forks source link

Categorical raster data not displaying in mapshot output, using leaflet 2.2.2 #478

Open dnbucklin opened 5 months ago

dnbucklin commented 5 months ago

Since I've upgraded to leaflet package version 2.2.2, categorical rasters are not displaying in the mapview::mapshot output file:

library(terra)
library(leaflet)
library(mapview)

# Create classified raster
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
rclmat <- matrix(c(0, 200, 1,
                   200, 400, 2,
                   400, 600, 3), ncol=3, byrow=TRUE)
rc1 <- classify(r, rclmat, include.lowest=TRUE)
pal <- rainbow(3)

m <- leaflet::leaflet(options = leaflet::leafletOptions(zoomDelta = 0.5, zoomSnap = 0, attributionControl = FALSE)) %>%
  leaflet::addProviderTiles(leaflet::providers$Esri.WorldImagery, group = "Esri World Imagery") %>%
  leaflet::addRasterImage(rc1, color = pal, group = "category")
m

mapview::mapshot(m, file= "mapshot_image_leaflet_2_2_2.png")

The PNG file shows the basemap only:

image
> sessionInfo()
R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2019 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

time zone: America/New_York
tzcode source: internal

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

other attached packages:
[1] mapview_2.11.2 leaflet_2.2.2  terra_1.7-71  

loaded via a namespace (and not attached):
 [1] utf8_1.2.4              generics_0.1.3          class_7.3-22            KernSmooth_2.23-22      lattice_0.22-5          digest_0.6.35          
 [7] magrittr_2.0.3          evaluate_0.23           grid_4.3.3              fastmap_1.1.1           jsonlite_1.8.8          processx_3.8.4         
[13] e1071_1.7-14            DBI_1.2.2               ps_1.7.6                fansi_1.0.6             crosstalk_1.2.1         scales_1.3.0           
[19] codetools_0.2-19        jquerylib_0.1.4         cli_3.6.2               rlang_1.1.3             units_0.8-5             munsell_0.5.1          
[25] yaml_2.3.8              base64enc_0.1-3         tools_4.3.3             raster_3.6-26           dplyr_1.1.4             colorspace_2.1-0       
[31] webshot_0.5.5           vctrs_0.6.5             R6_2.5.1                png_0.1-8               stats4_4.3.3            proxy_0.4-27           
[37] lifecycle_1.0.4         classInt_0.4-10         leaflet.providers_2.0.0 htmlwidgets_1.6.4       callr_3.7.6             pkgconfig_2.0.3        
[43] pillar_1.9.0            glue_1.7.0              Rcpp_1.0.12             sf_1.0-15               xfun_0.43               tibble_3.2.1           
[49] tidyselect_1.2.1        knitr_1.46              rstudioapi_0.16.0       farver_2.1.1            htmltools_0.5.8         rmarkdown_2.26         
[55] leafem_0.2.3            satellite_1.0.5         compiler_4.3.3          sp_2.1-3                    

Here is what the output file looks like with leaflet 2.2.1:

image
tim-salabim commented 5 months ago

Works for me with mapshot2().

mapshot_image_leaflet_2_2_2

Are you using the latest github version or the latest CRAN version?

dnbucklin commented 5 months ago

Hi @tim-salabim , thanks for getting back to me.

I was using the latest CRAN version for my initial post. I installed the Github version (from the master branch) and tried mapshot again, but got the same result (no raster drawing). With mapshot2 I get the following error:

> mapview::mapshot2(m, file= "mapshot2_image_leaflet_2_2_2-github.png")
Error in sub(file, "", filename) : 
  invalid regular expression 'C:\Users\DAVID_~1\AppData\Local\Temp\2\RtmpMHcEj6\file1ecc30de3782.html', reason 'Invalid back reference'
In addition: Warning message:
In sub(file, "", filename) :
  TRE pattern compilation error 'Invalid back reference'

session info:

> sessionInfo()
R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2019 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

time zone: America/New_York
tzcode source: internal

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

other attached packages:
[1] mapview_2.11.2.9000 leaflet_2.2.2       terra_1.7-71       

loaded via a namespace (and not attached):
 [1] xfun_0.43               raster_3.6-26           htmlwidgets_1.6.4       devtools_2.4.5          remotes_2.5.0           websocket_1.4.1        
 [7] processx_3.8.4          lattice_0.22-5          callr_3.7.6             ps_1.7.6                leaflet.providers_2.0.0 vctrs_0.6.5            
[13] tools_4.3.3             crosstalk_1.2.1         generics_0.1.3          stats4_4.3.3            tibble_3.2.1            proxy_0.4-27           
[19] fansi_1.0.6             pkgconfig_2.0.3         KernSmooth_2.23-22      satellite_1.0.5         webshot_0.5.5           lifecycle_1.0.4        
[25] compiler_4.3.3          farver_2.1.1            stringr_1.5.1           munsell_0.5.1           chromote_0.2.0          codetools_0.2-20       
[31] httpuv_1.6.15           htmltools_0.5.8         usethis_2.2.3           class_7.3-22            yaml_2.3.8              later_1.3.2            
[37] pillar_1.9.0            jquerylib_0.1.4         urlchecker_1.0.1        ellipsis_0.3.2          classInt_0.4-10         cachem_1.0.8           
[43] sessioninfo_1.2.2       mime_0.12               webshot2_0.1.1          tidyselect_1.2.1        digest_0.6.35           stringi_1.8.3          
[49] sf_1.0-15               dplyr_1.1.4             purrr_1.0.2             fastmap_1.1.1           grid_4.3.3              colorspace_2.1-0       
[55] cli_3.6.2               magrittr_2.0.3          base64enc_0.1-3         pkgbuild_1.4.4          utf8_1.2.4              leafem_0.2.3           
[61] e1071_1.7-14            scales_1.3.0            promises_1.2.1          sp_2.1-3                rmarkdown_2.26          png_0.1-8              
[67] evaluate_0.23           memoise_2.0.1           shiny_1.8.1.1           knitr_1.46              miniUI_0.1.1.1          profvis_0.3.8          
[73] rlang_1.1.3             Rcpp_1.0.12             xtable_1.8-4            glue_1.7.0              DBI_1.2.2               pkgload_1.3.4          
[79] jsonlite_1.8.8          rstudioapi_0.16.0       R6_2.5.1                fs_1.6.3                units_0.8-5 
dnbucklin commented 4 months ago

In place of the mapshot2-only call, I was able to create an output PNG with the workaround below. It is similar to what I was getting before from mapshot, but the zoom level isn't the same, and I can't figure out if there is a way to adjust that.

output_file <- "map.png"
html_map <- gsub(".png", ".html", output_file)
m <- mapview::removeMapJunk(m, c("zoomControl", "layersControl", "homeButton", "scaleBar", "drawToolbar", "easyButton", "control"))
mapview::mapshot2(m, url = html_map)
webshot2::webshot(html_map, file = output_file, quiet = T)

So it seems this error from my previous message:

> mapview::mapshot2(m, file= "mapshot2_image_leaflet_2_2_2-github.png")
Error in sub(file, "", filename) : 
  invalid regular expression 'C:\Users\DAVID_~1\AppData\Local\Temp\2\RtmpMHcEj6\file1ecc30de3782.html', reason 'Invalid back reference'

happens only when I specify file in the mapshot2 call. Potentially because of the location of the intermediate html file in that case? Maybe this is a Windows-specific issue.