Geographic Information Systems Asked on June 17, 2021
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.
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.
Answered by Elia on June 17, 2021
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))
Answered by Robert Hijmans on June 17, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP