QGIS API Documentation 3.41.0-Master (f75d66fa9f9)
Loading...
Searching...
No Matches
qgsalgorithmcheckgeometrymissingvertex.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcheckgeometrymissingvertex.cpp
3 ---------------------
4 begin : February 2024
5 copyright : (C) 2024 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
22#include "qgspoint.h"
23#include "qgsvectorlayer.h"
25
27
28auto QgsGeometryCheckMissingVertexAlgorithm::name() const -> QString
29{
30 return QStringLiteral( "checkgeometrymissingvertex" );
31}
32
33auto QgsGeometryCheckMissingVertexAlgorithm::displayName() const -> QString
34{
35 return QObject::tr( "Check geometry (Missing Vertex)" );
36}
37
38auto QgsGeometryCheckMissingVertexAlgorithm::tags() const -> QStringList
39{
40 return QObject::tr( "check,geometry,missing,vertex" ).split( ',' );
41}
42
43auto QgsGeometryCheckMissingVertexAlgorithm::group() const -> QString
44{
45 return QObject::tr( "Check geometry" );
46}
47
48auto QgsGeometryCheckMissingVertexAlgorithm::groupId() const -> QString
49{
50 return QStringLiteral( "checkgeometry" );
51}
52
53auto QgsGeometryCheckMissingVertexAlgorithm::shortHelpString() const -> QString
54{
55 return QObject::tr( "This algorithm checks for missing vertices along polygon junctions." );
56}
57
58auto QgsGeometryCheckMissingVertexAlgorithm::flags() const -> Qgis::ProcessingAlgorithmFlags
59{
61}
62
63auto QgsGeometryCheckMissingVertexAlgorithm::createInstance() const -> QgsGeometryCheckMissingVertexAlgorithm *
64{
65 return new QgsGeometryCheckMissingVertexAlgorithm();
66}
67
68void QgsGeometryCheckMissingVertexAlgorithm::initAlgorithm( const QVariantMap &configuration )
69{
70 Q_UNUSED( configuration )
71
72 // inputs
73 addParameter(
75 QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ),
76 QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon )
77 )
78 );
79 addParameter( new QgsProcessingParameterField( QStringLiteral( "UNIQUE_ID" ), QObject::tr( "Unique feature identifier" ), QString(), QStringLiteral( "INPUT" ) ) );
80
81 // outputs
82 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "ERRORS" ), QObject::tr( "Error layer" ), Qgis::ProcessingSourceType::VectorPoint ) );
83 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ), Qgis::ProcessingSourceType::VectorPolygon ) );
84
85 std::unique_ptr<QgsProcessingParameterNumber> tolerance = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "TOLERANCE" ), QObject::tr( "Tolerance" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1, 13 );
86 tolerance->setFlags( tolerance->flags() | Qgis::ProcessingParameterFlag::Advanced );
87 addParameter( tolerance.release() );
88}
89
90auto QgsGeometryCheckMissingVertexAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool
91{
92 mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context );
93
94 return true;
95}
96
97auto QgsGeometryCheckMissingVertexAlgorithm::outputFields() -> QgsFields
98{
99 QgsFields fields;
100 fields.append( QgsField( QStringLiteral( "gc_layerid" ), QMetaType::QString ) );
101 fields.append( QgsField( QStringLiteral( "gc_layername" ), QMetaType::QString ) );
102 fields.append( QgsField( QStringLiteral( "gc_partidx" ), QMetaType::Int ) );
103 fields.append( QgsField( QStringLiteral( "gc_ringidx" ), QMetaType::Int ) );
104 fields.append( QgsField( QStringLiteral( "gc_vertidx" ), QMetaType::Int ) );
105 fields.append( QgsField( QStringLiteral( "gc_errorx" ), QMetaType::Double ) );
106 fields.append( QgsField( QStringLiteral( "gc_errory" ), QMetaType::Double ) );
107 fields.append( QgsField( QStringLiteral( "gc_error" ), QMetaType::QString ) );
108 return fields;
109}
110
111
112auto QgsGeometryCheckMissingVertexAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) -> QVariantMap
113{
114 QString dest_output;
115 QString dest_errors;
116 QgsProcessingFeatureSource *input = parameterAsSource( parameters, QStringLiteral( "INPUT" ), context );
117
118 QString uniqueIdFieldName( parameterAsString( parameters, QStringLiteral( "UNIQUE_ID" ), context ) );
119 int uniqueIdFieldIdx = input->fields().indexFromName( uniqueIdFieldName );
120 if ( uniqueIdFieldIdx == -1 )
121 throw QgsProcessingException( QObject::tr( "Missing field %1 in input layer" ).arg( uniqueIdFieldName ) );
122
123 const QgsField uniqueIdField = input->fields().at( uniqueIdFieldIdx );
124
125 QgsFields fields = outputFields();
126 fields.append( uniqueIdField );
127
128 const std::unique_ptr<QgsFeatureSink> sink_output( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest_output, fields, input->wkbType(), input->sourceCrs() ) );
129 if ( !sink_output )
130 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
131
132 const std::unique_ptr<QgsFeatureSink> sink_errors( parameterAsSink( parameters, QStringLiteral( "ERRORS" ), context, dest_errors, fields, Qgis::WkbType::Point, input->sourceCrs() ) );
133 if ( !sink_errors )
134 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "ERRORS" ) ) );
135
136 QgsProcessingMultiStepFeedback multiStepFeedback( 3, feedback );
137
138 QgsProject *project = QgsProject::instance();
139
140 const std::unique_ptr<QgsGeometryCheckContext> checkContext = std::make_unique<QgsGeometryCheckContext>( mTolerance, input->sourceCrs(), project->transformContext(), project );
141
142 // Test detection
143 QList<QgsGeometryCheckError *> checkErrors;
144 QStringList messages;
145
146 const QgsGeometryMissingVertexCheck check( checkContext.get(), QVariantMap() );
147
148 multiStepFeedback.setCurrentStep( 1 );
149 feedback->setProgressText( QObject::tr( "Preparing features…" ) );
150 QMap<QString, QgsFeaturePool *> featurePools;
151 QgsVectorLayer *inputLayer = input->materialize( QgsFeatureRequest() );
152 featurePools.insert( inputLayer->id(), new QgsVectorDataProviderFeaturePool( inputLayer ) );
153
154 multiStepFeedback.setCurrentStep( 2 );
155 feedback->setProgressText( QObject::tr( "Collecting errors…" ) );
156 check.collectErrors( featurePools, checkErrors, messages, feedback );
157
158 multiStepFeedback.setCurrentStep( 3 );
159 feedback->setProgressText( QObject::tr( "Exporting errors…" ) );
160 const double step { checkErrors.size() > 0 ? 100.0 / checkErrors.size() : 1 };
161 long i = 0;
162 feedback->setProgress( 0.0 );
163
164 for ( QgsGeometryCheckError *error : checkErrors )
165 {
166 if ( feedback->isCanceled() )
167 {
168 break;
169 }
170 QgsFeature f;
171 QgsAttributes attrs = f.attributes();
172
173 attrs << error->layerId()
174 << inputLayer->name()
175 << error->vidx().part
176 << error->vidx().ring
177 << error->vidx().vertex
178 << error->location().x()
179 << error->location().y()
180 << error->value().toString()
181 << inputLayer->getFeature( error->featureId() ).attribute( uniqueIdField.name() );
182 f.setAttributes( attrs );
183
184 f.setGeometry( error->geometry() );
185 if ( !sink_output->addFeature( f, QgsFeatureSink::FastInsert ) )
186 throw QgsProcessingException( writeFeatureError( sink_output.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
187
188 f.setGeometry( QgsGeometry::fromPoint( QgsPoint( error->location().x(), error->location().y() ) ) );
189 if ( !sink_errors->addFeature( f, QgsFeatureSink::FastInsert ) )
190 throw QgsProcessingException( writeFeatureError( sink_errors.get(), parameters, QStringLiteral( "ERRORS" ) ) );
191
192 i++;
193 feedback->setProgress( 100.0 * step * static_cast<double>( i ) );
194 }
195
196 QVariantMap outputs;
197 outputs.insert( QStringLiteral( "OUTPUT" ), dest_output );
198 outputs.insert( QStringLiteral( "ERRORS" ), dest_errors );
199
200 return outputs;
201}
202
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
A vector of attributes.
This class 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...
QgsVectorLayer * materialize(const QgsFeatureRequest &request, QgsFeedback *feedback=nullptr)
Materializes a request (query) made against this feature source, by running it over the source and re...
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.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString name
Definition qgsfield.h:62
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:70
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
This represents an error reported by a geometry check.
A topology check for missing vertices.
static QgsGeometry fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
QString name
Definition qgsmaplayer.h:80
QString id
Definition qgsmaplayer.h:79
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
Qgis::WkbType wkbType() const override
Returns the geometry type for features returned by this source.
QgsFields fields() const override
Returns the fields associated with features in the source.
Base class for providing feedback from a processing algorithm.
Processing feedback object for multi-step operations.
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
Definition qgsproject.h:113
A feature pool based on a vector data provider.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.