3

I am working with a set of raster layers containing CHIRPS monthly rainfall amounts over Africa. For purposes of reproducing the below code, I staged the decompressed tif files on Dropbox.

Now, let's say I want to crop the stacked images by a given geographic extent. Here's the referring code.

## file import
library(raster)

ch_fls_chirps <- list.files("path/to/tif", pattern = ".tif$", full.names = TRUE)
rst_chirps <- stack(ch_fls_chirps)

## desired extent (xmin, xmax, ymin, ymax)
ext <- extent(c(37, 37.7, -3.4, -2.84))

## cropping (with output storage)
rst_chirps <- crop(rst_chirps, ext, snap = "out", 
                   filename = "path/to/tif/CRP", 
                   bylayer = TRUE, suffix = names(rst_chirps), 
                   format = "GTiff", overwrite = TRUE)

When performing raster::crop, the following error message is thrown:

Error in checkAtAssignment("RasterStack", "legend", ".RasterLegend") :  
‘legend’ is not a slot in class “RasterStack”

By contrast, the code works just fine when output storage is omitted during raster::crop and manually done afterwards.

## cropping (without output storage)
rst_chirps <- crop(rst_chirps, ext, snap = "out")
rst_chirps <- writeRaster(rst_chirps, filename = "path/to/tif/CRP"), 
                          bylayer = TRUE, suffix = names(rst_chirps), 
                          format = "GTiff", overwrite = TRUE)

Can anybody tell me what the issue with direct output storage from within the crop command is all about?

3

That is a bug. I have fixed it in 'raster' version 2.4-21.

3
  • Maybe you should add that you are one of the developers of the raster package.
    – AndreJ
    Sep 18 '15 at 6:03
  • I had the same problem even with version 2.4-21
    – Wraf
    Oct 11 '15 at 19:26
  • Sorry for the late reply. The above code works with raster 2.5-2. Thanks again!
    – fdetsch
    Dec 27 '15 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.