R raster cover function - bug with tables

I am having an unusual problem with the skin function in R. We are trying to fill cloud pixels with values ​​from another layer. I can get it to work fine with stacks like below:

library(raster)
r1 <- raster(ncols=36, nrows=18)
r1[] <- 1:ncell(r1)
r1b <- r1a <- r1
r1_stack <- stack(r1, r1a, r1b)

r2 <- setValues(r1, runif(ncell(r1)))
r2b <- r2a <- r2
r_stack <- stack(r2, r2a, r2b)

r_stack[r_stack < 0.5] <- NA

r3 <- cover(r_stack, r1_stack)

      

But then I try to do the same with the bitmap stack and I get the error:

Error in as.character(x) : 
 cannot coerce type 'closure' to vector of type 'character'

      

Code:

# get all tifs
LS5_032_032_2008_09_21 <- list.files("LT050340302008090301T1-SC20170526100900/", 
                                     pattern = glob2rx("*band*.tif$"), full.names = T) 


# stack bands
cloudy_scene <- stack(LS5_032_032_2008_09_21)
# import cloud mask
cloud_mask <- raster('LT050340302008090301T1-SC20170526100900/LT05_L1TP_034030_20080903_20160905_01_T1_sr_cloud_qa.tif')

# mask data 
masked_data <- mask(cloudy_scene, mask = cloud_mask, maskvalue=0, inverse=TRUE)

####### get cloud free data
# get files
LS5_2008_09_19 <- list.files("LT050340302008091901T1-SC20170526101124/", 
                             pattern = glob2rx("*band*.tif$"), full.names = T) 

# subset and stack cloud free bands
cloud_free_data <- stack(LS5_2008_09_19)

# use cover function to assign NA pixels to corresponding pixels in other scene
cover <- cover(masked_data, cloud_free_data)

      

TRACEBACK () result:

9: toupper(format)
8: .defaultExtension(format)
7: .getExtension(filename, filetype)
6: .local(x, filename, ...)
5: writeStart(outRaster, filename = filename, format = format, datatype = datatype, 
       overwrite = overwrite)
4: writeStart(outRaster, filename = filename, format = format, datatype = datatype, 
       overwrite = overwrite)
3: .local(x, y, ...)
2: cover(masked_data, cloud_free_data)
1: cover(masked_data, cloud_free_data)

      

UPDATE: I tried to resample the data - still doesn't work

cloud_free_resam <- resample(cloud_free_data, masked_data)
cover <- cover(masked_data, cloud_free_resam)

      

ERROR:

Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'

      

I also tried to crop both layers - same error

# find intersection boundary
crop_extent <- intersect(extent(cloud_free_data), extent(masked_data))
cloud_free_data <- crop(cloud_free_data, crop_extent)
masked_data <- crop(masked_data, crop_extent)

# use cover function to assign NA pixels to corresponding pixels in other scene
cover <- cover(masked_data, cloud_free_data)

      

GET DATA: (WARNING: download 317mb - unpacks to ~ 1 gb) https://ndownloader.figshare.com/files/8561230

Any ideas what might be causing this error with this particular dataset? I'm sure we're missing something pretty basic, but ... what? Thank you in advance.

Leah

+3


source to share


2 answers


This is mistake. A cover art with two Raster * layered objects cannot be written to disc. This can be seen in a simple example by setting

rasterOptions(todisk=TRUE)

      



I fixed this in version 2-6.1 (upcoming)

+1


source


I think it has something to do with when the object is converted to a rasterBrick and the bitmap is converted to a temporary file. those.masked_data <- mask(cloudy_scene, mask = cloud_mask)

Using the Spacedman environment creates "in-memory" rasterBrick objects that do not rely on file access; in this case, the example works without error.

But using the full degree (or trimming at the end of the process), bitmap writing and reversal (temporary) files, and an error occurs.



Perhaps as a temporary fix is ​​to explicitly split the images into memory sized chunks, mask / cover, and then pin with mosaic

.

The extents of the two objects are also slightly different, so they should be corrected. It is also generally a good idea to avoid problems by explicitly specifying lane names, min-max values, and values ​​<0 to NA.

+1


source







All Articles