0

I have multiple raster objects, with the same resolution but slightly different extents. I have use the following function in order to reclassify them and now their values are between 1-6.

What I want to do, is to generate new rasters based on conditions (e.g. rasterC<-rasterA==6 AND rasterB == 6). I tried a simple ifelse, but I get a reasonable error, with regards to their extents. So my question is:

Is it possible to to use conditionals for rasters with different extents (e.g. only for the areas that overlap?)

#from this function, let's say that I get two different rasters (A and  B) with values 1-6. 
#How can I create a new one with the areas that have the value 6 on both A and B rasters?

my_reclass <-function(s,p) {
  ifelse(s<0 & p<=0.05,1,ifelse(s<0 & (p>0.05 & p<=0.1),2,ifelse(s<0 & p>0.1,3,ifelse(s>0 & p>0.1,4,
  ifelse(s>0 & (p >0.05 & p<=0.1),5,ifelse(s>0 & p <=0.05,6,NA))))))
}


new_raster<-ifelse(A==6 & B==6)

Apologies for not providing a fully reproducible example, but I do not know how to create from scratch rasters with slightly different extents.

  • You can change the extent of a raster by assigning a new extent to it: r=raster(matrix(1:12,3,4)); extent(r)=c(1,2,3,4) which might help you create some examples. – Spacedman Jul 16 at 7:20
  • so what extent should C have? the extent of A? the extent of B? or an extent that takes into account all cells that satisfy the conditions and no more? – Sam Jul 16 at 9:01
  • 1
    edit my above: i just saw you e.g. about areas that only overlap – Sam Jul 16 at 9:08
  • @Spacedman, thank you for your suggestion, but I was just wondering if there is a way to do this with rasters of different extents, only for the areas that overlap. – geo_dd Jul 16 at 9:23
  • 1
    Yes, I was suggesting how to create some rasters with different extents for you to make examples for us, as per your "I do not know how to create from scratch rasters with slightly different extents" – Spacedman Jul 16 at 9:50
1

I'll work with dummy rasters that have been reclassified to 1:6:

require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
r1 <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
r1[] <- sample(1:6, ncell(r1), replace = T)

r2 <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
r2[] <- sample(1:6, ncell(r2), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r3 <- raster(intersect(r1,r2))

# Choose cells from other 2 rasters conditionally (i.e. equal 6 in both case)
# However the other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors
r3[crop(r1,extent(r3))==6 & crop(r2,extent(r3))==6] <- 1

# plot to check
par(mfrow=c(1,3))
plot(r1)
plot(r2)
plot(r3)

also look into raster::reclassify to reclassify rasters quickly.

if you are in a situation where you have lots of conditions to fulfill on only a few rasters, to make lots of new rasters, then crop the rasters to the blank extent first (to new objects) to save computation time.

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.