I have two raster:

R1 --> Extension raster ("RB_100" in the picture). This raster is used as expected extension for data and contains just 1 where I need data, and 0 where I don't.

R2 --> Precipitation raster ("Precipitation" in the picture). With values from 1 to 10.

Both rasters are already in the same extension and resolution. What I need is to generate a raster with data of R2 in every pixel where R1 is equal to 1 (grey pixels).

enter image description here

In other words, I need to create a raster where those grey pixels contain the closest value from R2 ("Precipitacion").

  • mmm i'd suggest making a contour for that, then you "mask" it using parentheses in the qgis raster calculator, like "(rb_100 = 1)*precipitation" – Elio Diaz Dec 11 '17 at 19:56

Here are three ways to do that in R. Perhaps that will help you think about a strategy in QGIS (or use the R plugin?)

Example data

r1 <- r2 <- raster(ncol=10, nrow=10)
values(r1) <- sort(rnorm(100, 0, 10))
values(r2) <- sample(10, 100, replace=TRUE)

One approach

m <- mask(r1, r2, maskvalue=1, inverse=TRUE)

Another way

x <- subs(r2, data.frame(old=1, new=1))
y <- r1 * x

And yet another

overlay(r1, r2, fun=function(x, y) {x[y != 1] <- NA; x})
| improve this answer | |
  • Thanks, I will consider to use R to do this kind of calculations but for now I'm not too good at using it with GIS data. Can you figure it out how to do this in the raster calculator tool ? – César Arquero Dec 12 '17 at 7:42

I found a work around to have the job done. This are the steps:

  1. SAGA tool: Close gaps. This will calculate a value for all the pixels without a value. So this... enter image description here ...will turn into this: enter image description here

Sorry about the color display. The key is that all the gray pixels in picture 1 (pixels of R1 that I want to have covered with data) are filled with data in picture 2.

  1. "Clip" with raster calculator. The aim now is to keep just pixels of R2 where R1 is equal to 1. The formula is: (R1>=0)*R2. Raster calculator also allows to refit the output to have same extension and resolution as the Raster Base 1. To do so, just select the raster you want to use as a model and then push the right button "Extension from actual layer". enter image description here Here is the final result: enter image description here

Note: Close Gaps tool performs an interpolation and transforms int values into double. You can correct this (if needed) with r.recode.

| improve this answer | |

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.