QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmcheckgeometrygap.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcheckgeometrygap.cpp
3 ---------------------
4 begin : April 2025
5 copyright : (C) 2025 by Jacky Volpes
6 email : jacky dot volpes at oslandia 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
22#include "qgsgeometrygapcheck.h"
23#include "qgspoint.h"
25#include "qgsvectorlayer.h"
26
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32
33QString QgsGeometryCheckGapAlgorithm::name() const
34{
35 return u"checkgeometrygap"_s;
36}
37
38QString QgsGeometryCheckGapAlgorithm::displayName() const
39{
40 return QObject::tr( "Small gaps" );
41}
42
43QString QgsGeometryCheckGapAlgorithm::shortDescription() const
44{
45 return QObject::tr( "Detects gaps between polygons smaller than a given area." );
46}
47
48QStringList QgsGeometryCheckGapAlgorithm::tags() const
49{
50 return QObject::tr( "check,geometry,gap" ).split( ',' );
51}
52
53QString QgsGeometryCheckGapAlgorithm::group() const
54{
55 return QObject::tr( "Check geometry" );
56}
57
58QString QgsGeometryCheckGapAlgorithm::groupId() const
59{
60 return u"checkgeometry"_s;
61}
62
63QString QgsGeometryCheckGapAlgorithm::shortHelpString() const
64{
65 return QObject::tr(
66 "This algorithm checks the gaps between polygons.\n"
67 "Gaps with an area smaller than the gap threshold are errors.\n\n"
68 "If an allowed gaps layer is given, the gaps contained in polygons from this layer will be ignored.\n"
69 "An optional buffer can be applied to the allowed gaps.\n\n"
70 "The neighbors output layer is needed for the fix geometry (gaps) algorithm. It is a 1-N "
71 "relational table for correspondence between a gap and the unique id of its neighbor features."
72 );
73}
74
75Qgis::ProcessingAlgorithmFlags QgsGeometryCheckGapAlgorithm::flags() const
76{
78}
79
80QgsGeometryCheckGapAlgorithm *QgsGeometryCheckGapAlgorithm::createInstance() const
81{
82 return new QgsGeometryCheckGapAlgorithm();
83}
84
85void QgsGeometryCheckGapAlgorithm::initAlgorithm( const QVariantMap &configuration )
86{
87 Q_UNUSED( configuration )
88
89 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon ) ) );
90 addParameter( new QgsProcessingParameterField( u"UNIQUE_ID"_s, QObject::tr( "Unique feature identifier" ), QString(), u"INPUT"_s ) );
91 addParameter( new QgsProcessingParameterNumber( u"GAP_THRESHOLD"_s, QObject::tr( "Gap threshold" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0.0 ) );
92
93 // Optional allowed gaps layer and buffer value
94 addParameter(
95 new QgsProcessingParameterVectorLayer( u"ALLOWED_GAPS_LAYER"_s, QObject::tr( "Allowed gaps layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon ), QVariant(), true )
96 );
97 addParameter( new QgsProcessingParameterDistance( u"ALLOWED_GAPS_BUFFER"_s, QObject::tr( "Allowed gaps buffer" ), QVariant(), u"ALLOWED_GAPS_LAYER"_s, true, 0.0 ) );
98
99 addParameter( new QgsProcessingParameterFeatureSink( u"NEIGHBORS"_s, QObject::tr( "Neighbors layer" ), Qgis::ProcessingSourceType::Vector ) );
100 addParameter( new QgsProcessingParameterFeatureSink( u"ERRORS"_s, QObject::tr( "Gap errors" ), Qgis::ProcessingSourceType::VectorPoint, QVariant(), true, false ) );
101 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Gap features" ), Qgis::ProcessingSourceType::VectorPolygon ) );
102
103 auto tolerance = std::make_unique<QgsProcessingParameterNumber>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1, 13 );
104 tolerance->setFlags( tolerance->flags() | Qgis::ProcessingParameterFlag::Advanced );
105 tolerance->setHelp(
106 QObject::tr(
107 "The \"Tolerance\" advanced parameter defines the numerical precision of geometric operations, "
108 "given as an integer n, meaning that any difference smaller than 10⁻ⁿ (in map units) is considered zero."
109 )
110 );
111 addParameter( tolerance.release() );
112}
113
114bool QgsGeometryCheckGapAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
115{
116 mTolerance = parameterAsInt( parameters, u"TOLERANCE"_s, context );
117
118 return true;
119}
120
121QgsFields QgsGeometryCheckGapAlgorithm::outputFields()
122{
123 QgsFields fields;
124 fields.append( QgsField( u"gc_layerid"_s, QMetaType::QString ) );
125 fields.append( QgsField( u"gc_layername"_s, QMetaType::QString ) );
126 fields.append( QgsField( u"gc_partidx"_s, QMetaType::Int ) );
127 fields.append( QgsField( u"gc_ringidx"_s, QMetaType::Int ) );
128 fields.append( QgsField( u"gc_vertidx"_s, QMetaType::Int ) );
129 fields.append( QgsField( u"gc_errorx"_s, QMetaType::Double ) );
130 fields.append( QgsField( u"gc_errory"_s, QMetaType::Double ) );
131 fields.append( QgsField( u"gc_error"_s, QMetaType::QString ) );
132 fields.append( QgsField( u"gc_errorid"_s, QMetaType::LongLong ) );
133 return fields;
134}
135
136QVariantMap QgsGeometryCheckGapAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
137{
138 QString dest_output;
139 QString dest_errors;
140 QString dest_neighbors;
141 const std::unique_ptr<QgsProcessingFeatureSource> input( parameterAsSource( parameters, u"INPUT"_s, context ) );
142 if ( !input )
143 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
144
145 QgsVectorLayer *allowedGapsLayer = parameterAsVectorLayer( parameters, u"ALLOWED_GAPS_LAYER"_s, context );
146
147 const double allowedGapsBuffer = parameterAsDouble( parameters, u"ALLOWED_GAPS_BUFFER"_s, context );
148 const double gapThreshold = parameterAsDouble( parameters, u"GAP_THRESHOLD"_s, context );
149
150 const QgsFields fields = outputFields();
151
152 const std::unique_ptr<QgsFeatureSink> sink_output( parameterAsSink( parameters, u"OUTPUT"_s, context, dest_output, fields, input->wkbType(), input->sourceCrs() ) );
153 if ( !sink_output )
154 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
155
156 const std::unique_ptr<QgsFeatureSink> sink_errors( parameterAsSink( parameters, u"ERRORS"_s, context, dest_errors, fields, Qgis::WkbType::Point, input->sourceCrs() ) );
157
158 const QString uniqueIdFieldName( parameterAsString( parameters, u"UNIQUE_ID"_s, context ) );
159 const int uniqueIdFieldIdx = input->fields().indexFromName( uniqueIdFieldName );
160 if ( uniqueIdFieldIdx == -1 )
161 throw QgsProcessingException( QObject::tr( "Missing field %1 in input layer" ).arg( uniqueIdFieldName ) );
162
163 QgsFields neighborsFields = QgsFields();
164 neighborsFields.append( QgsField( "gc_errorid", QMetaType::LongLong ) );
165 neighborsFields.append( input->fields().at( uniqueIdFieldIdx ) );
166 const std::unique_ptr<QgsFeatureSink> sink_neighbors( parameterAsSink( parameters, u"NEIGHBORS"_s, context, dest_neighbors, neighborsFields, Qgis::WkbType::NoGeometry ) );
167 if ( !sink_neighbors )
168 throw QgsProcessingException( invalidSinkError( parameters, u"NEIGHBORS"_s ) );
169
170 QgsProcessingMultiStepFeedback multiStepFeedback( 3, feedback );
171
172 QgsGeometryCheckContext checkContext = QgsGeometryCheckContext( mTolerance, input->sourceCrs(), context.transformContext(), context.project(), uniqueIdFieldIdx );
173
174 // Test detection
175 QList<QgsGeometryCheckError *> checkErrors;
176 QStringList messages;
177
178 QVariantMap configurationCheck;
179 configurationCheck.insert( "gapThreshold", gapThreshold );
180 configurationCheck.insert( "allowedGapsEnabled", allowedGapsLayer != nullptr );
181 if ( allowedGapsLayer )
182 {
183 configurationCheck.insert( "allowedGapsLayer", allowedGapsLayer->id() );
184 configurationCheck.insert( "allowedGapsBuffer", allowedGapsBuffer );
185 }
186 QgsGeometryGapCheck check( &checkContext, configurationCheck );
187 check.prepare( &checkContext, configurationCheck );
188
189 multiStepFeedback.setCurrentStep( 1 );
190 feedback->setProgressText( QObject::tr( "Preparing features…" ) );
191 QMap<QString, QgsFeaturePool *> checkerFeaturePools;
192
193 std::unique_ptr<QgsVectorLayer> inputLayer( input->materialize( QgsFeatureRequest() ) );
195 checkerFeaturePools.insert( inputLayer->id(), &featurePool );
196
197 multiStepFeedback.setCurrentStep( 2 );
198 feedback->setProgressText( QObject::tr( "Collecting errors…" ) );
199 QgsGeometryCheck::Result res = check.collectErrors( checkerFeaturePools, checkErrors, messages, feedback );
201 {
202 feedback->pushInfo( QObject::tr( "Errors collected successfully." ) );
203 }
204 else if ( res == QgsGeometryCheck::Result::Canceled )
205 {
206 throw QgsProcessingException( QObject::tr( "Operation was canceled." ) );
207 }
209 {
210 throw QgsProcessingException( QObject::tr( "Field '%1' contains non-unique values and can not be used as unique ID." ).arg( uniqueIdFieldName ) );
211 }
213 {
214 throw QgsProcessingException( QObject::tr( "Failed to perform geometry overlay operation." ) );
215 }
216
217 multiStepFeedback.setCurrentStep( 3 );
218 feedback->setProgressText( QObject::tr( "Exporting errors…" ) );
219 double step { checkErrors.size() > 0 ? 100.0 / checkErrors.size() : 1 };
220 long long i = 0;
221 feedback->setProgress( 0.0 );
222
223 for ( const QgsGeometryCheckError *error : checkErrors )
224 {
225 if ( feedback->isCanceled() )
226 break;
227
228 const QgsGeometryGapCheckError *gapError = dynamic_cast<const QgsGeometryGapCheckError *>( error );
229 if ( !gapError )
230 break;
231
232 const QgsFeatureIds neighborIds = gapError->neighbors()[inputLayer->id()];
233 for ( QgsFeatureId neighborId : neighborIds )
234 {
235 QgsFeature neighborFeature;
236 neighborFeature.setAttributes( QgsAttributes() << i << inputLayer->getFeature( neighborId ).attribute( uniqueIdFieldIdx ) );
237 if ( !sink_neighbors->addFeature( neighborFeature, QgsFeatureSink::FastInsert ) )
238 throw QgsProcessingException( writeFeatureError( sink_neighbors.get(), parameters, u"NEIGHBORS"_s ) );
239 }
240
241 QgsFeature f;
242 QgsAttributes attrs = f.attributes();
243 attrs << inputLayer->id() << inputLayer->name() << error->vidx().part << error->vidx().ring << error->vidx().vertex << error->location().x() << error->location().y() << error->value().toString() << i;
244 f.setAttributes( attrs );
245
246 f.setGeometry( error->geometry() );
247 if ( !sink_output->addFeature( f, QgsFeatureSink::FastInsert ) )
248 throw QgsProcessingException( writeFeatureError( sink_output.get(), parameters, u"OUTPUT"_s ) );
249
250 f.setGeometry( QgsGeometry::fromPoint( QgsPoint( error->location().x(), error->location().y() ) ) );
251 if ( sink_errors && !sink_errors->addFeature( f, QgsFeatureSink::FastInsert ) )
252 throw QgsProcessingException( writeFeatureError( sink_errors.get(), parameters, u"ERRORS"_s ) );
253
254 i++;
255 feedback->setProgress( 100.0 * step * static_cast<double>( i ) );
256 }
257
258 // Place the point layer above the polygon layer
259 if ( context.willLoadLayerOnCompletion( dest_output ) && context.willLoadLayerOnCompletion( dest_errors ) )
260 {
261 context.layerToLoadOnCompletionDetails( dest_errors ).layerSortKey = 1;
262 context.layerToLoadOnCompletionDetails( dest_output ).layerSortKey = 0;
263 }
264
265 // cleanup memory of the pointed data
266 for ( const QgsGeometryCheckError *error : checkErrors )
267 {
268 delete error;
269 }
270
271 QVariantMap outputs;
272 outputs.insert( u"NEIGHBORS"_s, dest_neighbors );
273 outputs.insert( u"OUTPUT"_s, dest_output );
274 if ( sink_errors )
275 outputs.insert( u"ERRORS"_s, dest_errors );
276
277 return outputs;
278}
279
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3724
@ Point
Point.
Definition qgis.h:296
@ NoGeometry
No geometry.
Definition qgis.h:312
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition qgis.h:3703
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
Definition qgis.h:3711
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Double
Double/float values.
Definition qgis.h:3921
A vector of attributes.
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.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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:75
Base configuration for geometry checks.
This represents an error reported by a geometry check.
Result
Result of the geometry checker operation.
@ Canceled
User canceled calculation.
@ DuplicatedUniqueId
Found duplicated unique ID value.
@ GeometryOverlayError
Error performing geometry overlay operation.
@ Success
Operation completed successfully.
An error produced by a QgsGeometryGapCheck.
const QMap< QString, QgsFeatureIds > & neighbors() const
A map of layers and feature ids of the neighbors of the gap.
Checks for gaps between neighbouring polygons.
static QgsGeometry fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
QString id
Definition qgsmaplayer.h:86
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
int layerSortKey
Optional sorting key for sorting output layers when loading them into a project.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingContext::LayerDetails & layerToLoadOnCompletionDetails(const QString &layer)
Returns a reference to the details for a given layer which is loaded on completion of the algorithm o...
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
bool willLoadLayerOnCompletion(const QString &layer) const
Returns true if the given layer (by ID or datasource) will be loaded into the current project upon co...
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
Processing feedback object for multi-step operations.
A double numeric parameter for distance values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A numeric parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
A feature pool based on a vector data provider.
Represents a vector layer which manages a vector based dataset.
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features