2

I'm trying to combine 2 raster layers using gdal_calc in order to use them in QGIS 3.8.
One of the condition that has to be met could be translated as "if the value of raster A is 1 and the value of raster B is either 10 or 11 or... or 800 (I've got 20 different values or so), then keep the value of raster B".

I managed to achieve what I wanted using several logical_and :

logical_and(A==1,B==10)*B + logical_and(A==1,B==11)*B + ... + logical_and(A==1,B==800)*B

But it's tedious to write and hard to read for other users. Is there a way to get the same result using a more compact syntax ? I tried using

logical_and(A==1,any(B==[1,10,11,12,33,35,41,42,43,44,51,52,53,222,223,224,602,700,702,710,800]))*B and logical_and(A==1,B==any([1,10,11,12,33,35,41,42,43,44,51,52,53,222,223,224,602,700,702,710,800]))*B

But neither of these gave me the same result as my previous lengthy expression...

1 Answer 1

3
+50

a more compact writing could be

B*(A==1)*((B==10)+(B==11)+...+(B==800))

but this would still be quite long.

For me, any is not the good command but you could use isin (not tested)

B*(A==1)*(isin(B,[10,11,...,800]))
3
  • 1
    Just tried it, and it works! Thank you. Would you, by chance, have any good resource suggestions to learn the commands that one can use with gdal_calc? I used scipy.org, but it's rather difficult for me to figure out what I need using this website, considering I'm not familiar with the syntax and sometimes have no idea about the name of the command I'm looking for...
    – Mefimefi
    Nov 4, 2019 at 15:21
  • 1
    I am more familiar with otbcli_BandMath orfeo-toolbox.org/CookBook/Applications/app_BandMath.html , =i suggest that you have a look at it. Otherwise, you should refer to the numpy help.
    – radouxju
    Nov 4, 2019 at 16:21
  • I'll have look at it. Thanks !
    – Mefimefi
    Nov 5, 2019 at 9:09

Your Answer

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

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