QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmcheckvalidity.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcheckvalidity.cpp
3 ---------------------
4 begin : February 2025
5 copyright : (C) 2025 by Alexander Bruy
6 email : alexander dot bruy 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#include "qgsvectorlayer.h"
22
24
25QString QgsCheckValidityAlgorithm::name() const
26{
27 return QStringLiteral( "checkvalidity" );
28}
29
30QString QgsCheckValidityAlgorithm::displayName() const
31{
32 return QObject::tr( "Check validity" );
33}
34
35QStringList QgsCheckValidityAlgorithm::tags() const
36{
37 return QObject::tr( "valid,invalid,detect,error" ).split( ',' );
38}
39
40QString QgsCheckValidityAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsCheckValidityAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsCheckValidityAlgorithm::shortHelpString() const
51{
52 return QObject::tr( "This algorithm performs a validity check on the geometries of a vector layer.\n\n"
53 "The geometries are classified in three groups (valid, invalid and error), and a vector layer "
54 "is generated with the features in each of these categories.\n\n"
55 "By default the algorithm uses the strict OGC definition of polygon validity, where a polygon "
56 "is marked as invalid if a self-intersecting ring causes an interior hole. If the 'Ignore "
57 "ring self intersections' option is checked, then this rule will be ignored and a more "
58 "lenient validity check will be performed.\n\n"
59 "The GEOS method is faster and performs better on larger geometries, but is limited to only "
60 "returning the first error encountered in a geometry. The QGIS method will be slower but "
61 "reports all errors encountered in the geometry, not just the first." );
62}
63
64QString QgsCheckValidityAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Performs a validity check on the geometries of a vector layer "
67 "and classifies them in three groups (valid, invalid and error)." );
68}
69
70QgsCheckValidityAlgorithm *QgsCheckValidityAlgorithm::createInstance() const
71{
72 return new QgsCheckValidityAlgorithm();
73}
74
75void QgsCheckValidityAlgorithm::initAlgorithm( const QVariantMap & )
76{
77 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT_LAYER" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorAnyGeometry ) ) );
78
79 const QStringList options = QStringList()
80 << QObject::tr( "The one selected in digitizing settings" )
81 << QStringLiteral( "QGIS" )
82 << QStringLiteral( "GEOS" );
83 auto methodParam = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), options, false, 2 );
84 QVariantMap methodParamMetadata;
85 QVariantMap widgetMetadata;
86 widgetMetadata.insert( QStringLiteral( "useCheckBoxes" ), true );
87 widgetMetadata.insert( QStringLiteral( "columns" ), 3 );
88 methodParamMetadata.insert( QStringLiteral( "widget_wrapper" ), widgetMetadata );
89 methodParam->setMetadata( methodParamMetadata );
90 addParameter( methodParam.release() );
91
92 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "IGNORE_RING_SELF_INTERSECTION" ), QObject::tr( "Ignore ring self intersections" ), false ) );
93 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "VALID_OUTPUT" ), QObject::tr( "Valid output" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true ) );
94 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "INVALID_OUTPUT" ), QObject::tr( "Invalid output" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true ) );
95 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "ERROR_OUTPUT" ), QObject::tr( "Error output" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true ) );
96 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "VALID_COUNT" ), QObject::tr( "Count of valid features" ) ) );
97 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "INVALID_COUNT" ), QObject::tr( "Count of invalid features" ) ) );
98 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "ERROR_COUNT" ), QObject::tr( "Count of errors" ) ) );
99}
100
101QVariantMap QgsCheckValidityAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
102{
103 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT_LAYER" ), context ) );
104 if ( !source )
105 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT_LAYER" ) ) );
106
107 int method = parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context );
108 if ( method == 0 )
109 {
110 const int methodFromSettings = QgsSettingsRegistryCore::settingsDigitizingValidateGeometries->value() - 1;
111 method = methodFromSettings > 0 ? methodFromSettings : 0;
112 }
113 else
114 {
115 method--;
116 }
117
118 const bool ignoreRingSelfIntersection = parameterAsBool( parameters, QStringLiteral( "IGNORE_RING_SELF_INTERSECTION" ), context );
119
121
122 QString validSinkId;
123 std::unique_ptr<QgsFeatureSink> validSink( parameterAsSink( parameters, QStringLiteral( "VALID_OUTPUT" ), context, validSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
124
125 QgsFields invalidFields = source->fields();
126 invalidFields.append( QgsField( QStringLiteral( "_errors" ), QMetaType::Type::QString, QStringLiteral( "string" ), 255 ) );
127 QString invalidSinkId;
128 std::unique_ptr<QgsFeatureSink> invalidSink( parameterAsSink( parameters, QStringLiteral( "INVALID_OUTPUT" ), context, invalidSinkId, invalidFields, source->wkbType(), source->sourceCrs() ) );
129
130 QgsFields errorFields;
131 errorFields.append( QgsField( QStringLiteral( "message" ), QMetaType::Type::QString, QStringLiteral( "string" ), 255 ) );
132 QString errorSinkId;
133 std::unique_ptr<QgsFeatureSink> errorSink( parameterAsSink( parameters, QStringLiteral( "ERROR_OUTPUT" ), context, errorSinkId, errorFields, Qgis::WkbType::Point, source->sourceCrs() ) );
134
135 int validCount = 0;
136 int invalidCount = 0;
137 int errorCount = 0;
138
139 const long count = source->featureCount();
140 const double step = count > 0 ? 100.0 / count : 1;
141 long long current = 0;
142
144 QgsFeature f;
145 while ( it.nextFeature( f ) )
146 {
147 if ( feedback->isCanceled() )
148 {
149 break;
150 }
151
152 const QgsGeometry geom = f.geometry();
153 QgsAttributes attrs = f.attributes();
154
155 bool isValid = true;
156
157 if ( !geom.isNull() && !geom.isEmpty() )
158 {
159 QVector< QgsGeometry::Error > errors;
160 geom.validateGeometry( errors, Qgis::GeometryValidationEngine( method ), flags );
161 if ( errors.count() > 0 )
162 {
163 isValid = false;
164 QStringList reasons;
165 reasons.reserve( errors.count() );
166 for ( const QgsGeometry::Error &error : std::as_const( errors ) )
167 {
168 if ( errorSink )
169 {
170 QgsFeature f;
171 f.setGeometry( QgsGeometry::fromPointXY( error.where() ) );
172 f.setAttributes( QVector< QVariant >() << error.what() );
173 if ( !errorSink->addFeature( f, QgsFeatureSink::FastInsert ) )
174 {
175 throw QgsProcessingException( writeFeatureError( errorSink.get(), parameters, QStringLiteral( "ERROR_OUTPUT" ) ) );
176 }
177 }
178 errorCount++;
179 reasons.append( error.what() );
180 }
181 QString reason = reasons.join( '\n' );
182 if ( reason.size() > 255 )
183 {
184 reason = reason.left( 252 ) + QStringLiteral( "…" );
185 }
186 attrs.append( reason );
187 }
188 }
189
190 QgsFeature f;
191 f.setGeometry( geom );
192 f.setAttributes( attrs );
193
194 if ( isValid )
195 {
196 if ( validSink && !validSink->addFeature( f, QgsFeatureSink::FastInsert ) )
197 {
198 throw QgsProcessingException( writeFeatureError( validSink.get(), parameters, QStringLiteral( "VALID_OUTPUT" ) ) );
199 }
200 validCount++;
201 }
202 else
203 {
204 if ( invalidSink && !invalidSink->addFeature( f, QgsFeatureSink::FastInsert ) )
205 {
206 throw QgsProcessingException( writeFeatureError( invalidSink.get(), parameters, QStringLiteral( "INVALID_OUTPUT" ) ) );
207 }
208 invalidCount++;
209 }
210
211 feedback->setProgress( current * step );
212 current++;
213 }
214
215 if ( validSink )
216 {
217 validSink->finalize();
218 }
219 if ( invalidSink )
220 {
221 invalidSink->finalize();
222 }
223 if ( errorSink )
224 {
225 errorSink->finalize();
226 }
227
228 QVariantMap outputs;
229 outputs.insert( QStringLiteral( "VALID_COUNT" ), validCount );
230 outputs.insert( QStringLiteral( "INVALID_COUNT" ), invalidCount );
231 outputs.insert( QStringLiteral( "ERROR_COUNT" ), errorCount );
232
233 if ( validSink )
234 {
235 outputs.insert( QStringLiteral( "VALID_OUTPUT" ), validSinkId );
236 }
237 if ( invalidSink )
238 {
239 outputs.insert( QStringLiteral( "INVALID_OUTPUT" ), invalidSinkId );
240 }
241 if ( errorSink )
242 {
243 outputs.insert( QStringLiteral( "ERROR_OUTPUT" ), errorSinkId );
244 }
245
246 return outputs;
247}
248
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3533
@ AllowSelfTouchingHoles
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
Definition qgis.h:2072
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
Definition qgis.h:2075
GeometryValidationEngine
Available engines for validating geometries.
Definition qgis.h:2084
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
@ Point
Point.
Definition qgis.h:279
A vector of attributes.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:73
A geometry error.
A geometry is the spatial representation of a feature.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
void validateGeometry(QVector< QgsGeometry::Error > &errors, Qgis::GeometryValidationEngine method=Qgis::GeometryValidationEngine::QgisInternal, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const
Validates geometry and produces a list of geometry errors.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A numeric output for processing algorithms.
A boolean parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
static const QgsSettingsEntryInteger * settingsDigitizingValidateGeometries
Settings entry digitizing validate geometries.