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