2

A script I have written to perform a number of tasks on a single input needs amending to work with multiple inputs given an input folder. The working script is this and takes a input raster through some processes to output a shp:

##in_rast=raster
##out_shp=output vector

#Take grid and create extent
out1=processing.runalg('gdalogr:rastercalculator', in_rast,'1',None,'1',None,'1',None,'1',None,'1',None,'1','A>0',None,5,None,None)
#Ratser extent to polygon
out2=processing.runalg('gdalogr:polygonize', out1['OUTPUT'],'DN',None)
#Calculate area m^2 and add to new field 'Area_m2'
out3=processing.runalg('qgis:fieldcalculator', out2['OUTPUT'],'Area_m2',0,10.0,3.0,True,'$area',None)
#Select polygons >5m^2 and extract to new shp
out4=processing.runalg('qgis:extractbyattribute', out3['OUTPUT_LAYER'],'Area_m2',3,'5',None)
#Fill holes less than 200m^2
out5=processing.runalg('qgis:fillholes', out4['OUTPUT'],200.0,out_shp)

I have made a start on updating this to work on multiple inputs but being relativity new to python my understanding of the correct methods to use has reached its limit. The updated script looks as follows, only working and outputting for the first process:

##Input_raster=multiple raster
##result_ras=folder

import os

layers = Input_raster.split(';')
for raster in layers:
    result_name = os.path.splitext(os.path.basename(raster)) [0] + "_Extent.tif"
    out1=processing.runalg('gdalogr:rastercalculator', raster,'1',None,'1',None,'1',None,'1',None,'1',None,'1','A>0',None,5,None,result_ras + "/" + result_name)

The question is; how can I continue to write the rest of the processes into my script? I am only interested in the output for the final step so all intermediate outputs can be saved as temp. Ultimately I'd like the output to take the name of the original input raster and append _Outline.shp.

  • What happens if you add the the rest of the processes exactly like you did previously inside your for loop? – Joseph Nov 16 '16 at 10:51
  • 1
    Adding them into that loop has worked I also adjusted the naming of the out_shp as James suggested. – AJ Walsh Nov 16 '16 at 11:31
2

Can you just add the remaining processes to the loop:

layers = Input_raster.split(';')
for raster in layers:
    result_name = os.path.splitext(os.path.basename(raster)) [0] + "_Extent.tif"
    out1=processing.runalg('gdalogr:rastercalculator', raster,'1',None,'1',None,'1',None,'1',None,'1',None,'1','A>0',None,5,None,result_ras + "/" + result_name)
    out2=processing.runalg('gdalogr:polygonize', out1['OUTPUT'],'DN',None)
    out3=processing.runalg('qgis:fieldcalculator', out2['OUTPUT'],'Area_m2',0,10.0,3.0,True,'$area',None)
    out4=processing.runalg('qgis:extractbyattribute', out3['OUTPUT_LAYER'],'Area_m2',3,'5',None)
    out5=processing.runalg('qgis:fillholes', out4['OUTPUT'],200.0,out_shp)

I guess out_shp is the name of the output shapefile, so you will need to generate a new name for each layer - you can do this just the same way that you generate result_name, just use .shp instead of .tif

1

Following the comments from James & Joseph I added the other processes inside the loop. The script now appears to be producing the desired results:

##Input_raster=multiple raster
##result_shp=folder

import os

layers = Input_raster.split(';')
for raster in layers:
    result_name = os.path.splitext(os.path.basename(raster)) [0] + "_Extent.tif"
    out1=processing.runalg('gdalogr:rastercalculator', raster,'1',None,'1',None,'1',None,'1',None,'1',None,'1','A>0',None,5,None,result_shp + "/" + result_name)
    out2=processing.runalg('gdalogr:polygonize', out1['OUTPUT'],'DN',None)
    out3=processing.runalg('qgis:fieldcalculator', out2['OUTPUT'],'Area_m2',0,10.0,3.0,True,'$area',None)
    out4=processing.runalg('qgis:extractbyattribute', out3['OUTPUT_LAYER'],'Area_m2',3,'5',None)
    shp_name = os.path.splitext(os.path.basename(raster)) [0] + "_out.shp"
    out5=processing.runalg('qgis:fillholes', out4['OUTPUT'],200.0,result_shp + "/" + shp_name)

Thanks for the help

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.