QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
21 
22 QString QgsAlgorithmRemoveDuplicateVertices::name() const
23 {
24  return QStringLiteral( "removeduplicatevertices" );
25 }
26 
27 QString QgsAlgorithmRemoveDuplicateVertices::displayName() const
28 {
29  return QObject::tr( "Remove duplicate vertices" );
30 }
31 
32 QStringList QgsAlgorithmRemoveDuplicateVertices::tags() const
33 {
34  return QObject::tr( "points,valid,overlapping,vertex,nodes" ).split( ',' );
35 }
36 
37 QString QgsAlgorithmRemoveDuplicateVertices::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsAlgorithmRemoveDuplicateVertices::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsAlgorithmRemoveDuplicateVertices::outputName() const
48 {
49  return QObject::tr( "Cleaned" );
50 }
51 
52 QString QgsAlgorithmRemoveDuplicateVertices::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm removes duplicate vertices from features, wherever removing the vertices does "
55  "not result in a degenerate geometry.\n\n"
56  "The tolerance parameter specifies the tolerance for coordinates when determining whether "
57  "vertices are identical.\n\n"
58  "By default, z values are not considered when detecting duplicate vertices. E.g. two vertices "
59  "with the same x and y coordinate but different z values will still be considered "
60  "duplicate and one will be removed. If the Use Z Value parameter is true, then the z values are "
61  "also tested and vertices with the same x and y but different z will be maintained.\n\n"
62  "Note that duplicate vertices are not tested between different parts of a multipart geometry. E.g. "
63  "a multipoint geometry with overlapping points will not be changed by this method." );
64 }
65 
66 QgsAlgorithmRemoveDuplicateVertices *QgsAlgorithmRemoveDuplicateVertices::createInstance() const
67 {
68  return new QgsAlgorithmRemoveDuplicateVertices();
69 }
70 
71 void QgsAlgorithmRemoveDuplicateVertices::initParameters( const QVariantMap & )
72 {
73  std::unique_ptr< QgsProcessingParameterDistance> tolerance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "TOLERANCE" ),
74  QObject::tr( "Tolerance" ), 0.000001, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
75  tolerance->setIsDynamic( true );
76  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
77  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
78  addParameter( tolerance.release() );
79 
80  std::unique_ptr< QgsProcessingParameterBoolean > useZ = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "USE_Z_VALUE" ),
81  QObject::tr( "Use Z Value" ), false );
82  useZ->setIsDynamic( true );
83  useZ->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "UseZ" ), QObject::tr( "Use Z Value" ), QgsPropertyDefinition::Boolean ) );
84  useZ->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
85  addParameter( useZ.release() );
86 }
87 
88 QgsProcessingFeatureSource::Flag QgsAlgorithmRemoveDuplicateVertices::sourceFlags() const
89 {
90  // skip geometry checks - this algorithm can be used to repair geometries
92 }
93 
94 bool QgsAlgorithmRemoveDuplicateVertices::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
95 {
96  mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
97  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "TOLERANCE" ) );
98  if ( mDynamicTolerance )
99  mToleranceProperty = parameters.value( QStringLiteral( "TOLERANCE" ) ).value< QgsProperty >();
100 
101  mUseZValues = parameterAsBoolean( parameters, QStringLiteral( "USE_Z_VALUE" ), context );
102  mDynamicUseZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "USE_Z_VALUE" ) );
103  if ( mDynamicUseZ )
104  mUseZProperty = parameters.value( QStringLiteral( "USE_Z_VALUE" ) ).value< QgsProperty >();
105 
106  return true;
107 }
108 
109 QgsFeatureList QgsAlgorithmRemoveDuplicateVertices::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
110 {
111  QgsFeature f = feature;
112  if ( f.hasGeometry() )
113  {
114  QgsGeometry geometry = f.geometry();
115  double tolerance = mTolerance;
116  if ( mDynamicTolerance )
117  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
118 
119  bool useZValue = mUseZValues;
120  if ( mDynamicUseZ )
121  useZValue = mUseZProperty.valueAsBool( context.expressionContext(), useZValue );
122 
123  geometry.removeDuplicateNodes( tolerance, useZValue );
124  f.setGeometry( geometry );
125  }
126  return QgsFeatureList() << f;
127 }
128 
130 
131 
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:205
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:145
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
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.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
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:48
@ Boolean
Boolean value.
Definition: qgsproperty.h:54
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59
A store for object properties.
Definition: qgsproperty.h:232
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
Definition: qgsfeature.h:736