Skip to content
Permalink
Browse files

[processing] restore simplify geometries & smooth algorithms (#4766)

  • Loading branch information
nirvn committed Jun 23, 2017
1 parent 62f4534 commit b7e66b4f72f708f5ac675198c119b7068ab617e9
@@ -61,10 +61,11 @@
from .RandomExtract import RandomExtract
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
from .RegularPoints import RegularPoints
from .SimplifyGeometries import SimplifyGeometries
from .Smooth import Smooth
from .SymmetricalDifference import SymmetricalDifference
from .VectorSplit import VectorSplit


# from .ExtractByLocation import ExtractByLocation
# from .PointsInPolygon import PointsInPolygon
# from .PointsInPolygonUnique import PointsInPolygonUnique
@@ -78,7 +79,6 @@
# from .ExportGeometryInfo import ExportGeometryInfo
# from .Delaunay import Delaunay
# from .VoronoiPolygons import VoronoiPolygons
# from .SimplifyGeometries import SimplifyGeometries
# from .LinesToPolygons import LinesToPolygons
# from .PolygonsToLines import PolygonsToLines
# from .SinglePartsToMultiparts import SinglePartsToMultiparts
@@ -134,7 +134,6 @@
# from .FieldsMapper import FieldsMapper
# from .Datasources2Vrt import Datasources2Vrt
# from .OrientedMinimumBoundingBox import OrientedMinimumBoundingBox
# from .Smooth import Smooth
# from .ReverseLineDirection import ReverseLineDirection
# from .SpatialIndex import SpatialIndex
# from .DefineProjection import DefineProjection
@@ -192,7 +191,7 @@ def getAlgs(self):
# NearestNeighbourAnalysis(), MeanCoords(),
# LinesIntersection(), UniqueValues(), PointDistance(),
# ExportGeometryInfo(),
# Delaunay(), VoronoiPolygons(), SimplifyGeometries(),
# Delaunay(), VoronoiPolygons(),
# , SinglePartsToMultiparts(),
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
# ConvexHull(), FixedDistanceBuffer(),
@@ -222,7 +221,7 @@ def getAlgs(self):
# SelectByExpression(), HypsometricCurves(),
# SplitWithLines(), CreateConstantRaster(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(), Smooth(),
# OrientedMinimumBoundingBox(),
# ReverseLineDirection(), SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
@@ -265,6 +264,8 @@ def getAlgs(self):
RandomExtract(),
RandomExtractWithinSubsets(),
RegularPoints(),
SimplifyGeometries(),
Smooth(),
SymmetricalDifference(),
VectorSplit()
]
@@ -32,13 +32,14 @@
from qgis.core import (QgsMapToPixelSimplifier,
QgsMessageLog,
QgsFeatureSink,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingOutputVectorLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@@ -59,20 +60,21 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'),
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'),
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.methods = [self.tr('Distance (Douglas-Peucker)'),
'Snap to grid',
'Area (Visvalingam)']
self.addParameter(ParameterSelection(
self.addParameter(QgsProcessingParameterEnum(
self.METHOD,
self.tr('Simplification method'),
self.methods, default=0))
self.addParameter(ParameterNumber(self.TOLERANCE,
self.tr('Tolerance'), 0.0, 10000000.0, 1.0))
self.methods, defaultValue=0))
self.addParameter(QgsProcessingParameterNumber(self.TOLERANCE,
self.tr('Tolerance'), minValue=0.0, maxValue=10000000.0, defaultValue=1.0))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Simplified')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Simplified')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Simplified')))

def name(self):
return 'simplifygeometries'
@@ -81,23 +83,25 @@ def displayName(self):
return self.tr('Simplify geometries')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
tolerance = self.getParameterValue(self.TOLERANCE)
method = self.getParameterValue(self.METHOD)
source = self.parameterAsSource(parameters, self.INPUT, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
method = self.parameterAsEnum(parameters, self.METHOD, context)

pointsBefore = 0
pointsAfter = 0

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(),
layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())

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

if method != 0:
simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method)

for current, input_feature in enumerate(features):
if feedback.isCanceled():
break
out_feature = input_feature
if input_feature.geometry():
input_geometry = input_feature.geometry()
@@ -110,10 +114,11 @@ def processAlgorithm(self, parameters, context, feedback):

pointsAfter += output_geometry.geometry().nCoordinates()
out_feature.setGeometry(output_geometry)
writer.addFeature(out_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

QgsMessageLog.logMessage(self.tr('Simplify: Input geometries have been simplified from {0} to {1} points').format(pointsBefore, pointsAfter),
self.tr('Processing'), QgsMessageLog.INFO)

return {self.OUTPUT: dest_id}
@@ -27,18 +27,21 @@

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterNumber,
QgsProcessingOutputVectorLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector, ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects


class Smooth(QgisAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
ITERATIONS = 'ITERATIONS'
MAX_ANGLE = 'MAX_ANGLE'
OFFSET = 'OFFSET'
@@ -54,15 +57,20 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(ParameterNumber(self.ITERATIONS,
self.tr('Iterations'), default=1, minValue=1, maxValue=10))
self.addParameter(ParameterNumber(self.OFFSET,
self.tr('Offset'), default=0.25, minValue=0.0, maxValue=0.5))
self.addParameter(ParameterNumber(self.MAX_ANGLE,
self.tr('Maximum node angle to smooth'), default=180.0, minValue=0.0, maxValue=180.0))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Smoothed')))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(QgsProcessingParameterNumber(self.ITERATIONS,
self.tr('Iterations'),
defaultValue=1, minValue=1, maxValue=10))
self.addParameter(QgsProcessingParameterNumber(self.OFFSET,
self.tr('Offset'), QgsProcessingParameterNumber.Double,
defaultValue=0.25, minValue=0.0, maxValue=0.5))
self.addParameter(QgsProcessingParameterNumber(self.MAX_ANGLE,
self.tr('Maximum node angle to smooth'), QgsProcessingParameterNumber.Double,
defaultValue=180.0, minValue=0.0, maxValue=180.0))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Smoothed')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Smoothed')))

def name(self):
return 'smoothgeometry'
@@ -71,18 +79,20 @@ def displayName(self):
return self.tr('Smooth geometry')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
iterations = self.getParameterValue(self.ITERATIONS)
offset = self.getParameterValue(self.OFFSET)
max_angle = self.getParameterValue(self.MAX_ANGLE)
source = self.parameterAsSource(parameters, self.INPUT, context)
iterations = self.parameterAsInt(parameters, self.ITERATIONS, context)
offset = self.parameterAsDouble(parameters, self.OFFSET, context)
max_angle = self.parameterAsDouble(parameters, self.MAX_ANGLE, context)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())

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

for current, input_feature in enumerate(features):
if feedback.isCanceled():
break
output_feature = input_feature
if input_feature.geometry():
output_geometry = input_feature.geometry().smooth(iterations, offset, -1, max_angle)
@@ -92,7 +102,7 @@ def processAlgorithm(self, parameters, context, feedback):

output_feature.setGeometry(output_geometry)

writer.addFeature(output_feature, QgsFeatureSink.FastInsert)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}

0 comments on commit b7e66b4

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