QGIS API Documentation 3.99.0-Master (09f76ad7019)
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( "This algorithm removes duplicate vertices from features, wherever removing the vertices does "
59 "not result in a degenerate geometry.\n\n"
60 "The tolerance parameter specifies the tolerance for coordinates when determining whether "
61 "vertices are identical.\n\n"
62 "By default, z values are not considered when detecting duplicate vertices. E.g. two vertices "
63 "with the same x and y coordinate but different z values will still be considered "
64 "duplicate and one will be removed. If the Use Z Value parameter is true, then the z values are "
65 "also tested and vertices with the same x and y but different z will be maintained.\n\n"
66 "Note that duplicate vertices are not tested between different parts of a multipart geometry. E.g. "
67 "a multipoint geometry with overlapping points will not be changed by this method." );
68}
69
70QString QgsAlgorithmRemoveDuplicateVertices::shortDescription() const
71{
72 return QObject::tr( "Removes duplicate vertices from features, wherever removing the vertices does "
73 "not result in a degenerate geometry." );
74}
75
76QgsAlgorithmRemoveDuplicateVertices *QgsAlgorithmRemoveDuplicateVertices::createInstance() const
77{
78 return new QgsAlgorithmRemoveDuplicateVertices();
79}
80
81void QgsAlgorithmRemoveDuplicateVertices::initParameters( const QVariantMap & )
82{
83 auto tolerance = std::make_unique<QgsProcessingParameterDistance>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), 0.000001, u"INPUT"_s, false, 0, 10000000.0 );
84 tolerance->setIsDynamic( true );
85 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Tolerance"_s, QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
86 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
87 addParameter( tolerance.release() );
88
89 auto useZ = std::make_unique<QgsProcessingParameterBoolean>( u"USE_Z_VALUE"_s, QObject::tr( "Use Z Value" ), false );
90 useZ->setIsDynamic( true );
91 useZ->setDynamicPropertyDefinition( QgsPropertyDefinition( u"UseZ"_s, QObject::tr( "Use Z Value" ), QgsPropertyDefinition::Boolean ) );
92 useZ->setDynamicLayerParameterName( u"INPUT"_s );
93 addParameter( useZ.release() );
94}
95
96Qgis::ProcessingFeatureSourceFlags QgsAlgorithmRemoveDuplicateVertices::sourceFlags() const
97{
98 // skip geometry checks - this algorithm can be used to repair geometries
100}
101
102bool QgsAlgorithmRemoveDuplicateVertices::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
103{
104 mTolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
105 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"TOLERANCE"_s );
106 if ( mDynamicTolerance )
107 mToleranceProperty = parameters.value( u"TOLERANCE"_s ).value<QgsProperty>();
108
109 mUseZValues = parameterAsBoolean( parameters, u"USE_Z_VALUE"_s, context );
110 mDynamicUseZ = QgsProcessingParameters::isDynamic( parameters, u"USE_Z_VALUE"_s );
111 if ( mDynamicUseZ )
112 mUseZProperty = parameters.value( u"USE_Z_VALUE"_s ).value<QgsProperty>();
113
114 return true;
115}
116
117QgsFeatureList QgsAlgorithmRemoveDuplicateVertices::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
118{
119 QgsFeature f = feature;
120 if ( f.hasGeometry() )
121 {
122 QgsGeometry geometry = f.geometry();
123 double tolerance = mTolerance;
124 if ( mDynamicTolerance )
125 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
126
127 bool useZValue = mUseZValues;
128 if ( mDynamicUseZ )
129 useZValue = mUseZProperty.valueAsBool( context.expressionContext(), useZValue );
130
131 geometry.removeDuplicateNodes( tolerance, useZValue );
132 f.setGeometry( geometry );
133 }
134 return QgsFeatureList() << f;
135}
136
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
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:53
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
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