16EAGLE / basemaps

A lightweight package for accessing basemaps from open sources in R 🗺️
https://jakob.schwalb-willmann.de/basemaps
GNU General Public License v3.0
57 stars 15 forks source link

basemap_ggplot() Error in grDevices::rgb(red = df$val1, green ... #19

Closed seaCatKim closed 5 months ago

seaCatKim commented 1 year ago

Loving the basemaps package!

Running into an error when I try and use basemap_ggplot() for a reef I am trying to map. From memory, it was working fine until I read in a saved geotif (basemap_geotif()) with {stars} and then plotted the raster with geom_stars(). Looks like the values have changed from 0:1 to 0:255 potentially?? color intensity is NA?

Thanks in advance!

library(basemaps)
#> Warning: package 'basemaps' was built under R version 4.2.2
data(ext)
basemap_ggplot(ext)
#> Warning: Transforming 'ext' to Web Mercator (EPSG: 3857), since 'ext' has a
#> different CRS. The CRS of the returned basemap will be Web Mercator, which is
#> the default CRS used by the supported tile services.
#> Loading basemap 'terrain' from map service 'osm_stamen'...

basemap(ext)
#> Warning: Transforming 'ext' to Web Mercator (EPSG: 3857), since 'ext' has a
#> different CRS. The CRS of the returned basemap will be Web Mercator, which is
#> the default CRS used by the supported tile services.
#> Loading basemap 'terrain' from map service 'osm_stamen'...

library(sf)
#> Warning: package 'sf' was built under R version 4.2.2
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

set_defaults(map_service = "esri", map_type = "world_imagery")
t_bbox <- st_bbox(c(xmin = 119.8, xmax = 119.85, ymax = 8.82, ymin = 8.72), crs = st_crs(4326))

basemap(t_bbox)
#> Warning: Transforming 'ext' to Web Mercator (EPSG: 3857), since 'ext' has a
#> different CRS. The CRS of the returned basemap will be Web Mercator, which is
#> the default CRS used by the supported tile services.
#> Loading basemap 'world_imagery' from map service 'esri'...

basemap_ggplot(ext = t_bbox)
#> Warning: Transforming 'ext' to Web Mercator (EPSG: 3857), since 'ext' has a
#> different CRS. The CRS of the returned basemap will be Web Mercator, which is
#> the default CRS used by the supported tile services.
#> Loading basemap 'world_imagery' from map service 'esri'...
#> Error in grDevices::rgb(red = df$val1, green = df$val2, blue = df$val3, : color intensity NA, not in [0,1]
sessionInfo()
#> R version 4.2.1 (2022-06-23 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8   
#> [3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C                      
#> [5] LC_TIME=English_Australia.utf8    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.31   withr_2.5.0     lifecycle_1.0.3 reprex_2.0.2   
#>  [5] evaluate_0.20   rlang_1.0.6     cli_3.4.1       rstudioapi_0.14
#>  [9] fs_1.5.2        rmarkdown_2.20  tools_4.2.1     glue_1.6.2     
#> [13] xfun_0.37       yaml_2.3.7      fastmap_1.1.0   compiler_4.2.1 
#> [17] htmltools_0.5.4 knitr_1.42

Created on 2023-02-10 with reprex v2.0.2

bmaxted commented 1 year ago

Having the same issue with basemap_gglayer() trying to plot esri world_imagery. Looks like there are NA values in the SpatRaster object that basemap() generates, which gg_raster() is therefore unable to plot. The same map_type and ext were working fine for me two days ago, so presumably this has happened because the esri world_imagery data has been updated in some way, and the issue may well correct itself if it's updated again? It would be helpful though if basemap()could automatically convert NA values to zero e.g. map[is.na(map)]<-0 when class = "ggplot" or "gglayer".

Bevann commented 1 year ago

This has also broken some of my workflows wit esri layers and osm topographic, osm stamen still seems to work fine

andrewdigby commented 1 year ago

I'm also loving basemaps, but am having the same problem. None of the map_service='osm' options are working for me.

irhoppe commented 1 year ago

In case anyone comes looking for a workaround, you can first download the basemap as a raster, then plot that in ggplot:

library(ggplot2)
library(basemaps)
library(raster)

data(ext)

m1 <- basemap_gglayer(ext, "esri", "world_imagery", map_res=1)     # low res/blurry

ggplot() + 
  m1 + 
  scale_fill_identity() + 
  coord_sf()

m2 <- basemap_raster(ext, "esri", "world_imagery", map_res=1)   # hi res

m2_xy <- as.data.frame(m2, xy=TRUE)

ggplot() + 
  geom_raster(data=m2_xy, 
              aes(x=x, y=y, 
                  fill=rgb(red=red, green=green, blue=blue, maxColorValue=255))) + 
  scale_fill_identity(guide="none") + 
  coord_sf()
rikudoukarthik commented 1 year ago

@irhoppe's solution worked for me for 2-3 days, but has now broken as well! The basemap_raster() call now produces some NAs, so I suppose the only solution is to manually remove NAs (rationale here).

16EAGLE commented 6 months ago

This should be solved with PR #20 by @robertomrosati.

@seaCatKim on my side, your code now runs without the error you reported. Please try whether the error persists after installing the most recent version 0.0.6 of basemaps from CRAN. Thanks!

16EAGLE commented 5 months ago

Please reopen if this issue can be still encountered with the latest CRAN release of basemaps.