QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsgeometrymultipartcheck.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometrymultipartcheck.cpp
3 ---------------------
4 begin : September 2015
5 copyright : (C) 2014 by Sandro Mani / Sourcepole AG
6 email : smani at sourcepole dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
18#include "qgsfeaturepool.h"
19
20QList<QgsSingleGeometryCheckError *> QgsGeometryMultipartCheck::processGeometry( const QgsGeometry &geometry ) const
21{
22 QList<QgsSingleGeometryCheckError *> errors;
23
24 const QgsAbstractGeometry *geom = geometry.constGet();
25 const QgsWkbTypes::Type type = geom->wkbType();
26 if ( geom->partCount() == 1 && QgsWkbTypes::isMultiType( type ) )
27 {
28 errors.append( new QgsSingleGeometryCheckError( this, geometry, geometry ) );
29 }
30 return errors;
31}
32
33void QgsGeometryMultipartCheck::fixError( const QMap<QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap<QString, int> & /*mergeAttributeIndices*/, Changes &changes ) const
34{
35 QgsFeaturePool *featurePool = featurePools[ error->layerId() ];
36 QgsFeature feature;
37 if ( !featurePool->getFeature( error->featureId(), feature ) )
38 {
39 error->setObsolete();
40 return;
41 }
42 const QgsGeometry featureGeom = feature.geometry();
43 const QgsAbstractGeometry *geom = featureGeom.constGet();
44
45 // Check if error still applies
46 if ( geom->partCount() > 1 || !QgsWkbTypes::isMultiType( geom->wkbType() ) )
47 {
48 error->setObsolete();
49 return;
50 }
51
52 // Fix error
53 if ( method == NoChange )
54 {
55 error->setFixed( method );
56 }
57 else if ( method == ConvertToSingle )
58 {
59 feature.setGeometry( QgsGeometry( QgsGeometryCheckerUtils::getGeomPart( geom, 0 )->clone() ) );
60 featurePool->updateFeature( feature );
61 error->setFixed( method );
62 changes[error->layerId()][feature.id()].append( Change( ChangeFeature, ChangeChanged ) );
63 }
64 else if ( method == RemoveObject )
65 {
66 featurePool->deleteFeature( feature.id() );
67 error->setFixed( method );
68 changes[error->layerId()][feature.id()].append( Change( ChangeFeature, ChangeRemoved ) );
69 }
70 else
71 {
72 error->setFixFailed( tr( "Unknown method" ) );
73 }
74}
75
77{
78 static const QStringList methods = QStringList()
79 << tr( "Convert to single part feature" )
80 << tr( "Delete feature" )
81 << tr( "No action" );
82 return methods;
83}
Abstract base class for all geometries.
virtual int partCount() const =0
Returns count of parts contained in the geometry.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
A feature pool is based on a vector layer and caches features.
virtual void updateFeature(QgsFeature &feature)=0
Updates a feature in this pool.
virtual void deleteFeature(QgsFeatureId fid)=0
Removes a feature from this pool.
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
This represents an error reported by a geometry check.
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
void setObsolete()
Set the error status to obsolete.
const QString & layerId() const
The id of the layer on which this error has been detected.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
@ ChangeFeature
This change happens on feature level.
@ ChangeChanged
Something has been updated.
@ ChangeRemoved
Something has been removed.
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
void fixError(const QMap< QString, QgsFeaturePool * > &featurePools, QgsGeometryCheckError *error, int method, const QMap< QString, int > &mergeAttributeIndices, Changes &changes) const override
Fixes the error error with the specified method.
QList< QgsSingleGeometryCheckError * > processGeometry(const QgsGeometry &geometry) const override
Check the geometry for errors.
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
An error from a QgsSingleGeometryCheck.
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:862
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
Descripts a change to fix a geometry.