QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:189
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:132
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:214
QgsWkbTypes::isMultiType
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:831
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsGeometryCheck::ChangeChanged
@ ChangeChanged
Something has been updated.
Definition: qgsgeometrycheck.h:147
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsAbstractGeometry::partCount
virtual int partCount() const =0
Returns count of parts contained in the geometry.
QgsSingleGeometryCheckError
Definition: qgssinglegeometrycheck.h:39
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:45
QgsGeometryCheck::ChangeRemoved
@ ChangeRemoved
Something has been removed.
Definition: qgsgeometrycheck.h:146
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsGeometryMultipartCheck::RemoveObject
@ RemoveObject
Definition: qgsgeometrymultipartcheck.h:45
qgsgeometrycheckcontext.h
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:137
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:45
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const
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:71
QgsGeometry
Definition: qgsgeometry.h:122
QgsGeometryMultipartCheck::processGeometry
QList< QgsSingleGeometryCheckError * > processGeometry(const QgsGeometry &geometry) const override
Check the geometry for errors.
Definition: qgsgeometrymultipartcheck.cpp:20
QgsFeature
Definition: qgsfeature.h:55
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
QgsGeometryCheckError
Definition: qgsgeometrycheckerror.h:35
QgsFeaturePool
Definition: qgsfeaturepool.h:37
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