QGIS API Documentation  3.6.0-Noosa (5873452)
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 }
QgsFeatureId id
Definition: qgsfeature.h:64
void fixError(const QMap< QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap< QString, int > &mergeAttributeIndices, Changes &changes) const override
Fix the error error with the specified method.
bool getFeature(QgsFeatureId id, QgsFeature &feature, QgsFeedback *feedback=nullptr)
Retrieves the feature with the specified id into feature.
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:559
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
void setObsolete()
Set the error status to obsolete.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
virtual void updateFeature(QgsFeature &feature)=0
Updates a feature in this pool.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
Something has been removed.
Abstract base class for all geometries.
const QString & layerId() const
The id of the layer on which this error has been detected.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
An error from a QgsSingleGeometryCheck.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QList< QgsSingleGeometryCheckError * > processGeometry(const QgsGeometry &geometry) const override
Check the geometry for errors.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
A feature pool is based on a vector layer and caches features.
QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
Descripts a change to fix a geometry.
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
virtual void deleteFeature(QgsFeatureId fid)=0
Removes a feature from this pool.
QgsFeatureId featureId() const
The id of the feature on which this error has been detected.
QgsGeometry geometry
Definition: qgsfeature.h:67
This represents an error reported by a geometry check.
Something has been updated.
This change happens on feature level.
virtual int partCount() const =0
Returns count of parts contained in the geometry.