QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
20 QList<QgsSingleGeometryCheckError *> QgsGeometryMultipartCheck::processGeometry( const QgsGeometry &geometry ) const
21 {
22  QList<QgsSingleGeometryCheckError *> errors;
23 
24  const QgsAbstractGeometry *geom = geometry.constGet();
25  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 
33 void 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  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 QStringList methods = QStringList()
79  << tr( "Convert to single part feature" )
80  << tr( "Delete feature" )
81  << tr( "No action" );
82  return methods;
83 }
QgsGeometryCheckError::setFixFailed
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
Definition: qgsgeometrycheckerror.cpp:109
QgsGeometryCheck::Change
Descripts a change to fix a geometry.
Definition: qgsgeometrycheck.h:177
QgsFeature::id
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
QgsGeometryCheckerUtils::getGeomPart
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
Definition: qgsgeometrycheckerutils.cpp:268
QgsGeometryCheck::ChangeFeature
@ ChangeFeature
This change happens on feature level.
Definition: qgsgeometrycheck.h:131
qgsgeometrymultipartcheck.h
QgsGeometryCheckError::setObsolete
void setObsolete()
Set the error status to obsolete.
Definition: qgsgeometrycheckerror.h:166
QgsGeometryCheckError::featureId
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
Definition: qgsgeometrycheckerror.h:90
QgsGeometryCheck::Changes
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
Definition: qgsgeometrycheck.h:213
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:67
QgsGeometryCheck::ChangeChanged
@ ChangeChanged
Something has been updated.
Definition: qgsgeometrycheck.h:146
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsAbstractGeometry::partCount
virtual int partCount() const =0
Returns count of parts contained in the geometry.
QgsSingleGeometryCheckError
An error from a QgsSingleGeometryCheck.
Definition: qgssinglegeometrycheck.h:40
QgsFeaturePool::getFeature
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
Definition: qgsfeaturepool.cpp:39
qgsfeaturepool.h
QgsGeometryMultipartCheck::NoChange
@ NoChange
Definition: qgsgeometrymultipartcheck.h:46
QgsGeometryCheck::ChangeRemoved
@ ChangeRemoved
Something has been removed.
Definition: qgsgeometrycheck.h:145
QgsGeometryMultipartCheck::RemoveObject
@ RemoveObject
Definition: qgsgeometrymultipartcheck.h:46
qgsgeometrycheckcontext.h
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:193
QgsFeaturePool::deleteFeature
virtual void deleteFeature(QgsFeatureId fid)=0
Removes a feature from this pool.
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:139
QgsFeaturePool::updateFeature
virtual void updateFeature(QgsFeature &feature)=0
Updates a feature in this pool.
QgsGeometryCheckError::setFixed
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
Definition: qgsgeometrycheckerror.cpp:98
QgsGeometryMultipartCheck::ConvertToSingle
@ ConvertToSingle
Definition: qgsgeometrymultipartcheck.h:46
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:74
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsGeometryMultipartCheck::processGeometry
QList< QgsSingleGeometryCheckError * > processGeometry(const QgsGeometry &geometry) const override
Check the geometry for errors.
Definition: qgsgeometrymultipartcheck.cpp:20
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsGeometryCheckError::layerId
const QString & layerId() const
The id of the layer on which this error has been detected.
Definition: qgsgeometrycheckerror.h:85
QgsGeometryMultipartCheck::resolutionMethods
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
Definition: qgsgeometrymultipartcheck.cpp:76
QgsWkbTypes::isMultiType
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:832
QgsGeometryCheckError
This represents an error reported by a geometry check.
Definition: qgsgeometrycheckerror.h:36
QgsFeaturePool
A feature pool is based on a vector layer and caches features.
Definition: qgsfeaturepool.h:38
QgsGeometryMultipartCheck::fixError
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.
Definition: qgsgeometrymultipartcheck.cpp:33