26QString QgsFixGeometryGapAlgorithm::name()
const
28 return QStringLiteral(
"fixgeometrygap" );
31QString QgsFixGeometryGapAlgorithm::displayName()
const
33 return QObject::tr(
"Fix geometry (gap)" );
36QStringList QgsFixGeometryGapAlgorithm::tags()
const
38 return QObject::tr(
"fix,gap" ).split(
',' );
41QString QgsFixGeometryGapAlgorithm::group()
const
43 return QObject::tr(
"Fix geometry" );
46QString QgsFixGeometryGapAlgorithm::groupId()
const
48 return QStringLiteral(
"fixgeometry" );
51QString QgsFixGeometryGapAlgorithm::shortHelpString()
const
53 return QObject::tr(
"This algorithm fills the gaps based on a gap and neighbors layer from the check gap algorithm." );
56QgsFixGeometryGapAlgorithm *QgsFixGeometryGapAlgorithm::createInstance()
const
58 return new QgsFixGeometryGapAlgorithm();
61void QgsFixGeometryGapAlgorithm::initAlgorithm(
const QVariantMap &configuration )
63 Q_UNUSED( configuration )
81 checkMethods.cbegin(), checkMethods.cend() - 1, std::inserter( methods, methods.begin() ),
88 QStringLiteral(
"UNIQUE_ID" ), QObject::tr(
"Field of original feature unique identifier" ),
89 QString(), QStringLiteral(
"INPUT" )
92 QStringLiteral(
"ERROR_ID_IDX" ), QObject::tr(
"Field of error id" ),
93 QStringLiteral(
"gc_errorid" ), QStringLiteral(
"GAPS" ),
105 std::unique_ptr<QgsProcessingParameterNumber> tolerance = std::make_unique<QgsProcessingParameterNumber>(
109 addParameter( tolerance.release() );
114 const std::unique_ptr<QgsProcessingFeatureSource> input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
118 const std::unique_ptr<QgsProcessingFeatureSource> neighbors( parameterAsSource( parameters, QStringLiteral(
"NEIGHBORS" ), context ) );
122 const std::unique_ptr<QgsProcessingFeatureSource> gaps( parameterAsSource( parameters, QStringLiteral(
"GAPS" ), context ) );
128 const QString featIdFieldName = parameterAsString( parameters, QStringLiteral(
"UNIQUE_ID" ), context );
129 const QString errorIdFieldName = parameterAsString( parameters, QStringLiteral(
"ERROR_ID_IDX" ), context );
132 int method = parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context );
149 if ( gaps->fields().indexFromName( errorIdFieldName ) == -1 )
150 throw QgsProcessingException( QObject::tr(
"Field \"%1\" does not exist in the gaps layer." ).arg( errorIdFieldName ) );
151 if ( neighbors->fields().indexFromName( featIdFieldName ) == -1 )
152 throw QgsProcessingException( QObject::tr(
"Field \"%1\" does not exist in the neighbors layer." ).arg( featIdFieldName ) );
153 int inputIdFieldIndex = input->fields().indexFromName( featIdFieldName );
154 if ( inputIdFieldIndex == -1 )
155 throw QgsProcessingException( QObject::tr(
"Field \"%1\" does not exist in input layer." ).arg( featIdFieldName ) );
157 const QgsField inputFeatIdField = input->fields().at( inputIdFieldIndex );
158 const QMetaType::Type inputFeatIdFieldType = inputFeatIdField.
type();
159 if ( inputFeatIdFieldType != neighbors->fields().at( neighbors->fields().indexFromName( featIdFieldName ) ).type() )
160 throw QgsProcessingException( QObject::tr(
"Field \"%1\" does not have the same type as in the neighbors layer." ).arg( featIdFieldName ) );
163 const std::unique_ptr<QgsFeatureSink> sink_output( parameterAsSink(
164 parameters, QStringLiteral(
"OUTPUT" ), context, dest_output, input->fields(), input->wkbType(), input->sourceCrs()
171 reportFields.
append(
QgsField( QStringLiteral(
"report" ), QMetaType::QString ) );
172 reportFields.
append(
QgsField( QStringLiteral(
"error_fixed" ), QMetaType::Bool ) );
173 const std::unique_ptr<QgsFeatureSink> sink_report( parameterAsSink(
174 parameters, QStringLiteral(
"REPORT" ), context, dest_report, reportFields,
Qgis::WkbType::Point, gaps->sourceCrs()
183 QStringList messages;
187 multiStepFeedback.setCurrentStep( 1 );
188 multiStepFeedback.setProgressText( QObject::tr(
"Preparing features..." ) );
189 std::unique_ptr<QgsVectorLayer> fixedLayer( input->materialize(
QgsFeatureRequest() ) );
191 QMap<QString, QgsFeaturePool *> featurePools;
192 featurePools.insert( fixedLayer->id(), &featurePool );
197 project->
addMapLayer( fixedLayer.get(),
false,
false );
198 fixedLayer->startEditing();
205 long long progression = 0;
206 long long totalProgression = gaps->featureCount();
207 multiStepFeedback.setCurrentStep( 2 );
208 multiStepFeedback.setProgressText( QObject::tr(
"Fixing errors..." ) );
212 multiStepFeedback.setProgress(
static_cast<double>(
static_cast<long double>( progression ) / totalProgression ) * 100 );
215 QVariant gapId = gapFeature.
attribute( errorIdFieldName );
216 if ( !gapId.isValid() || gapId.isNull() )
217 throw QgsProcessingException( QObject::tr(
"NULL or invalid value found in field \"%1\"" ).arg( errorIdFieldName ) );
222 const QString errorIdValue = gapFeature.
attribute( errorIdFieldName ).toString();
226 QString neighborIdValue = f.
attribute( featIdFieldName ).toString();
227 if ( inputFeatIdFieldType == QMetaType::QString )
228 neighborIdValue =
"'" + neighborIdValue +
"'";
230 if ( fixedLayer->getFeatures(
QgsFeatureRequest().setFilterExpression(
"\"" + featIdFieldName +
"\" = " + neighborIdValue ) ).nextFeature( neighborFeature ) )
231 neighborIds << neighborFeature.
id();
234 QMap<QString, QgsFeatureIds> neighborsMap;
235 neighborsMap.insert( fixedLayer->id(), neighborIds );
247 check.fixError( featurePools, &gapError, method, QMap<QString, int>(), changes );
251 resolutionMessage = QObject::tr(
"Error is obsolete" );
256 throw QgsProcessingException( writeFeatureError( sink_report.get(), parameters, QStringLiteral(
"REPORT" ) ) );
258 multiStepFeedback.setProgress( 100 );
262 if ( !fixedLayer->commitChanges() )
268 totalProgression = fixedLayer->featureCount();
269 multiStepFeedback.setCurrentStep( 2 );
270 multiStepFeedback.setProgressText( QObject::tr(
"Exporting fixed layer..." ) );
273 while ( fixedFeaturesIt.
nextFeature( fixedFeature ) )
276 multiStepFeedback.setProgress(
static_cast<double>(
static_cast<long double>( progression ) / totalProgression ) * 100 );
278 throw QgsProcessingException( writeFeatureError( sink_output.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
280 multiStepFeedback.setProgress( 100 );
283 outputs.insert( QStringLiteral(
"OUTPUT" ), dest_output );
284 outputs.insert( QStringLiteral(
"REPORT" ), dest_report );
291 mTolerance = parameterAsInt( parameters, QStringLiteral(
"TOLERANCE" ), context );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ Numeric
Accepts numeric fields.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ 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.
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 setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
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.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Base configuration for geometry checks.
@ StatusFixed
The error is fixed.
@ StatusObsolete
The error is obsolete because of other modifications.
Status status() const
The status of the error.
QString resolutionMessage() const
A message with details, how the error has been resolved.
Implements a resolution for problems detected in geometry checks.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
An error produced by a QgsGeometryGapCheck.
Checks for gaps between neighbouring polygons.
QList< QgsGeometryCheckResolutionMethod > availableResolutionMethods() const override
Returns a list of available resolution methods.
@ CreateNewFeature
Create a new feature with the gap geometry.
@ MergeLongestEdge
Merge the gap with the polygon with the longest shared edge.
@ MergeLargestArea
Merge with neighbouring polygon with largest area.
QgsGeometry centroid() const
Returns the center of mass of a geometry.
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.
Base class for providing feedback from a processing algorithm.
Processing feedback object for multi-step operations.
An enum based parameter for processing algorithms, allowing for selection from predefined 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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
void removeMapLayer(const QString &layerId)
Remove a layer from the registry by layer ID.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
QgsMapLayer * addMapLayer(QgsMapLayer *mapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
A rectangle specified with double values.
A feature pool based on a vector data provider.
QSet< QgsFeatureId > QgsFeatureIds