tmap icon indicating copy to clipboard operation
tmap copied to clipboard

Downsampling of rasters not working in certain scenarios

Open ailich opened this issue 2 years ago • 3 comments

I can't seem to create an easy reproducible example, but in some cases when plotting rasters I get a warning that says "Warning message: In fetch(.x, ..., downsample = downsample) : with RasterIO defined, argument downsample is ignored." This can be a bottleneck for large rasters since without downsampling they take a long time to plot and often I need to experiment with the layout a bit to make it look good meaning that I need to plot them several times. Do you know what would be triggering this or how to avoid it?

ailich avatar Jun 10 '22 02:06 ailich

Can you create a reproducible example (easy or not)?

mtennekes avatar Jun 27 '22 20:06 mtennekes

@mtennekes, it seems to happen with large multiband rasters written to disk. Using ignore_file=TRUE seems to be a functional workaround. I also tried using 2 layers as in the multiband example but that wasn't enough to trigger the issue.

library(terra)
#> Warning: package 'terra' was built under R version 4.2.1
#> terra 1.5.48
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(tmap)

r10_mem<- rast(nrows=5000, ncol=5000, nlyrs=10) # 10 layers, in memory
set.seed(5)
values(r10_mem)<- rnorm(ncell(r10_mem)*nlyr(r10_mem))

r1_mem<- r10_mem[[1]] # 1 layer, in memory



writeRaster(r1_mem, "r1_mem.tif", overwrite=TRUE)
writeRaster(r10_mem, "r10_mem.tif", overwrite=TRUE)

r1_file<- rast("r1_mem.tif") # 1 layer, from file
r10_file<- rast("r10_mem.tif") # 10 layers, from file

tm_shape(r1_mem, raster.downsample = TRUE)+
  tm_raster(style = "cont")
#> stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#> Variable(s) "NA" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.


tm_shape(r10_mem, raster.downsample = TRUE)+
  tm_raster(style = "cont")
#> stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#> Variable(s) "NA" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.


tm_shape(r1_file, raster.downsample = TRUE)+
  tm_raster(style = "cont")
#> stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#> Variable(s) "NA" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.


tm_shape(r10_file, raster.downsample = TRUE)+
  tm_raster(style = "cont")
#> Warning in fetch(.x, ..., downsample = downsample): with RasterIO defined,
#> argument downsample is ignored
#> stars_proxy object shown at 5000 by 5000 cells.
#> Variable(s) "NA" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.


tm_shape(st_as_stars(r1_file, ignore_file=TRUE), raster.downsample = TRUE)+
  tm_raster(style = "cont")
#> stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#> Variable(s) "NA" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.


tm_shape(st_as_stars(r10_file, ignore_file=TRUE), raster.downsample = TRUE)+
           tm_raster(style = "cont")
#> stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#> Variable(s) "NA" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

Created on 2022-06-28 by the reprex package (v2.0.1)

ailich avatar Jun 28 '22 15:06 ailich

It seems to work in tmap v4, but it is very slow.

Nowosad avatar Sep 18 '23 09:09 Nowosad