4

I have 5 categorical rasters and an old script to classify them. The script (*.sml) was created for TNTmips which is no longer an option. I know how to do the operation with raster calculator in QGIS but it is a tedious job. The script looks something like this:

    GetlnputRaster(A)
    GetlnputRaster(B)
    GetlnputRaster(C)
    GetlnputRaster(D)
    GetlnputRaster(E)

    GetOutputRaster(F,Numlins(A),NumCols(A),RastType(A))
    SetScale(F,LinScale(A),ColScale(A))
    CopySubobjects(A,F)

    for each A begin
    F=((A=1   and   B<=40 and C<=60)*6\
        +(A=1   and   B<=40  and C>60)*2\
        +(A=1   and  B>40  and  B<=60 and C<=60)*11\
        +(A=1   and  B>40  and  B<=60 and C>60)*21\
        +(A= 1 and  B>60)*1\
    +(A=2  and  C<=60 and B<=40)*61\
    +(A=2  and  C<=60 and B>40)*11\
    +(A=2  and  C>60 and B>40)*11\
    +(A=2  and  C>60  and B<=40)*21\
        +(A=41   and  D<=40 and  B<=50 and  C<=60)*61\
        +(A=41   and  D<=40 and  B<=50 and C>60)*21\
        +(A=41   and  D<=40 and  B>50 and  B<=60 and C>60)*21\
        +(A=41   and  D<=40 and  B>50  and  B<=60 and C<=60)*11\
        +(A=41   and  D<=40 and B>60)*11\
        +(A=41  and  D>40  and  D<=60 and B<=60)*411\
        +(A=41  and  D>40  and  D<=60 and B>60)*11\
        +(A=41   and D>60)*411\
    ..............
    end

but with a total of 54 lines. Is there a simpler way of doing this in python or to create a text file with classification rules and run it in python or more appropriate, how to translate this script in python? I ask this because I am not so good in python programming and I want to avoid a very, very long formula in raster calculator.


I have found a possible solution which raise another question. I have used the Combine tool from ArcGIS for three rasters and then I have added a new field in which I have used a python expression (in Field Calculator) to recode the cells:

def Classify(A, B, C):
  if((A == 100 and B == 1 and C == 1) or
     (A == 100 and B == 2 and C == 1) or
     (A == 100 and B == 5) or
     (A == 200 and B == 1 and C == 1) or
     (A == 200 and B == 2 and C == 1) or
     (A == 400 and B == 5 and C == 1) or
     (A == 500 and B == 5 and C == 1)):
      return 111 
  elif ((A == 100 and B == 1 and C == 2) or
        (A == 100 and B == 2 and C == 2) or 
        (A == 100 and B == 3 and C == 1) or
        (A == 200 and B == 1 and C == 2) or
        (A == 200 and B == 2 and C == 2) or
        (A == 400 and B == 5 and C > 1) or 
        (A == 500 and B == 5 and C > 1)):
        return 112
  elif ((A == 100 and B == 1 and C == 3) or 
        (A == 100 and B == 2 and C == 3) or 
        (A == 100 and B == 3 and C == 2) or
        (A == 100 and B == 4 and C == 1) or
        (A == 200 and B == 1 and C == 3) or
        (A == 200 and B == 2 and C == 3)):
        return 113
............................................

I have used a slightly different code system but the principle is the same. Now the question: There is any possibility to create a table with all the criteria and use this table to recode a raster? I ask this question because I intend in the future to use more than three categorical rasters.

closed as too broad by PolyGeo Apr 22 '18 at 1:25

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I think you should use the edit button to revise your question so that it represents just where you are currently stuck. – PolyGeo Apr 22 '18 at 1:25