25using namespace Qt::StringLiterals;
29QString QgsCheckValidityAlgorithm::name()
const
31 return u
"checkvalidity"_s;
34QString QgsCheckValidityAlgorithm::displayName()
const
36 return QObject::tr(
"Check validity" );
39QStringList QgsCheckValidityAlgorithm::tags()
const
41 return QObject::tr(
"valid,invalid,detect,error" ).split(
',' );
44QString QgsCheckValidityAlgorithm::group()
const
46 return QObject::tr(
"Vector geometry" );
49QString QgsCheckValidityAlgorithm::groupId()
const
51 return u
"vectorgeometry"_s;
54QString QgsCheckValidityAlgorithm::shortHelpString()
const
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." );
68QString QgsCheckValidityAlgorithm::shortDescription()
const
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)." );
74QgsCheckValidityAlgorithm *QgsCheckValidityAlgorithm::createInstance()
const
76 return new QgsCheckValidityAlgorithm();
79void QgsCheckValidityAlgorithm::initAlgorithm(
const QVariantMap & )
83 const QStringList options = QStringList()
84 << QObject::tr(
"The one selected in digitizing settings" )
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() );
96 addParameter(
new QgsProcessingParameterBoolean( u
"IGNORE_RING_SELF_INTERSECTION"_s, QObject::tr(
"Ignore ring self intersections" ),
false ) );
107 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT_LAYER"_s, context ) );
111 int method = parameterAsEnum( parameters, u
"METHOD"_s, context );
115 method = methodFromSettings > 0 ? methodFromSettings : 0;
122 const bool ignoreRingSelfIntersection = parameterAsBool( parameters, u
"IGNORE_RING_SELF_INTERSECTION"_s, context );
127 std::unique_ptr<QgsFeatureSink> validSink( parameterAsSink( parameters, u
"VALID_OUTPUT"_s, context, validSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
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() ) );
135 errorFields.
append(
QgsField( u
"message"_s, QMetaType::Type::QString, u
"string"_s, 255 ) );
137 std::unique_ptr<QgsFeatureSink> errorSink( parameterAsSink( parameters, u
"ERROR_OUTPUT"_s, context, errorSinkId, errorFields,
Qgis::WkbType::Point, source->sourceCrs() ) );
140 int invalidCount = 0;
143 const long count = source->featureCount();
144 const double step = count > 0 ? 100.0 / count : 1;
145 long long current = 0;
163 QVector< QgsGeometry::Error > errors;
165 if ( errors.count() > 0 )
169 reasons.reserve( errors.count() );
183 reasons.append( error.what() );
185 QString reason = reasons.join(
'\n' );
186 if ( reason.size() > 255 )
188 reason = reason.left( 252 ) + u
"…"_s;
190 attrs.append( reason );
221 validSink->finalize();
225 invalidSink->finalize();
229 errorSink->finalize();
233 outputs.insert( u
"VALID_COUNT"_s, validCount );
234 outputs.insert( u
"INVALID_COUNT"_s, invalidCount );
235 outputs.insert( u
"ERROR_COUNT"_s, errorCount );
239 outputs.insert( u
"VALID_OUTPUT"_s, validSinkId );
243 outputs.insert( u
"INVALID_OUTPUT"_s, invalidSinkId );
247 outputs.insert( u
"ERROR_OUTPUT"_s, 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.
static const QgsSettingsEntryInteger * settingsDigitizingValidateGeometries
Settings entry digitizing validate geometries.