Wednesday 22 February 2017

pyqgis - Python processing script with clip in QGIS 3.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}



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...