terra icon indicating copy to clipboard operation
terra copied to clipboard

crop and mask results persist issues

Open Eeveera opened this issue 1 year ago • 4 comments

I am new with these things. So, I stack my layer files into SpatRasters. It goes well. My buffer is a SpatVector. (though the issue persisted when I swapped it to SpatRaster too)

r <- c(x, ...)
names(r)
> (a,b,c,d....)

It goes well until here. I visualize my data. Buffer works too and shows the area correctly after I plot it. But the stacked rasters are of an area much bigger than what I am working with. I plot each stacked raster, and they look fine. They have all their values, etc., correct when I print or head. So I move on to cropping them.

z<-crop(r,y)

I crop, then mask using my SpatRaster as x, and SpatVector as y without any additional settings. it gives

Warning message: 
[crop] could not write categories

then i mask crop output with y. Again get:

anotherfillername <- mask (z,y)

Warning message: 
[mask] could not write categories

And then I plot to check my cropped rasters (a,b,c,d...) and I see that while a is cropped as it should, b is full NA with 0 values, c is NA with values between 0-1 and d is as it should, etc. I checked my extends; they are all identical and crs match. I ran the crop/mask functions using rgdal/raster instead to see if it will have issues but no. Raster crop/mask did everything flawlessly, albeit much slower. However, I cannot continue using rgdal/raster as my latter codes are not compatible with it and instead work with predicts/terra.

Solving this NA issue with terra would save a lot of time and trouble. I tried to use the extend=TRUE command on terra's crop and the result is still the same, with some certain rasters appearing NA after the crop. What could be causing this issue?

Eeveera avatar Feb 18 '24 11:02 Eeveera

This appears to be related to your raster having categories. But I would need a minimal, reproducible example to be able to diagnose and perhaps fix this.

rhijmans avatar Feb 19 '24 07:02 rhijmans

This appears to be related to your raster having categories. But I would need a minimal, reproducible example to be able to diagnose and perhaps fix this.

I see, how can I contact you for it then? If not would trying r <- categorize(r) or r <- asRaster(r) work I wonder 🤔 If those categoriez are related with stacked rasters I might be needing them though because i stacked variables to one to make cropping quick

Eeveera avatar Feb 19 '24 16:02 Eeveera

The standard practice is to add the example here. If for some reason you cannot do that, you can write to r.hijmans at gmail.com

rhijmans avatar Feb 20 '24 16:02 rhijmans

The standard practice is to add the example here. If for some reason you cannot do that, you can write to r.hijmans at gmail.com

I will send an email then

Eeveera avatar Feb 21 '24 10:02 Eeveera