2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SimplifyGeometries.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-04-24 14:35:50 +10:00
|
|
|
from qgis.core import (QgsMapToPixelSimplifier,
|
|
|
|
QgsMessageLog,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-06-23 14:46:40 +07:00
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingOutputVectorLayer)
|
2012-10-01 21:11:42 +03:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class SimplifyGeometries(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
TOLERANCE = 'TOLERANCE'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2016-09-01 08:22:18 +10:00
|
|
|
METHOD = 'METHOD'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'simplify.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-23 14:46:40 +07:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer'),
|
|
|
|
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
|
2016-09-01 08:22:18 +10:00
|
|
|
self.methods = [self.tr('Distance (Douglas-Peucker)'),
|
|
|
|
'Snap to grid',
|
|
|
|
'Area (Visvalingam)']
|
2017-06-23 14:46:40 +07:00
|
|
|
self.addParameter(QgsProcessingParameterEnum(
|
2016-09-01 08:22:18 +10:00
|
|
|
self.METHOD,
|
|
|
|
self.tr('Simplification method'),
|
2017-06-23 14:46:40 +07:00
|
|
|
self.methods, defaultValue=0))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.TOLERANCE,
|
|
|
|
self.tr('Tolerance'), minValue=0.0, maxValue=10000000.0, defaultValue=1.0))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-06-23 14:46:40 +07:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Simplified')))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Simplified')))
|
2012-10-01 21:11:42 +03:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.source = None
|
|
|
|
self.tolerance = None
|
|
|
|
self.method = None
|
|
|
|
self.sink = None
|
|
|
|
self.dest_id = None
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'simplifygeometries'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Simplify geometries')
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
|
|
|
self.source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
self.tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
|
|
|
|
self.method = self.parameterAsEnum(parameters, self.METHOD, context)
|
|
|
|
|
|
|
|
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
self.source.fields(), self.source.wkbType(), self.source.sourceCrs())
|
|
|
|
return True
|
2012-10-01 21:11:42 +03:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2012-10-01 21:11:42 +03:00
|
|
|
pointsBefore = 0
|
|
|
|
pointsAfter = 0
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
features = self.source.getFeatures()
|
|
|
|
total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0
|
2016-09-01 08:22:18 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
simplifier = None
|
|
|
|
if self.method != 0:
|
|
|
|
simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, self.tolerance, self.method)
|
2016-09-01 08:22:18 +10:00
|
|
|
|
2016-09-01 07:50:02 +10:00
|
|
|
for current, input_feature in enumerate(features):
|
2017-06-23 14:46:40 +07:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2016-09-01 07:50:02 +10:00
|
|
|
out_feature = input_feature
|
|
|
|
if input_feature.geometry():
|
|
|
|
input_geometry = input_feature.geometry()
|
|
|
|
pointsBefore += input_geometry.geometry().nCoordinates()
|
2016-09-01 08:22:18 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.method == 0: # distance
|
|
|
|
output_geometry = input_geometry.simplify(self.tolerance)
|
2016-09-01 08:22:18 +10:00
|
|
|
else:
|
|
|
|
output_geometry = simplifier.simplify(input_geometry)
|
|
|
|
|
2016-09-01 07:50:02 +10:00
|
|
|
pointsAfter += output_geometry.geometry().nCoordinates()
|
|
|
|
out_feature.setGeometry(output_geometry)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
|
2017-06-23 14:46:40 +07:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Simplify: Input geometries have been simplified from {0} to {1} points').format(pointsBefore, pointsAfter),
|
|
|
|
self.tr('Processing'), QgsMessageLog.INFO)
|
2017-06-28 19:55:08 +10:00
|
|
|
return True
|
2017-06-23 14:46:40 +07:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
|
|
return {self.OUTPUT: self.dest_id}
|