Wednesday, 12 February 2020

qgis processing - Using pyqgis logic to stop process if output has no geometry


I have a pyqgis script which customises the functionality of the grass algorithm r.lake.coords:


##Seed Points Lake=name

##DEM_input=raster
##Seed_vector_points=vector point
##Overspill_vector_boundary=vector polygon
##output=folder


# Import classes
from qgis.core import QgsRasterLayer
from qgis.utils import iface
from PyQt4.QtCore import QFileInfo

import processing

# Define layers
point_layer = processing.getObject(Seed_vector_points)
layer = processing.getObject(DEM_input)
boundary_layer = processing.getObject(Overspill_vector_boundary)
fileName = layer.source()
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)


# Get extent of the region
extent = iface.mapCanvas().extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

for feat in point_layer.getFeatures():
geom = feat.geometry()

attr = feat.attribute("Z_ADD_1")
i = 0
while i < 5:
output_1 = processing.runalg('grass:r.lake.coords', layer, "%f"%(attr), "%f,%f"% (geom.asPoint().x(), geom.asPoint().y()) , False, "%f,%f,%f,%f"% (xmin, xmax, ymin, ymax), 0.00, None)
output_2 = processing.runalg('saga:rastercalculator', output_1['lake'], '','a>0', False, 3, None)
output_3 = processing.runalg('gdalogr:polygonize', output_2['RESULT'], "DN", None)
output_4 = processing.runalg('qgis:dissolve',output_3['OUTPUT'], True, "DN", None)
output_5 = processing.runalg("qgis:extractbylocation", output_4['OUTPUT'], boundary_layer, u'within', output + '/' + baseName + "_%.6f_%.6f" %(geom.asPoint().x(), geom.asPoint().y()) + "_%.2f"%(attr) + 'm.shp')
i += 1
attr += 0.5


My problem is that the final algorithm which the script calls upon produces additional blank shapefiles where there is no geometry, this is technically correct if the outputs from output_4 are not within the overspill boundary.


However, I would ideally like to have the script set up using some sort of python logic where it avoids producing final shapefiles with no geometry.



Answer



Great answer by @LaughU! If you want to prevent processed layers with no geometry to have no output, you could use the None parameter to save the output as a temporary file initially, check if this has any features and if so, write to a shapefile otherwise skip it:


...
output_5 = processing.runalg("qgis:extractbylocation", output_4['OUTPUT'], boundary_layer, u'within', 0.00, None)
layer = processing.getObject(output_5['OUTPUT'])
if not layer.featureCount() == 0:
QgsVectorFileWriter.writeAsVectorFormat(layer, output + '/' + baseName + "_%.6f_%.6f" %(geom.asPoint().x(), geom.asPoint().y()) + "_%.2f"%(attr) + 'm.shp', "utf-8", None, "ESRI Shapefile")

i += 1
attr += 0.5

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...