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}
gene
55.8k3 gold badges115 silver badges196 bronze badges
asked Aug 28, 2018 at 12:29
4
  • What happens if you replace 'OUTPUT' : 'memory:clip', with just 'OUTPUT' : 'memory:',? Commented Aug 28, 2018 at 12:38
  • Still an empty vector file with 'OUTPUT' : 'memory:' , have tried lots of combinations. Commented Aug 28, 2018 at 12:45
  • Is you input layer a vector layer, regveg = "Regulated Vegetation"? Commented Aug 30, 2018 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). Commented Aug 31, 2018 at 0:54

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.