2

I need to perform an operation on a raster file. My first attempt was with mapcalc in GRASS, but I got lost with the way it handles the 0 values, I am around 0, there migth be many zero values. I am working with double numbers. Maybe, it can be done in QGIS, which will be nice.

In simple words, this is like adding a building on a surface, but limiting it to 2 m and modifying anything if there no building or it is below surface.

The input:

  • Raster1: a regular dtm with some null values in the bounduaries (I could have it in many format)
  • Raster2: a same size and resolution but with a small surface within non null values of the previous raster

The logical operation, raster3 is the result:

if raster1 = null then raster3:=null
else 
  if raster2 = null then raster3:= raster1
  else 
     if raster2 <> null then
       if raster2-raster1>0 and raster2-raster1<2 then
          raster3:=raster2
       else
          if raster2-raster1>=2 then raster3:=raster1+2
          else raster3:= raster1            

It looks that I have a code, but I have never coded anything in GRASS or QGIS

1 Answer 1

2

As I do not have appropriate dataset, I have not tested this ... hopefully you get the idea.

("raster1@1" != -9999) * (("raster2@1" = -9999) * ("raster1@1") + ("raster2@1" != -9999) * (((("raster2@1" - "raster1@1") > 0) * (("raster2@1" - "raster1@1") < 2)) * ("raster2@1") + ((("raster2@1" - "raster1@1") >= 2) * ("raster2@1" + 2.0)) + ((("raster2@1" - "raster1@1") <= 0) * ("raster1@1")))) + ("raster1@1" = -9999) * ("raster1@1")

To test, please copy and paste the above expression into your QGIS Raster Calculator.

("raster1@1" != -9999) * (                   // if raster1 is not null then
    ("raster2@1" = -9999) * ("raster1@1")    // if raster2 is null then take raster1
    +                                        // or
    ("raster2@1" != -9999) * (               // if raster2 is not null then 
        (
        (("raster2@1" - "raster1@1") > 0) * (("raster2@1" - "raster1@1") < 2)   // if delta is gt 0 and lt 2
        )
        * 
        ("raster2@1")                        // then take raster2
        +                                    // else
        (
        ("raster2@1" - "raster1@1" >= 2)     // if delta is equal or gt 2
        *
        ("raster2@1" + 2.0)                  // then take raster2 + 2.0
        )
        +                                    // else
        (
        ("raster2@1" - "raster1@1" <= 0)     // if delta is equal or lt 0
        *
        ("raster1@1")                        // then take raster1
        )
    )
)
+
("raster1@1" = -9999) * ("raster1@1")        // if raster1 is null then return null (i.e. raster1)

As this is assuming your nodata cell is represented by -9999, please modify the number as necessary.

3
  • The silliest question, which language is it? is it a r.mapcalc expression?
    – Marco
    Commented Oct 6, 2017 at 7:24
  • @Marco It is QGIS Raster Calculator. Please remove comments (// and followings) and make it 1-liner (sorry, forgot to mention it).
    – Kazuhito
    Commented Oct 6, 2017 at 7:27
  • @Marco Added one-liner code to my answer. Please test it and let me know what happens. Thanks!
    – Kazuhito
    Commented Oct 6, 2017 at 11:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.