0

I have two rasters, one with land use data (raster A) classified in discrete values that represent different classes (1,2,3,4,5...18). The other one (raster B) contains data that can represent precipitation, temperature, humidity, etc. In both rasters, each pixel has a different value.

Both rasters have the same coordinate system and resolution, although the extent varies. The first raster covers only a patch of the second raster.

I want to intercept both rasters, and where a certain class from raster A with a certain value from raster B intercept, it multiply raster B times a constant value that depends on A. I have these constant values in a CSV file.

For example, if raster A land-use category==7 intercepts raster B where the value of B is equal to 50, then multiply 50 times 0.15. Another example would be, where

raster A == 1
raster B <  50

then

raster C == raster B * 0.46

but, wherever

raster A == 1
raster B > 50 & raster B < 150

raster C == raster B * 0.30

Important to mention is that for the same land-use class, different default values exist that depend on the value range from raster B (as shown in the second part above).

The question is, how can implement such conditional operations in R? I have tried multiplying raster A so values are scaled up (x100) and I can freely manipulate the resulting merge, without a success.

2 Answers 2

0

It is really unclear what you want to do. Please provide some minimal reproducible examples, the code you've tested yet, and the output you expect. Without this information, it is impossible to understand what you want. Here I put a reproducible example so you can tell the community clearly what you want:

library(raster)
#landuse (raster A)
r <- raster(ncol=100,nrow=100,xmn=50, xmx=250,ymn=50,ymx=250)
r[] <- rep(1:10,1000)
e <- extent(r)
plot(r)
#i.e. precipitation (raster B)
s <- raster(ncol=250,nrow=250,xmn=0, xmx=500,,ymn=0,ymx=500)
s[] <- round(runif(62500)*100)
plot(s)
#as you said, the landuse (raster A) is contained in the precipitation (raster B)
plot(e,add=T)

#try to guess what you want, with the number you give
#first crop the raster B on the extent of raster A
cropped <- crop(s,r)
plot(cropped)
#here is the location of cropped raster where the landuse==7 and cropped==50
#the landuse mask
m <- r==7
#precipitation where landuse==7
x <- mask(cropped,m,maskvalue=1,inverse=T)
#precipitation==50 where landuse ==7 multiplied by 0.15
y <- mask(x,cropped==50,maskvalue=1,inverse=T)*0.15
plot(y)

you can transform the code above into a function where the parameters are the landuse classes and the precipitation values (in my example) and then apply in a for loop or with lapply or if you have speed issues with sfLapply from snowfall.

if you provide some data and code we can try to help you more and better.

3
  • Elia, I used the wording 'For example' which means that case is only one. I have 69 land use classes and to each one of them corresponds a different default value that will multiply raster B (precipitation, temperature, or whatever the input is).
    – Daniel
    Apr 16, 2021 at 13:36
  • Yeah I know what "for example" means. What I ve tried to explain is that if you don't give more infos about your problem it is hard to guess what do you want (for example, what kind of output do you expect). Let me know if the dummy data I ve created reproduce your case study. If not, please spend some time to create data for who want to help you.
    – Elia
    Apr 16, 2021 at 13:59
  • I added some extra description. I will update two small samples of the dataset, I am on it. :)
    – Daniel
    Apr 16, 2021 at 14:00
0

Perhaps terra::ifel is what you are looking for

library(terra)
A <- rast(ncol=5, nrow=5)
values(A) <- sample(10, ncell(A), replace=TRUE)

B <- rast(ncol=5, nrow=5)
values(B) <- runif(ncell(B))

x <- ifel(A > 6, B / 10, 
          ifel(A < 3, B * 10, 5))
 

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.