-
-
Save ghtmtt/2fa6467b1b062a15430a7bbc15c0c370 to your computer and use it in GitHub Desktop.
buffer_on_selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
*************************************************************************** | |
* * | |
* This program is free software; you can redistribute it and/or modify * | |
* it under the terms of the GNU General Public License as published by * | |
* the Free Software Foundation; either version 2 of the License, or * | |
* (at your option) any later version. * | |
* * | |
*************************************************************************** | |
""" | |
import os | |
from qgis.PyQt.QtCore import QCoreApplication | |
from qgis.PyQt.QtGui import QIcon | |
from qgis.core import (QgsProcessing, | |
QgsProcessingAlgorithm, | |
QgsProcessingParameterFeatureSource, | |
QgsProcessingParameterDistance, | |
QgsProcessingParameterBoolean, | |
QgsWkbTypes, | |
QgsProcessingParameterFeatureSink) | |
from qgis import processing | |
class EtraBufferAlgorithm(QgsProcessingAlgorithm): | |
INPUT = 'INPUT' | |
DISSOLVE = 'DISSOLVE' | |
DISTANCE = 'DISTANCE' | |
OUTPUT = 'OUTPUT' | |
def tr(self, string): | |
return QCoreApplication.translate('', string) | |
def createInstance(self): | |
return EtraBufferAlgorithm() | |
def name(self): | |
return 'etra_buffer' | |
def displayName(self): | |
return self.tr('Buffer') | |
def group(self): | |
return self.tr('Strumenti') | |
def groupId(self): | |
return 'strumenti' | |
def icon(self): | |
icon_path = QIcon(os.path.join(os.path.dirname(__file__), '..', 'icons', 'mAlgorithmBuffer.svg')) | |
return icon_path | |
def initAlgorithm(self, config=None): | |
self.addParameter( | |
QgsProcessingParameterFeatureSource( | |
name=self.INPUT, | |
description=self.tr('Layer in Input'), | |
types=[QgsProcessing.TypeVectorAnyGeometry], | |
) | |
) | |
self.addParameter( | |
QgsProcessingParameterDistance( | |
self.DISTANCE, | |
self.tr('Distanza del buffer'), | |
parentParameterName=self.INPUT, | |
defaultValue=10 | |
) | |
) | |
self.addParameter( | |
QgsProcessingParameterBoolean( | |
self.DISSOLVE, | |
self.tr('Dissolvi il risultato'), | |
defaultValue=False | |
) | |
) | |
self.addParameter( | |
QgsProcessingParameterFeatureSink( | |
self.OUTPUT, | |
self.tr('Buffer') | |
) | |
) | |
def processAlgorithm(self, parameters, context, feedback): | |
source = self.parameterAsSource( | |
parameters, | |
self.INPUT, | |
context | |
) | |
distance = self.parameterAsDouble( | |
parameters, | |
self.DISTANCE, | |
context | |
) | |
dissolve = self.parameterAsBoolean( | |
parameters, | |
self.DISSOLVE, | |
context | |
) | |
(sink, dest_id) = self.parameterAsSink( | |
parameters, | |
self.OUTPUT, | |
context, | |
source.fields(), | |
QgsWkbTypes.MultiPolygon, | |
source.sourceCrs() | |
) | |
param = { | |
'INPUT': source.sourceName(), | |
'DISTANCE': distance, | |
'DISSOLVE': dissolve, | |
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT | |
} | |
buffered = processing.run('native:buffer', param, feedback=feedback, context=context)['OUTPUT'] | |
for f in buffered.getFeatures(): | |
sink.addFeature(f) | |
return {self.OUTPUT: dest_id} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment