Skip to content

Instantly share code, notes, and snippets.

@benwirf
Last active March 30, 2023 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwirf/5dd676a3b794c649d9a289af8447b928 to your computer and use it in GitHub Desktop.
Save benwirf/5dd676a3b794c649d9a289af8447b928 to your computer and use it in GitHub Desktop.
QGIS processing model exported as Python script with layer post processor to rename final output layer
"""
Model exported as python.
Name : clipped_points_buffered
Group : Models
With QGIS : 31415
"""
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsProcessingLayerPostProcessorInterface
from qgis.core import QgsVectorLayer
import processing
class Clipped_points_buffered(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('Inputpoints', 'Input_points', types=[QgsProcessing.TypeVectorPoint], defaultValue=None))
self.addParameter(QgsProcessingParameterVectorLayer('Inputpolygons', 'Input_polygons', types=[QgsProcessing.TypeVectorPolygon], defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('Final_layer', 'Final_layer', type=QgsProcessing.TypeVectorPolygon, createByDefault=True, supportsAppend=True, defaultValue=None))
def processAlgorithm(self, parameters, context, model_feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(2, model_feedback)
results = {}
outputs = {}
# Clip
alg_params = {
'INPUT': parameters['Inputpoints'],
'OVERLAY': parameters['Inputpolygons'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Clip'] = processing.run('native:clip', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(1)
if feedback.isCanceled():
return {}
# Buffer
alg_params = {
'DISSOLVE': False,
'DISTANCE': 0.01,
'END_CAP_STYLE': 0,
'INPUT': outputs['Clip']['OUTPUT'],
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'SEGMENTS': 8,
'OUTPUT': parameters['Final_layer']
}
outputs['Buffer'] = processing.run('native:buffer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Final_layer'] = outputs['Buffer']['OUTPUT']
if context.willLoadLayerOnCompletion(results['Final_layer']):
context.layerToLoadOnCompletionDetails(results['Final_layer']).setPostProcessor(MyLayerPostProcessor.create())
return results
def name(self):
return 'clipped_points_buffered'
def displayName(self):
return 'clipped_points_buffered'
def group(self):
return 'Models'
def groupId(self):
return 'Models'
def createInstance(self):
return Clipped_points_buffered()
# Courtesy of Nyall Dawson: https://gist.github.com/nyalldawson/26c091dd48b4f8bf56f172efe22cf75f
class MyLayerPostProcessor(QgsProcessingLayerPostProcessorInterface):
instance = None
def postProcessLayer(self, layer, context, feedback): # pylint: disable=unused-argument
if not isinstance(layer, QgsVectorLayer):
return
# Do any non-thread safe post-processing here.
# Set a style for your layer etc.
layer.setName('Renamed layer')
# Hack to work around sip bug!
@staticmethod
def create() -> 'MyLayerPostProcessor':
"""
Returns a new instance of the post processor, keeping a reference to the sip
wrapper so that sip doesn't get confused with the Python subclass and call
the base wrapper implementation instead... ahhh sip, you wonderful piece of sip
"""
MyLayerPostProcessor.instance = MyLayerPostProcessor()
return MyLayerPostProcessor.instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment