QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsgeometrytypecheck.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometrytypecheck.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 "qgsmulticurve.h"
19#include "qgsmultilinestring.h"
20#include "qgsmultipoint.h"
21#include "qgsmultipolygon.h"
22#include "qgsfeaturepool.h"
23
24
25QList<QgsSingleGeometryCheckError *> QgsGeometryTypeCheck::processGeometry( const QgsGeometry &geometry ) const
26{
27 QList<QgsSingleGeometryCheckError *> errors;
28 const QgsAbstractGeometry *geom = geometry.constGet();
29 const Qgis::WkbType type = QgsWkbTypes::flatType( geom->wkbType() );
30 if ( ( mAllowedTypes & ( 1 << static_cast< quint32>( type ) ) ) == 0 )
31 {
32 errors.append( new QgsGeometryTypeCheckError( this, geometry, geometry, type ) );
33 }
34 return errors;
35}
36
37void QgsGeometryTypeCheck::fixError( const QMap<QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap<QString, int> & /*mergeAttributeIndices*/, Changes &changes ) const
38{
39 QgsFeaturePool *featurePool = featurePools[ error->layerId() ];
40 QgsFeature feature;
41 if ( !featurePool->getFeature( error->featureId(), feature ) )
42 {
43 error->setObsolete();
44 return;
45 }
46 const QgsGeometry featureGeom = feature.geometry();
47 const QgsAbstractGeometry *geom = featureGeom.constGet();
48
49 // Check if error still applies
50 const Qgis::WkbType type = QgsWkbTypes::flatType( geom->wkbType() );
51 if ( ( mAllowedTypes & ( 1 << static_cast< quint32>( type ) ) ) != 0 )
52 {
53 error->setObsolete();
54 return;
55 }
56
57 // Fix with selected method
58 if ( method == NoChange )
59 {
60 error->setFixed( method );
61 }
62 else if ( method == Convert )
63 {
64 // Check if corresponding single type is allowed
65 if ( QgsWkbTypes::isMultiType( type ) && ( ( 1 << static_cast< quint32>( QgsWkbTypes::singleType( type ) ) ) & mAllowedTypes ) != 0 )
66 {
67 // Explode multi-type feature into single-type features
68 for ( int iPart = 1, nParts = geom->partCount(); iPart < nParts; ++iPart )
69 {
70 QgsFeature newFeature;
71 newFeature.setAttributes( feature.attributes() );
72 newFeature.setGeometry( QgsGeometry( QgsGeometryCheckerUtils::getGeomPart( geom, iPart )->clone() ) );
73 featurePool->addFeature( newFeature );
74 changes[error->layerId()][newFeature.id()].append( Change( ChangeFeature, ChangeAdded ) );
75 }
76 // Recycle feature for part 0
77 feature.setGeometry( QgsGeometry( QgsGeometryCheckerUtils::getGeomPart( geom, 0 )->clone() ) );
78 featurePool->updateFeature( feature );
79 changes[error->layerId()][feature.id()].append( Change( ChangeFeature, ChangeChanged ) );
80 }
81 // Check if corresponding multi type is allowed
82 else if ( QgsWkbTypes::isSingleType( type ) && ( ( 1 << static_cast< quint32>( QgsWkbTypes::multiType( type ) ) ) & mAllowedTypes ) != 0 )
83 {
84 QgsGeometryCollection *geomCollection = nullptr;
85 switch ( QgsWkbTypes::multiType( type ) )
86 {
88 {
89 geomCollection = new QgsMultiPoint();
90 break;
91 }
93 {
94 geomCollection = new QgsMultiLineString();
95 break;
96 }
98 {
99 geomCollection = new QgsMultiPolygon();
100 break;
101 }
103 {
104 geomCollection = new QgsMultiCurve();
105 break;
106 }
108 {
109 geomCollection = new QgsMultiSurface();
110 break;
111 }
112 default:
113 break;
114 }
115 if ( !geomCollection )
116 {
117 error->setFixFailed( tr( "Unknown geometry type" ) );
118 }
119 else
120 {
121 geomCollection->addGeometry( geom->clone() );
122
123 feature.setGeometry( QgsGeometry( geomCollection ) );
124 featurePool->updateFeature( feature );
125 changes[error->layerId()][feature.id()].append( Change( ChangeFeature, ChangeChanged ) );
126 }
127 }
128 // Delete feature
129 else
130 {
131 featurePool->deleteFeature( feature.id() );
132 changes[error->layerId()][error->featureId()].append( Change( ChangeFeature, ChangeRemoved ) );
133 }
134 error->setFixed( method );
135 }
136 else if ( method == Delete )
137 {
138 featurePool->deleteFeature( feature.id() );
139 error->setFixed( method );
140 changes[error->layerId()][error->featureId()].append( Change( ChangeFeature, ChangeRemoved ) );
141 }
142 else
143 {
144 error->setFixFailed( tr( "Unknown method" ) );
145 }
146}
147
149{
150 static const QStringList methods = QStringList()
151 << tr( "Convert to corresponding multi or single type if possible, otherwise delete feature" )
152 << tr( "Delete feature" )
153 << tr( "No action" );
154 return methods;
155}
156
158{
159 return tr( "Geometry type" );
160}
161
163{
164 return factoryDescription();
165}
166
168{
169 return QStringLiteral( "QgsGeometryTypeCheck" );
170}
171
173{
175}
176
178{
179 return factoryId();
180}
181
183{
184 return factoryCheckType();
185}
186
188{
189 return QgsSingleGeometryCheckError::isEqual( other ) &&
190 mFlatType == static_cast<const QgsGeometryTypeCheckError *>( other )->mFlatType;
191}
192
194{
195 return QStringLiteral( "%1 (%2)" ).arg( mCheck->description(), QgsWkbTypes::displayString( mFlatType ) );
196}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:182
@ MultiPoint
MultiPoint.
@ MultiPolygon
MultiPolygon.
@ MultiLineString
MultiLineString.
@ MultiCurve
MultiCurve.
@ MultiSurface
MultiSurface.
Abstract base class for all geometries.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
virtual int partCount() const =0
Returns count of parts contained in the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
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.
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsGeometry geometry
Definition: qgsfeature.h:67
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:167
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.
CheckType
The type of a check.
@ FeatureCheck
The check controls geometries as a whole.
@ ChangeChanged
Something has been updated.
@ ChangeAdded
Something has been added.
@ ChangeRemoved
Something has been removed.
virtual QString description() const =0
Returns a human readable description for this check.
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
Geometry collection.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
A geometry type check error.
bool isEqual(const QgsSingleGeometryCheckError *other) const override
Check if this error is equal to other.
QString description() const override
A human readable description of this error.
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
QgsGeometryCheck::CheckType checkType() const override
Returns the check type.
QString id() const override
Returns an id for this check.
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.
static QgsGeometryCheck::CheckType factoryCheckType()
QString description() const override
Returns a human readable description for this check.
static QString factoryDescription()
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
Multi line string geometry collection.
Multi point geometry collection.
Definition: qgsmultipoint.h:29
Multi polygon geometry collection.
Multi surface geometry collection.
An error from a QgsSingleGeometryCheck.
const QgsSingleGeometryCheck * mCheck
virtual bool isEqual(const QgsSingleGeometryCheckError *other) const
Check if this error is equal to other.
static bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:758
static QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:53
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:200
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:628
static bool isSingleType(Qgis::WkbType type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:748
Descripts a change to fix a geometry.