2

I have been following the QGIS scripting templates, but have been unable to get vector processing to work with input and output layers. Working with QGIS 3.2.2.

The following script is my attempt at processing a clip. I am pretty sure the clip processes, but the 'Output layer' it produces is empty. Attempts at processing a buffer have also produced an empty vector layer.

from PyQt5.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                   QgsFeatureSink,
                   QgsProcessingException,
                   QgsProcessingAlgorithm,
                   QgsProcessingParameterFeatureSource,
                   QgsProcessingParameterFeatureSink)
import processing

class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return ExampleProcessingAlgorithm()

    def name(self):
        return 'clip'

    def displayName(self):
        return self.tr('Clip setup')

    def group(self):
        return self.tr('Example scripts')

    def groupId(self):
        return 'examplescripts'

    def shortHelpString(self):
        return self.tr("Clip test")

    def initAlgorithm(self, config=None):
        self.addParameter(
            QgsProcessingParameterFeatureSource(
            self.INPUT,
            self.tr('Input layer'),
            [QgsProcessing.TypeVectorPolygon]))

        self.addParameter(
        QgsProcessingParameterFeatureSink(
            self.OUTPUT,
            self.tr('Output layer')))

   def processAlgorithm(self, parameters, context, feedback):

       regveg = "Regulated Vegetation"
       source = self.parameterAsSource(parameters, self.INPUT, context)
       (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,source.fields(), source.wkbType(), source.sourceCrs())

        cliplayer = processing.run("native:clip", {
        'INPUT' : regveg, 
        'OUTPUT' : 'memory:clip', 
        'OVERLAY' : parameters['INPUT']
    }, context=context, feedback=feedback)['OUTPUT']

        return {self.OUTPUT: cliplayer}
  • What happens if you replace 'OUTPUT' : 'memory:clip', with just 'OUTPUT' : 'memory:',? – Joseph Aug 28 '18 at 12:38
  • Still an empty vector file with 'OUTPUT' : 'memory:' , have tried lots of combinations. – Geoffrey Sinclair Aug 28 '18 at 12:45
  • Is you input layer a vector layer, regveg = "Regulated Vegetation"? – artwork21 Aug 30 '18 at 12:08
  • Yes @atwork21 the 'regveg' is a polygon vector layer that is clipped, it is in the project file. I know the clip is working from the processing log (I tried some samples with topology errors to check). – Geoffrey Sinclair Aug 31 '18 at 0:54

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.