QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmremoveduplicatevertices.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmremoveduplicatevertices.cpp
3 ---------------------
4 begin : November 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsAlgorithmRemoveDuplicateVertices::name() const
27{
28 return u"removeduplicatevertices"_s;
29}
30
31QString QgsAlgorithmRemoveDuplicateVertices::displayName() const
32{
33 return QObject::tr( "Remove duplicate vertices" );
34}
35
36QStringList QgsAlgorithmRemoveDuplicateVertices::tags() const
37{
38 return QObject::tr( "points,valid,overlapping,vertex,nodes,invalid,error,repair" ).split( ',' );
39}
40
41QString QgsAlgorithmRemoveDuplicateVertices::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsAlgorithmRemoveDuplicateVertices::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsAlgorithmRemoveDuplicateVertices::outputName() const
52{
53 return QObject::tr( "Cleaned" );
54}
55
56QString QgsAlgorithmRemoveDuplicateVertices::shortHelpString() const
57{
58 return QObject::tr(
59 "This algorithm removes duplicate vertices from features, wherever removing the vertices does "
60 "not result in a degenerate geometry.\n\n"
61 "The tolerance parameter specifies the tolerance for coordinates when determining whether "
62 "vertices are identical.\n\n"
63 "By default, z values are not considered when detecting duplicate vertices. E.g. two vertices "
64 "with the same x and y coordinate but different z values will still be considered "
65 "duplicate and one will be removed. If the Use Z Value parameter is true, then the z values are "
66 "also tested and vertices with the same x and y but different z will be maintained.\n\n"
67 "Note that duplicate vertices are not tested between different parts of a multipart geometry. E.g. "
68 "a multipoint geometry with overlapping points will not be changed by this method."
69 );
70}
71
72QString QgsAlgorithmRemoveDuplicateVertices::shortDescription() const
73{
74 return QObject::tr(
75 "Removes duplicate vertices from features, wherever removing the vertices does "
76 "not result in a degenerate geometry."
77 );
78}
79
80QgsAlgorithmRemoveDuplicateVertices *QgsAlgorithmRemoveDuplicateVertices::createInstance() const
81{
82 return new QgsAlgorithmRemoveDuplicateVertices();
83}
84
85void QgsAlgorithmRemoveDuplicateVertices::initParameters( const QVariantMap & )
86{
87 auto tolerance = std::make_unique<QgsProcessingParameterDistance>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), 0.000001, u"INPUT"_s, false, 0, 10000000.0 );
88 tolerance->setIsDynamic( true );
89 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Tolerance"_s, QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
90 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
91 addParameter( tolerance.release() );
92
93 auto useZ = std::make_unique<QgsProcessingParameterBoolean>( u"USE_Z_VALUE"_s, QObject::tr( "Use Z Value" ), false );
94 useZ->setIsDynamic( true );
95 useZ->setDynamicPropertyDefinition( QgsPropertyDefinition( u"UseZ"_s, QObject::tr( "Use Z Value" ), QgsPropertyDefinition::Boolean ) );
96 useZ->setDynamicLayerParameterName( u"INPUT"_s );
97 addParameter( useZ.release() );
98}
99
100Qgis::ProcessingFeatureSourceFlags QgsAlgorithmRemoveDuplicateVertices::sourceFlags() const
101{
102 // skip geometry checks - this algorithm can be used to repair geometries
104}
105
106bool QgsAlgorithmRemoveDuplicateVertices::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
107{
108 mTolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
109 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"TOLERANCE"_s );
110 if ( mDynamicTolerance )
111 mToleranceProperty = parameters.value( u"TOLERANCE"_s ).value<QgsProperty>();
112
113 mUseZValues = parameterAsBoolean( parameters, u"USE_Z_VALUE"_s, context );
114 mDynamicUseZ = QgsProcessingParameters::isDynamic( parameters, u"USE_Z_VALUE"_s );
115 if ( mDynamicUseZ )
116 mUseZProperty = parameters.value( u"USE_Z_VALUE"_s ).value<QgsProperty>();
117
118 return true;
119}
120
121QgsFeatureList QgsAlgorithmRemoveDuplicateVertices::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
122{
123 QgsFeature f = feature;
124 if ( f.hasGeometry() )
125 {
126 QgsGeometry geometry = f.geometry();
127 double tolerance = mTolerance;
128 if ( mDynamicTolerance )
129 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
130
131 bool useZValue = mUseZValues;
132 if ( mDynamicUseZ )
133 useZValue = mUseZProperty.valueAsBool( context.expressionContext(), useZValue );
134
135 geometry.removeDuplicateNodes( tolerance, useZValue );
136 f.setGeometry( geometry );
137 }
138 return QgsFeatureList() << f;
139}
140
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false)
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:47
@ Boolean
Boolean value.
Definition qgsproperty.h:52
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:57
A store for object properties.
bool valueAsBool(const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an boolean.
QList< QgsFeature > QgsFeatureList