0

I am masking and cropping a rasterstack to a shapefile I made. I have done this previously with another shapefile, this is no problems. However, when I do this with this specific shapefile I get the following error

Error in compareRaster(x) : different extent

I don't fully understand this because I'm comparing a raster to a shapefile and not two rasters. In addition all rasters in the rasterstack have the same extent and I'm masking them to the same shapefile. Finally, I can do the first 8 or so rasters in the raster stack manually, but when I set my loop to the first item, I get the above error. Can someone tell me what is going on?

My code:

praccirc = stack()
for (i in 1:1){
  maskraster = raster::mask(pracstack[[i]], delcirc)
  maskraster = raster::crop(maskraster,delcirc)
  praccirc= stack(janpmdelhi,maskraster)
}
**Error in compareRaster(x) : different extent**

#This works for raster 1 of the raster stack
pracras1 = pracstack[[1]]
pracras1mask = raster::mask(pracras1, delcirc)
pracras1mask = raster::crop(pracras1mask, delcirc)

#additionally this for loop works with this shapefile
pracdel=stack()

for (i in 1:nlayers(pracstack)){
  maskraster = raster::mask(pracstack[[i]], delshp)
  maskraster = raster::crop(maskraster,delshp)
  pracdel= stack(janpmdelhi,maskraster)
}

The Datasets and shapefiles can be found here

1 Answer 1

0

Try nesting so that the mask is performed on the cropped object mask(crop(x, m), m)

1
  • I still get the same error. I also just tried separating the crop and mask into different for loops and the crop for loop works, but the mask for loop fails
    – johnnyg
    Commented Sep 13, 2021 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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