24QString QgsCheckValidityAlgorithm::name()
const
26 return QStringLiteral(
"checkvalidity" );
29QString QgsCheckValidityAlgorithm::displayName()
const
31 return QObject::tr(
"Check validity" );
34QStringList QgsCheckValidityAlgorithm::tags()
const
36 return QObject::tr(
"valid,invalid,detect,error" ).split(
',' );
39QString QgsCheckValidityAlgorithm::group()
const
41 return QObject::tr(
"Vector geometry" );
44QString QgsCheckValidityAlgorithm::groupId()
const
46 return QStringLiteral(
"vectorgeometry" );
49QString QgsCheckValidityAlgorithm::shortHelpString()
const
51 return QObject::tr(
"Performs a validity check on the geometries of a vector layer.\n\n"
52 "The geometries are classified in three groups (valid, invalid and error), and a vector layer "
53 "is generated with the features in each of these categories.\n\n"
54 "By default the algorithm uses the strict OGC definition of polygon validity, where a polygon "
55 "is marked as invalid if a self-intersecting ring causes an interior hole. If the 'Ignore "
56 "ring self intersections' option is checked, then this rule will be ignored and a more "
57 "lenient validity check will be performed.\n\n"
58 "The GEOS method is faster and performs better on larger geometries, but is limited to only "
59 "returning the first error encountered in a geometry. The QGIS method will be slower but "
60 "reports all errors encountered in the geometry, not just the first." );
63QgsCheckValidityAlgorithm *QgsCheckValidityAlgorithm::createInstance()
const
65 return new QgsCheckValidityAlgorithm();
68void QgsCheckValidityAlgorithm::initAlgorithm(
const QVariantMap & )
72 const QStringList options = QStringList()
73 << QObject::tr(
"The one selected in digitizing settings" )
74 << QStringLiteral(
"QGIS" )
75 << QStringLiteral(
"GEOS" );
76 auto methodParam = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), options,
false, 2 );
77 QVariantMap methodParamMetadata;
78 QVariantMap widgetMetadata;
79 widgetMetadata.insert( QStringLiteral(
"useCheckBoxes" ),
true );
80 widgetMetadata.insert( QStringLiteral(
"columns" ), 3 );
81 methodParamMetadata.insert( QStringLiteral(
"widget_wrapper" ), widgetMetadata );
82 methodParam->setMetadata( methodParamMetadata );
83 addParameter( methodParam.release() );
85 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"IGNORE_RING_SELF_INTERSECTION" ), QObject::tr(
"Ignore ring self intersections" ),
false ) );
90 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"INVALID_COUNT" ), QObject::tr(
"Count of invalid features" ) ) );
96 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT_LAYER" ), context ) );
100 int method = parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context );
104 method = methodFromSettings > 0 ? methodFromSettings : 0;
111 const bool ignoreRingSelfIntersection = parameterAsBool( parameters, QStringLiteral(
"IGNORE_RING_SELF_INTERSECTION" ), context );
116 std::unique_ptr<QgsFeatureSink> validSink( parameterAsSink( parameters, QStringLiteral(
"VALID_OUTPUT" ), context, validSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
118 QgsFields invalidFields = source->fields();
119 invalidFields.
append(
QgsField( QStringLiteral(
"_errors" ), QMetaType::Type::QString, QStringLiteral(
"string" ), 255 ) );
120 QString invalidSinkId;
121 std::unique_ptr<QgsFeatureSink> invalidSink( parameterAsSink( parameters, QStringLiteral(
"INVALID_OUTPUT" ), context, invalidSinkId, invalidFields, source->wkbType(), source->sourceCrs() ) );
124 errorFields.
append(
QgsField( QStringLiteral(
"message" ), QMetaType::Type::QString, QStringLiteral(
"string" ), 255 ) );
126 std::unique_ptr<QgsFeatureSink> errorSink( parameterAsSink( parameters, QStringLiteral(
"ERROR_OUTPUT" ), context, errorSinkId, errorFields,
Qgis::WkbType::Point, source->sourceCrs() ) );
129 int invalidCount = 0;
132 const long count = source->featureCount();
133 const double step = count > 0 ? 100.0 / count : 1;
134 long long current = 0;
152 QVector< QgsGeometry::Error > errors;
154 if ( errors.count() > 0 )
158 reasons.reserve( errors.count() );
168 throw QgsProcessingException( writeFeatureError( errorSink.get(), parameters, QStringLiteral(
"ERROR_OUTPUT" ) ) );
172 reasons.append( error.what() );
174 QString reason = reasons.join(
'\n' );
175 if ( reason.size() > 255 )
177 reason = reason.left( 252 ) + QStringLiteral(
"…" );
179 attrs.append( reason );
191 throw QgsProcessingException( writeFeatureError( validSink.get(), parameters, QStringLiteral(
"VALID_OUTPUT" ) ) );
199 throw QgsProcessingException( writeFeatureError( invalidSink.get(), parameters, QStringLiteral(
"INVALID_OUTPUT" ) ) );
210 validSink->finalize();
214 invalidSink->finalize();
218 errorSink->finalize();
222 outputs.insert( QStringLiteral(
"VALID_COUNT" ), validCount );
223 outputs.insert( QStringLiteral(
"INVALID_COUNT" ), invalidCount );
224 outputs.insert( QStringLiteral(
"ERROR_COUNT" ), errorCount );
228 outputs.insert( QStringLiteral(
"VALID_OUTPUT" ), validSinkId );
232 outputs.insert( QStringLiteral(
"INVALID_OUTPUT" ), invalidSinkId );
236 outputs.insert( QStringLiteral(
"ERROR_OUTPUT" ), errorSinkId );
@ VectorAnyGeometry
Any vector layer with geometry.
@ AllowSelfTouchingHoles
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
GeometryValidationEngine
Available engines for validating geometries.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryInteger * settingsDigitizingValidateGeometries
Settings entry digitizing validate geometries.