2

I am new to QGIS and got my hands dirty using the processing modeler. the task is to perform zonal statistics to get total area of classes in a raster layer against classes in a vector layer. the process is outlined below

  1. get admin vector layer, raster layer1, and raster layer2
  2. using raster calculator, select all cells in raster layer 1 that is not equal to '2' and multiply by '0.04'
  3. perform zonalstatistics between admin vector layer and output of step #2
  4. using raster calculator, select all cells in raster layer 2 that is equal to '2' and multiply by '0.04'
  5. perform zonalstatistics between output of step#3 and output of step#4
  6. repeat step 4 to 5 changing the expression criteria to 3 until 12
  7. edit the attribute table of the last zonal statistics output and remove columns that do not have '_sum'

How can I make this into a script using iterator or loop?

here is the exported script from the model

##Rice and SoS area calculator=name
##ricerasterlayer=raster
##inputs=vector
##sosupperclasscode=number 12
##sosrasterlayer=raster
##soslowerclass=number 2
##output=output vector


outputs_QGISREPROJECTLAYER_1=processing.runalg('qgis:reprojectlayer', inputs,'+proj=cea +lon_0=100 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs',None)
outputs_GDALOGRRASTERCALCULATOR_1=processing.runalg('gdalogr:rastercalculator', ricerasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A!=2)*0.04','-9999',5,None,None)
outputs_GDALOGRRASTERCALCULATOR_2=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==2)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_3=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==3)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_4=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==4)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_5=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==5)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_6=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==6)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_7=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==7)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_8=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==8)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_9=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==9)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_10=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==10)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_11=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==11)*0.04',None,5,None,None)
outputs_GDALOGRRASTERCALCULATOR_12=processing.runalg('gdalogr:rastercalculator', sosrasterlayer,'1',None,'1',None,'1',None,'1',None,'1',None,'1','(A==12)*0.04',None,5,None,None)
outputs_QGISZONALSTATISTICS_1=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_1['OUTPUT'],1.0,outputs_QGISREPROJECTLAYER_1['OUTPUT'],'RA_',False,None)
outputs_QGISZONALSTATISTICS_2=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_2['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_1['OUTPUT_LAYER'],'SoS2_',False,None)
outputs_QGISZONALSTATISTICS_3=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_3['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_2['OUTPUT_LAYER'],'SoS3_',False,None)
outputs_QGISZONALSTATISTICS_4=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_4['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_3['OUTPUT_LAYER'],'SoS4_',False,None)
outputs_QGISZONALSTATISTICS_5=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_5['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_4['OUTPUT_LAYER'],'SoS5_',False,None)
outputs_QGISZONALSTATISTICS_6=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_6['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_5['OUTPUT_LAYER'],'SoS6_',False,None)
outputs_QGISZONALSTATISTICS_7=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_7['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_6['OUTPUT_LAYER'],'SoS7_',False,None)
outputs_QGISZONALSTATISTICS_8=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_8['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_7['OUTPUT_LAYER'],'SoS8_',False,None)
outputs_QGISZONALSTATISTICS_9=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_9['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_8['OUTPUT_LAYER'],'SoS9_',False,None)
outputs_QGISZONALSTATISTICS_10=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_10['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_9['OUTPUT_LAYER'],'SoS10_',False,None)
outputs_QGISZONALSTATISTICS_11=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_11['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_10['OUTPUT_LAYER'],'SoS11_',False,None)
outputs_QGISZONALSTATISTICS_12=processing.runalg('qgis:zonalstatistics', outputs_GDALOGRRASTERCALCULATOR_12['OUTPUT'],1.0,outputs_QGISZONALSTATISTICS_11['OUTPUT_LAYER'],'SoS12_',False,output)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.