0

I have a binary stacked SpatRaster bstacked_fht whose 25 layers correspond to presence/absence data for different forest habitat types. Instead of having only values 0 and 1, I want to replace the value 1 with the respective pixel of another SpatRaster vit_dec only when the value is 0.25. In other words, replace the values only when bstacked_fht[[i]] = 1 and vit_dec = 0.25. Both layers have also NULL values.

print(bstacked_fht)
# class       : SpatRaster 
# dimensions  : 2370, 2513, 25  (nrow, ncol, nlyr)
# resolution  : 100, 100  (x, y)
# extent      : 280329.8, 531629.8, 5578461, 5815461  (xmin, xmax, ymin, ymax)
# coord. ref. : ETRS89 / UTM zone 32N (EPSG:25832) 
# sources     : spat_nx7E84x54c8tnTN_24884.tif  
# spat_nx7E84x54c8tnTN_24884.tif  (17 layers) 
# spat_nx7E84x54c8tnTN_24884.tif  (7 layers) 
# names       : class_1, class_3, class_4, class_5, class_6, class_7, ... 
# min values  :       0,       0,       0,       0,       0,       0, ... 
# max values  :       1,       1,       1,       1,       1,       1, ... 


freq(bstacked_fht)
# layer value count
# 1      1     0 91763
# 2      1     1   524
# 3      2     0 88677
# 4      2     1  3610
# 5      3     0 92281
# 6      3     1     6
# 7      4     0 92281
# 8      4     1     6
# 9      5     0 91933
# 10     5     1   354
# 11     6     0 80878
# 12     6     1 11409
# 13     7     0 92106
# 14     7     1   181
# 15     8     0 89741
# 16     8     1  2546
# 17     9     0 92268
# 18     9     1    19
# 19    10     0 90569
# 20    10     1  1718
# 21    11     0 92245
# 22    11     1    42
# 23    12     0 92141
# 24    12     1   146
# 25    13     0 55803
# 26    13     1 36484
# 27    14     0 92280
# 28    14     1     7
# 29    15     0 69071
# 30    15     1 23216
# 31    16     0 91796
# 32    16     1   491
# 33    17     0 85216
# 34    17     1  7071
# 35    18     0 92105
# 36    18     1   182
# 37    19     0 91956
# 38    19     1   331
# 39    20     0 89634
# 40    20     1  2653
# 41    21     0 91160
# 42    21     1  1127
# 43    22     0 92286
# 44    22     1     1
# 45    23     0 92131
# 46    23     1   156
# 47    24     0 92282
# 48    24     1     5
# 49    25     0 92285
# 50    25     1     2

print(vit_dec)
# class       : SpatRaster 
# dimensions  : 2370, 2513, 1  (nrow, ncol, nlyr)
# resolution  : 100, 100  (x, y)
# extent      : 280329.8, 531629.8, 5578461, 5815461  (xmin, xmax, ymin, ymax)
# coord. ref. : ETRS89 / UTM zone 32N (EPSG:25832) 
# source(s)   : memory
# name        : vitality_highly_decreased_25832 
# min value   :                            0.00 
# max value   :                            0.25 

freq(vit_dec)
#   layer value  count
# 1     1     0 186034

I've attempted various approaches with no success. Something like:

function(bstacked_fht, vit_dec, thresh = 0.5, vit_dec_thresh = 0.20) {
    result <- list()
    
  for (i in 1:25) {
        layer <- bstacked_fht[[i]]
        
        higher <- layer > thresh & vit_layer > vit_dec_thresh
        layer[higher] <- vit_layer[higher]
        
        result[[i]] <- layer
    }
    
    names(result) <- names(bstacked_fht)
    return(result)
}

or

for (i in 1:25) {
    a_layer <- bstacked_fht[[i]]
    
    a_layer[] <- ifelse(a_layer[] > 0.5 & vit_dec[] > 0.2, vit_dec[], a_layer[])
    
    bstacked_fht[[i]] <- a_layer
}

I also tried

mask_bstacked <- bstacked_fht == 1 
mask_vit <- vit_dec == 0.25 
modified_bstacked_fht <- mask(bstacked_fht, mask_bstacked * mask_vit, updatevalue = 0.25)

But when I run freq(modified_bstacked_fht) I still get only values 0 and 1 although when I plot it I get some strange plot with 0,1 and 0.25 values only in some cases.

Perhaps this isn't feasible in a binary stacked raster where only binary values are possible. I'm running low on ideas. Do you have any suggestions on how to do it? Should I convert my bstacked_fht into another type of object?

4
  • 1
    Can you explain why your approaches didn't work? What went wrong with them? And any chance of some code to generate a simple sample data set, maybe just a 10x10x4 raster example?
    – Spacedman
    Commented Aug 11, 2023 at 15:44
  • Thanks for your comment. Basically, I don't get any relevant changes to my SpatRaster (see edited question). Sorry, I'm not sure I got what you are asking with your third question. This (github.com/fabiofebocastelli/systematic-conservation-planning/…) is the link to my repository where everything is explained. Commented Aug 12, 2023 at 10:44
  • We don't have your data so we can't run your code. Even with your data, the raster is 2000x2000ish which is large and hard to see what is happening. By making small test data with known values its much easier to get to grips with how your code is working.
    – Spacedman
    Commented Aug 12, 2023 at 11:11
  • Hi, please if you can give a look at the same discussion here: stackoverflow.com/questions/76884224/… Commented Aug 13, 2023 at 10:10

0

Your Answer

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

Browse other questions tagged or ask your own question.