I am creating a Python/PyQGIS tool in QGIS 2.18 which combines a few functions:

  • Extract several attributes from source layer
  • Redefine a coordinate system
  • Optional: clip the source layer with a clipping layer

The goal is to run this tool as a batch processing over multiple layers.

The tool is working now, but results sometimes in empty layer because the source layer does not have features within the clipping layer. In that case I want to skip the output (no output). Does anyone know how to built that in Python?

This is my current code:

##clip=optional vector
##output=output vector

from qgis.core import *
from qgis.core import QgsFeature

layer = processing.getObject(input)
select = QgsVectorLayer.selectByExpression(layer, 'eindRegistratie is NULL')
num = layer.selectedFeatureCount()
if num > 0:
    if clip is not None:
        outputs_QGISEXTRACTBYEXPRESSION_1=processing.runalg('qgis:extractbyexpression', input,'eindRegistratie is NULL',None)
        outputs_QGISDEFINECURRENTPROJECTION_1=processing.runalg('qgis:definecurrentprojection', outputs_QGISEXTRACTBYEXPRESSION_1['OUTPUT'],'EPSG:28992')
        outputs_QGISCLIP_1=processing.runalg('qgis:clip', outputs_QGISDEFINECURRENTPROJECTION_1['OUTPUT'],clip,None)

        outputs_QGISEXTRACTBYEXPRESSION_1=processing.runalg('qgis:extractbyexpression', input,'eindRegistratie is NULL',output)
        outputs_QGISDEFINECURRENTPROJECTION_1=processing.runalg('qgis:definecurrentprojection', outputs_QGISEXTRACTBYEXPRESSION_1['OUTPUT'],'EPSG:28992')

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.