From 18f85d9e54e311f6a3e789ef564df879bd899edd Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 13 Oct 2017 08:30:12 +1000 Subject: [PATCH] Port drop M/Z algorithm to c++ --- python/plugins/processing/algs/help/qgis.yaml | 3 - .../processing/algs/qgis/DropMZValues.py | 94 ------------------- .../algs/qgis/QGISAlgorithmProvider.py | 2 - .../tests/testdata/qgis_algorithm_tests.yaml | 6 +- src/core/processing/qgsnativealgorithms.cpp | 51 ++++++++++ src/core/processing/qgsnativealgorithms.h | 30 ++++++ 6 files changed, 84 insertions(+), 102 deletions(-) delete mode 100644 python/plugins/processing/algs/qgis/DropMZValues.py diff --git a/python/plugins/processing/algs/help/qgis.yaml b/python/plugins/processing/algs/help/qgis.yaml index 154b19fec90..3e002333212 100644 --- a/python/plugins/processing/algs/help/qgis.yaml +++ b/python/plugins/processing/algs/help/qgis.yaml @@ -153,9 +153,6 @@ qgis:distancetonearesthub: > The resulting layer can contain only source points with an additional field indicating the distance to the nearest point and the name of the destination point, or lines linking each source point with its nearest destination point. -qgis:dropmzvalues: > - This algorithm can remove any measure (M) or Z values from input geometries. - qgis:eliminateselectedpolygons: > This algorithm combines selected polygons of the input layer with certain adjacent polygons by erasing their common boundary. The adjacent polygon can be either the one with the largest or smallest area or the one sharing the largest common boundary with the polygon to be eliminated. The selected features will always be eliminated whether the option "Use only selected features" is set or not. Eliminate is normally used to get rid of sliver polygons, i.e. tiny polygons that are a result of polygon intersection processes where boundaries of the inputs are similar but not identical. diff --git a/python/plugins/processing/algs/qgis/DropMZValues.py b/python/plugins/processing/algs/qgis/DropMZValues.py deleted file mode 100644 index be69f27a11d..00000000000 --- a/python/plugins/processing/algs/qgis/DropMZValues.py +++ /dev/null @@ -1,94 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -*************************************************************************** - DropMZValues.py - -------------- - Date : July 2017 - Copyright : (C) 2017 by Nyall Dawson - Email : nyall dot dawson 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__ = 'Nyall Dawson' -__date__ = 'July 2017' -__copyright__ = '(C) 2017, Nyall Dawson' - -# This will get replaced with a git SHA1 when you do a git archive323 - -__revision__ = '$Format:%H$' - -import os - -from qgis.core import (QgsGeometry, - QgsWkbTypes, - QgsProcessingParameterBoolean) - - -from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm - -pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] - - -class DropMZValues(QgisFeatureBasedAlgorithm): - - DROP_M_VALUES = 'DROP_M_VALUES' - DROP_Z_VALUES = 'DROP_Z_VALUES' - - def group(self): - return self.tr('Vector geometry') - - def __init__(self): - super().__init__() - self.drop_m = False - self.drop_z = False - - def name(self): - return 'dropmzvalues' - - def displayName(self): - return self.tr('Drop M/Z values') - - def outputName(self): - return self.tr('Z/M Dropped') - - def tags(self): - return self.tr('drop,set,convert,m,measure,z,25d,3d,values').split(',') - - def initParameters(self, config=None): - self.addParameter(QgsProcessingParameterBoolean(self.DROP_M_VALUES, - self.tr('Drop M Values'), defaultValue=False)) - self.addParameter(QgsProcessingParameterBoolean(self.DROP_Z_VALUES, - self.tr('Drop Z Values'), defaultValue=False)) - - def outputWkbType(self, inputWkb): - wkb = inputWkb - if self.drop_m: - wkb = QgsWkbTypes.dropM(wkb) - if self.drop_z: - wkb = QgsWkbTypes.dropZ(wkb) - return wkb - - def prepareAlgorithm(self, parameters, context, feedback): - self.drop_m = self.parameterAsBool(parameters, self.DROP_M_VALUES, context) - self.drop_z = self.parameterAsBool(parameters, self.DROP_Z_VALUES, context) - return True - - def processFeature(self, feature, feedback): - input_geometry = feature.geometry() - if input_geometry: - new_geom = input_geometry.geometry().clone() - if self.drop_m: - new_geom.dropMValue() - if self.drop_z: - new_geom.dropZValue() - feature.setGeometry(QgsGeometry(new_geom)) - - return feature diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index 2ccdadf311e..796f6bb4243 100755 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -57,7 +57,6 @@ from .DeleteHoles import DeleteHoles from .DensifyGeometries import DensifyGeometries from .DensifyGeometriesInterval import DensifyGeometriesInterval from .Difference import Difference -from .DropMZValues import DropMZValues from .EliminateSelection import EliminateSelection from .EquivalentNumField import EquivalentNumField from .ExecuteSQL import ExecuteSQL @@ -185,7 +184,6 @@ class QGISAlgorithmProvider(QgsProcessingProvider): DensifyGeometries(), DensifyGeometriesInterval(), Difference(), - DropMZValues(), EliminateSelection(), EquivalentNumField(), ExecuteSQL(), diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index d08a0eda34f..56e58ad6a0a 100755 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -733,7 +733,7 @@ tests: name: expected/set_z_value.shp type: vector - - algorithm: qgis:dropmzvalues + - algorithm: native:dropmzvalues name: Drop M Value params: INPUT: @@ -746,7 +746,7 @@ tests: name: expected/m_dropped.shp type: vector - - algorithm: qgis:dropmzvalues + - algorithm: native:dropmzvalues name: Drop Z Value params: INPUT: @@ -759,7 +759,7 @@ tests: name: expected/z_dropped.shp type: vector - - algorithm: qgis:dropmzvalues + - algorithm: native:dropmzvalues name: Drop ZM Value params: INPUT: diff --git a/src/core/processing/qgsnativealgorithms.cpp b/src/core/processing/qgsnativealgorithms.cpp index a4a4d7eae53..6fc3e788e51 100644 --- a/src/core/processing/qgsnativealgorithms.cpp +++ b/src/core/processing/qgsnativealgorithms.cpp @@ -96,6 +96,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsAddIncrementalFieldAlgorithm() ); addAlgorithm( new QgsBoundaryAlgorithm() ); addAlgorithm( new QgsDropGeometryAlgorithm() ); + addAlgorithm( new QgsDropMZValuesAlgorithm() ); } void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & ) @@ -3295,6 +3296,56 @@ QgsFeature QgsDropGeometryAlgorithm::processFeature( const QgsFeature &feature, return f; } +QString QgsDropMZValuesAlgorithm::shortHelpString() const +{ + return QObject::tr( "This algorithm can remove any measure (M) or Z values from input geometries." ); +} + +QgsDropMZValuesAlgorithm *QgsDropMZValuesAlgorithm::createInstance() const +{ + return new QgsDropMZValuesAlgorithm(); +} + +void QgsDropMZValuesAlgorithm::initParameters( const QVariantMap & ) +{ + addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DROP_M_VALUES" ), QObject::tr( "Drop M Values" ), false ) ); + addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DROP_Z_VALUES" ), QObject::tr( "Drop Z Values" ), false ) ); +} + +QgsWkbTypes::Type QgsDropMZValuesAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const +{ + QgsWkbTypes::Type wkb = inputWkbType; + if ( mDropM ) + wkb = QgsWkbTypes::dropM( wkb ); + if ( mDropZ ) + wkb = QgsWkbTypes::dropZ( wkb ); + return wkb; +} + +bool QgsDropMZValuesAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) +{ + mDropM = parameterAsBool( parameters, QStringLiteral( "DROP_M_VALUES" ), context ); + mDropZ = parameterAsBool( parameters, QStringLiteral( "DROP_Z_VALUES" ), context ); + return true; +} + +QgsFeature QgsDropMZValuesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * ) +{ + QgsFeature f = feature; + if ( f.hasGeometry() ) + { + std::unique_ptr< QgsAbstractGeometry > newGeom( f.geometry().geometry()->clone() ); + if ( mDropM ) + newGeom->dropMValue(); + if ( mDropZ ) + newGeom->dropZValue(); + f.setGeometry( QgsGeometry( newGeom.release() ) ); + } + + return f; +} ///@endcond + + diff --git a/src/core/processing/qgsnativealgorithms.h b/src/core/processing/qgsnativealgorithms.h index 72a3527b075..0b06ce7a578 100644 --- a/src/core/processing/qgsnativealgorithms.h +++ b/src/core/processing/qgsnativealgorithms.h @@ -147,6 +147,36 @@ class QgsDropGeometryAlgorithm : public QgsProcessingFeatureBasedAlgorithm QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override; }; +/** + * Native drop M/Z values algorithm. + */ +class QgsDropMZValuesAlgorithm : public QgsProcessingFeatureBasedAlgorithm +{ + + public: + + QgsDropMZValuesAlgorithm() = default; + QString name() const override { return QStringLiteral( "dropmzvalues" ); } + QString displayName() const override { return QObject::tr( "Drop M/Z values" ); } + QStringList tags() const override { return QObject::tr( "drop,set,convert,m,measure,z,25d,3d,values" ).split( ',' ); } + QString group() const override { return QObject::tr( "Vector geometry" ); } + QString shortHelpString() const override; + QgsDropMZValuesAlgorithm *createInstance() const override SIP_FACTORY; + + protected: + + void initParameters( const QVariantMap &configuration = QVariantMap() ) override; + QString outputName() const override { return QObject::tr( "Z/M Dropped" ); } + QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override; + bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; + QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override; + + private: + + bool mDropM = false; + bool mDropZ = false; +}; + /** * Native transform algorithm. */