Skip to content
Permalink
Browse files

Port fixed distance buffer to new API

  • Loading branch information
nyalldawson committed Jul 28, 2017
1 parent e53a14a commit 5763381cbdb98299d6d92191c519e692522af23f
@@ -25,25 +25,32 @@

__revision__ = '$Format:%H$'

from qgis.core import QgsFeature, QgsGeometry, QgsProcessingUtils
from qgis.core import (QgsFeature,
QgsGeometry,
QgsFeatureRequest,
QgsFeatureSink)


def buffering(feedback, context, writer, distance, field, useField, layer, dissolve, segments, endCapStyle=1,
def buffering(feedback, context, sink, distance, field, useField, source, dissolve, segments, endCapStyle=1,
joinStyle=1, mitreLimit=2):

if useField:
field = layer.fields().lookupField(field)
field = source.fields().lookupField(field)

outFeat = QgsFeature()

current = 0
features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
total = 100.0 / source.featureCount() if source.featureCount() else 0

# With dissolve
if dissolve:

features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]))
buffered_geometries = []
for inFeat in features:
if feedback.isCanceled():
break

attrs = inFeat.attributes()
if useField:
value = attrs[field]
@@ -60,10 +67,15 @@ def buffering(feedback, context, writer, distance, field, useField, layer, disso
final_geometry = QgsGeometry.unaryUnion(buffered_geometries)
outFeat.setGeometry(final_geometry)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
else:

features = source.getFeatures()

# Without dissolve
for inFeat in features:
if feedback.isCanceled():
break
attrs = inFeat.attributes()
if useField:
value = attrs[field]
@@ -74,8 +86,6 @@ def buffering(feedback, context, writer, distance, field, useField, layer, disso
outGeom = inGeom.buffer(float(value), segments, endCapStyle, joinStyle, mitreLimit)
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
current += 1
feedback.setProgress(int(current * total))

del writer
@@ -30,18 +30,16 @@
from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsWkbTypes,
QgsFeatureSink,
QgsProcessingUtils)
QgsProcessing,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection

from processing.core.outputs import OutputVector
from . import Buffer as buff
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -68,31 +66,33 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer')))
self.addParameter(ParameterNumber(self.DISTANCE,
self.tr('Distance'), default=10.0))
self.addParameter(ParameterNumber(self.SEGMENTS,
self.tr('Segments'), 1, default=5))
self.addParameter(ParameterBoolean(self.DISSOLVE,
self.tr('Dissolve result'), False))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))

self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
self.tr('Distance'), defaultValue=10.0))
self.addParameter(QgsProcessingParameterNumber(self.SEGMENTS,
self.tr('Segments'), type=QgsProcessingParameterNumber.Integer, minValue=1, defaultValue=5))
self.addParameter(QgsProcessingParameterBoolean(self.DISSOLVE,
self.tr('Dissolve result'), defaultValue=False))
self.end_cap_styles = [self.tr('Round'),
'Flat',
'Square']
self.addParameter(ParameterSelection(
self.addParameter(QgsProcessingParameterEnum(
self.END_CAP_STYLE,
self.tr('End cap style'),
self.end_cap_styles, default=0))
options=self.end_cap_styles, defaultValue=0))
self.join_styles = [self.tr('Round'),
'Mitre',
'Bevel']
self.addParameter(ParameterSelection(
self.addParameter(QgsProcessingParameterEnum(
self.JOIN_STYLE,
self.tr('Join style'),
self.join_styles, default=0))
self.addParameter(ParameterNumber(self.MITRE_LIMIT,
self.tr('Mitre limit'), 1, default=2))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Buffer'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
options=self.join_styles, defaultValue=0))
self.addParameter(QgsProcessingParameterNumber(self.MITRE_LIMIT,
self.tr('Mitre limit'), minValue=0, defaultValue=2))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Buffer'), QgsProcessing.TypeVectorPolygon))

def name(self):
return 'fixeddistancebuffer'
@@ -101,16 +101,19 @@ def displayName(self):
return self.tr('Fixed distance buffer')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
distance = self.getParameterValue(self.DISTANCE)
dissolve = self.getParameterValue(self.DISSOLVE)
segments = int(self.getParameterValue(self.SEGMENTS))
end_cap_style = self.getParameterValue(self.END_CAP_STYLE) + 1
join_style = self.getParameterValue(self.JOIN_STYLE) + 1
miter_limit = self.getParameterValue(self.MITRE_LIMIT)

writer = self.getOutputFromName(
self.OUTPUT).getVectorWriter(layer.fields(), QgsWkbTypes.Polygon, layer.crs(), context)

buff.buffering(feedback, context, writer, distance, None, False, layer, dissolve, segments, end_cap_style,
source = self.parameterAsSource(parameters, self.INPUT, context)

distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
dissolve = self.parameterAsBool(parameters, self.DISSOLVE, context)
segments = self.parameterAsInt(parameters, self.SEGMENTS, context)
end_cap_style = self.parameterAsEnum(parameters, self.END_CAP_STYLE, context) + 1
join_style = self.parameterAsEnum(parameters, self.JOIN_STYLE, context) + 1
miter_limit = self.parameterAsDouble(parameters, self.MITRE_LIMIT, context)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), QgsWkbTypes.Polygon, source.sourceCrs())

buff.buffering(feedback, context, sink, distance, None, False, source, dissolve, segments, end_cap_style,
join_style, miter_limit)

return {self.OUTPUT: dest_id}
@@ -67,6 +67,7 @@
from .ExtentFromLayer import ExtentFromLayer
from .ExtractNodes import ExtractNodes
from .ExtractSpecificNodes import ExtractSpecificNodes
from .FixedDistanceBuffer import FixedDistanceBuffer
from .FixGeometry import FixGeometry
from .GeometryByExpression import GeometryByExpression
from .GridPolygon import GridPolygon
@@ -132,7 +133,6 @@
from .ZonalStatistics import ZonalStatistics

# from .ExtractByLocation import ExtractByLocation
# from .FixedDistanceBuffer import FixedDistanceBuffer
# from .VariableDistanceBuffer import VariableDistanceBuffer
# from .RandomSelection import RandomSelection
# from .RandomSelectionWithinSubsets import RandomSelectionWithinSubsets
@@ -185,7 +185,6 @@ def __init__(self):

def getAlgs(self):
# algs = [
# FixedDistanceBuffer(),
# VariableDistanceBuffer(),
# RandomSelection(), RandomSelectionWithinSubsets(),
# SelectByLocation(),
@@ -240,6 +239,7 @@ def getAlgs(self):
ExtentFromLayer(),
ExtractNodes(),
ExtractSpecificNodes(),
FixedDistanceBuffer(),
FixGeometry(),
GeometryByExpression(),
GridPolygon(),

0 comments on commit 5763381

Please sign in to comment.
You can’t perform that action at this time.