Skip to content

Commit 9009ef0

Browse files
committed
[processing] Add smooth algorithm
Allows for smoothing line or polygon layers
1 parent 53aa583 commit 9009ef0

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
from Datasources2Vrt import Datasources2Vrt
132132
from CheckValidity import CheckValidity
133133
from OrientedMinimumBoundingBox import OrientedMinimumBoundingBox
134+
from Smooth import Smooth
134135

135136
pluginPath = os.path.normpath(os.path.join(
136137
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -179,7 +180,7 @@ def __init__(self):
179180
SelectByExpression(), HypsometricCurves(),
180181
SplitLinesWithLines(), CreateConstantRaster(),
181182
FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
182-
CheckValidity(), OrientedMinimumBoundingBox()
183+
CheckValidity(), OrientedMinimumBoundingBox(), Smooth()
183184
]
184185

185186
if hasMatplotlib:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Smooth.py
6+
---------
7+
Date : November 2015
8+
Copyright : (C) 2015 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'November 2015'
22+
__copyright__ = '(C) 2015, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import QGis, QgsGeometry, QgsFeature
29+
from processing.core.GeoAlgorithm import GeoAlgorithm
30+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
31+
from processing.core.parameters import ParameterVector, ParameterNumber
32+
from processing.core.outputs import OutputVector
33+
from processing.tools import dataobjects, vector
34+
35+
36+
class Smooth(GeoAlgorithm):
37+
38+
INPUT_LAYER = 'INPUT_LAYER'
39+
OUTPUT_LAYER = 'OUTPUT_LAYER'
40+
ITERATIONS = 'ITERATIONS'
41+
OFFSET = 'OFFSET'
42+
43+
def defineCharacteristics(self):
44+
self.name, self.i18n_name = self.trAlgorithm('Smooth geometry')
45+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
46+
47+
self.addParameter(ParameterVector(self.INPUT_LAYER,
48+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POLYGON, ParameterVector.VECTOR_TYPE_LINE]))
49+
self.addParameter(ParameterNumber(self.ITERATIONS,
50+
self.tr('Iterations'), default=1, minValue=1, maxValue=10))
51+
self.addParameter(ParameterNumber(self.OFFSET,
52+
self.tr('Offset'), default=0.25, minValue=0.0, maxValue=0.5))
53+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Smoothed')))
54+
55+
def processAlgorithm(self, progress):
56+
layer = dataobjects.getObjectFromUri(
57+
self.getParameterValue(self.INPUT_LAYER))
58+
provider = layer.dataProvider()
59+
iterations = self.getParameterValue(self.ITERATIONS)
60+
offset = self.getParameterValue(self.OFFSET)
61+
62+
writer = self.getOutputFromName(
63+
self.OUTPUT_LAYER).getVectorWriter(
64+
layer.fields().toList(),
65+
provider.geometryType(),
66+
layer.crs())
67+
68+
outFeat = QgsFeature()
69+
70+
features = vector.features(layer)
71+
total = 100.0 / float(len(features))
72+
current = 0
73+
74+
for inFeat in features:
75+
inGeom = inFeat.constGeometry()
76+
attrs = inFeat.attributes()
77+
78+
outGeom = inGeom.smooth(iterations, offset)
79+
if outGeom is None:
80+
raise GeoAlgorithmExecutionException(
81+
self.tr('Error smoothing geometry'))
82+
83+
outFeat.setGeometry(outGeom)
84+
outFeat.setAttributes(attrs)
85+
writer.addFeature(outFeat)
86+
current += 1
87+
progress.setPercentage(int(current * total))
88+
89+
del writer

0 commit comments

Comments
 (0)