QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsgeometrycheckerror.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometrycheckerror.cpp
3  --------
4  begin : September 2018
5  copyright : (C) 2018 by Denis Rouzaud
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsgeometrycheckerror.h"
19 #include "qgsapplication.h"
20 
22  const QString &layerId,
23  QgsFeatureId featureId,
24  const QgsGeometry &geometry,
25  const QgsPointXY &errorLocation,
26  QgsVertexId vidx,
27  const QVariant &value, ValueType valueType )
28  : mCheck( check )
29  , mLayerId( layerId )
30  , mFeatureId( featureId )
31  , mGeometry( geometry )
32  , mErrorLocation( errorLocation )
33  , mVidx( vidx )
34  , mValue( value )
35  , mValueType( valueType )
36  , mStatus( StatusPending )
37 {
38 }
39 
41  const QgsGeometryCheckerUtils::LayerFeature &layerFeature,
42  const QgsPointXY &errorLocation,
43  QgsVertexId vidx,
44  const QVariant &value,
45  ValueType valueType )
46  : mCheck( check )
47  , mLayerId( layerFeature.layerId() )
48  , mFeatureId( layerFeature.feature().id() )
49  , mErrorLocation( errorLocation )
50  , mVidx( vidx )
51  , mValue( value )
52  , mValueType( valueType )
53  , mStatus( StatusPending )
54 {
55  if ( vidx.part != -1 )
56  {
57  const QgsGeometry geom = layerFeature.geometry();
59  }
60  else
61  {
62  mGeometry = layerFeature.geometry();
63  }
64  if ( !layerFeature.useMapCrs() )
65  {
66  QgsVectorLayer *vl = layerFeature.layer().data();
67  if ( vl )
68  {
70  try
71  {
72  mGeometry.transform( ct );
74  }
75  catch ( const QgsCsException & )
76  {
77  QgsDebugMsg( QStringLiteral( "Can not show error in current map coordinate reference system" ) );
78  }
79  }
80  }
81 }
82 
84 {
85  return mGeometry;
86 }
87 
89 {
90  return QgsRectangle();
91 }
92 
94 {
95  return mGeometry.boundingBox();
96 }
97 
99 {
101  const QList<QgsGeometryCheckResolutionMethod> methods = mCheck->availableResolutionMethods();
102  for ( const QgsGeometryCheckResolutionMethod &fix : methods )
103  {
104  if ( fix.id() == method )
105  mResolutionMessage = fix.name();
106  }
107 }
108 
109 void QgsGeometryCheckError::setFixFailed( const QString &reason )
110 {
112  mResolutionMessage = reason;
113 }
114 
116 {
117  return other->check() == check() &&
118  other->layerId() == layerId() &&
119  other->featureId() == featureId() &&
120  other->vidx() == vidx();
121 }
122 
124 {
125  return false;
126 }
127 
129 {
130  if ( status() == StatusObsolete )
131  {
132  return false;
133  }
134 
135  for ( const QgsGeometryCheck::Change &change : changes.value( layerId() ).value( featureId() ) )
136  {
137  if ( change.what == QgsGeometryCheck::ChangeFeature )
138  {
139  if ( change.type == QgsGeometryCheck::ChangeRemoved )
140  {
141  return false;
142  }
143  else if ( change.type == QgsGeometryCheck::ChangeChanged )
144  {
145  // If the check is checking the feature at geometry nodes level, the
146  // error almost certainly invalid after a geometry change. In the other
147  // cases, it might likely still be valid.
149  }
150  }
151  else if ( change.what == QgsGeometryCheck::ChangePart )
152  {
153  if ( mVidx.part == change.vidx.part )
154  {
155  return false;
156  }
157  else if ( mVidx.part > change.vidx.part )
158  {
159  mVidx.part += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1;
160  }
161  }
162  else if ( change.what == QgsGeometryCheck::ChangeRing )
163  {
164  if ( mVidx.partEqual( change.vidx ) )
165  {
166  if ( mVidx.ring == change.vidx.ring )
167  {
168  return false;
169  }
170  else if ( mVidx.ring > change.vidx.ring )
171  {
172  mVidx.ring += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1;
173  }
174  }
175  }
176  else if ( change.what == QgsGeometryCheck::ChangeNode )
177  {
178  if ( mVidx.ringEqual( change.vidx ) )
179  {
180  if ( mVidx.vertex == change.vidx.vertex )
181  {
182  return false;
183  }
184  else if ( mVidx.vertex > change.vidx.vertex )
185  {
186  mVidx.vertex += change.type == QgsGeometryCheck::ChangeAdded ? 1 : -1;
187  }
188  }
189  }
190  }
191  return true;
192 }
193 
194 QMap<QString, QgsFeatureIds> QgsGeometryCheckError::involvedFeatures() const
195 {
196  return QMap<QString, QSet<QgsFeatureId> >();
197 }
198 
200 {
202  return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmCheckGeometry.svg" ) );
203  else
204  return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmLineIntersections.svg" ) );
205 }
206 
208 {
209  Q_ASSERT( mCheck == other->mCheck );
210  Q_ASSERT( mLayerId == other->mLayerId );
211  Q_ASSERT( mFeatureId == other->mFeatureId );
213  mVidx = other->mVidx;
214  mValue = other->mValue;
215  mGeometry = other->mGeometry;
216 }
217 
218 QgsGeometryCheck::LayerFeatureIds::LayerFeatureIds( const QMap<QString, QgsFeatureIds> &idsIn )
219  : ids( idsIn )
220 {
221 }
QgsVertexId::part
int part
Part number.
Definition: qgsabstractgeometry.h:1131
QgsGeometryCheckError::value
QVariant value() const
An additional value for the error.
Definition: qgsgeometrycheckerror.h:128
QgsGeometryCheckError::setFixFailed
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
Definition: qgsgeometrycheckerror.cpp:109
QgsGeometryCheckError::contextBoundingBox
virtual QgsRectangle contextBoundingBox() const
The context of the error.
Definition: qgsgeometrycheckerror.cpp:88
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:89
QgsGeometryCheck::Change
Descripts a change to fix a geometry.
Definition: qgsgeometrycheck.h:177
QgsVertexId::vertex
int vertex
Vertex number.
Definition: qgsabstractgeometry.h:1137
QgsGeometry::transform
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
Definition: qgsgeometry.cpp:2813
QgsGeometryCheckError::mVidx
QgsVertexId mVidx
Definition: qgsgeometrycheckerror.h:231
QgsGeometryCheckerUtils::getGeomPart
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
Definition: qgsgeometrycheckerutils.cpp:268
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsGeometryCheck::ChangeAdded
@ ChangeAdded
Something has been added.
Definition: qgsgeometrycheck.h:144
QgsGeometryCheck::ChangeFeature
@ ChangeFeature
This change happens on feature level.
Definition: qgsgeometrycheck.h:131
QgsGeometryCheckError::closeMatch
virtual bool closeMatch(QgsGeometryCheckError *) const
Check if this error is almost equal to other.
Definition: qgsgeometrycheckerror.cpp:123
QgsGeometryCheckError::check
const QgsGeometryCheck * check() const
The geometry check that created this error.
Definition: qgsgeometrycheckerror.h:80
QgsGeometryCheckError::StatusFixed
@ StatusFixed
The error is fixed.
Definition: qgsgeometrycheckerror.h:46
QgsGeometryCheckError::ValueType
ValueType
Describes the type of an error value.
Definition: qgsgeometrycheckerror.h:54
QgsGeometryCheckerUtils::LayerFeature::useMapCrs
bool useMapCrs() const
Returns if the geometry is reprojected to the map CRS or not.
Definition: qgsgeometrycheckerutils.cpp:108
QgsGeometryCheckError::mStatus
Status mStatus
Definition: qgsgeometrycheckerror.h:234
QgsGeometryCheckError::mFeatureId
QgsFeatureId mFeatureId
Definition: qgsgeometrycheckerror.h:228
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
QgsGeometryCheckerUtils::LayerFeature::geometry
QgsGeometry geometry() const
Returns the geometry of this feature.
Definition: qgsgeometrycheckerutils.cpp:70
QgsGeometryCheckError::mErrorLocation
QgsPointXY mErrorLocation
Definition: qgsgeometrycheckerror.h:230
QgsGeometryCheck::FeatureNodeCheck
@ FeatureNodeCheck
The check controls individual nodes.
Definition: qgsgeometrycheck.h:156
QgsGeometryCheck::context
const QgsGeometryCheckContext * context() const
Returns the context.
Definition: qgsgeometrycheck.h:322
QgsGeometryCheck::ChangeChanged
@ ChangeChanged
Something has been updated.
Definition: qgsgeometrycheck.h:146
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
qgsgeometrycheckerror.h
QgsCoordinateTransform::transform
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
Definition: qgscoordinatetransform.cpp:239
QgsVertexId::partEqual
bool partEqual(QgsVertexId o) const SIP_HOLDGIL
Returns true if this vertex ID belongs to the same part as another vertex ID.
Definition: qgsabstractgeometry.h:1097
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
qgsapplication.h
QgsGeometryCheck::ChangeRemoved
@ ChangeRemoved
Something has been removed.
Definition: qgsgeometrycheck.h:145
QgsGeometryCheck
This class implements a geometry check.
Definition: qgsgeometrycheck.h:92
QgsGeometryCheckerUtils::LayerFeature::layer
QPointer< QgsVectorLayer > layer() const
The layer.
Definition: qgsgeometrycheckerutils.cpp:60
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
QgsGeometryCheck::checkType
virtual CheckType checkType() const =0
Returns the check type.
QgsGeometryCheckError::affectedAreaBBox
virtual QgsRectangle affectedAreaBBox() const
The bounding box of the affected area of the error.
Definition: qgsgeometrycheckerror.cpp:93
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
QgsGeometryCheckError::handleChanges
virtual bool handleChanges(const QgsGeometryCheck::Changes &changes)
Apply a list of changes.
Definition: qgsgeometrycheckerror.cpp:128
QgsGeometryCheck::ChangeNode
@ ChangeNode
This change happens on node level.
Definition: qgsgeometrycheck.h:134
QgsGeometryCheck::availableResolutionMethods
virtual QList< QgsGeometryCheckResolutionMethod > availableResolutionMethods() const
Returns a list of available resolution methods.
Definition: qgsgeometrycheck.cpp:59
QgsGeometryCheckResolutionMethod
This class implements a resolution for problems detected in geometry checks.
Definition: qgsgeometrycheckresolutionmethod.h:29
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsGeometryCheckError::mValue
QVariant mValue
Definition: qgsgeometrycheckerror.h:232
QgsGeometryCheckContext::mapCrs
const QgsCoordinateReferenceSystem mapCrs
The coordinate system in which calculations should be done.
Definition: qgsgeometrycheckcontext.h:61
QgsGeometryCheckerUtils::LayerFeature
A layer feature combination to uniquely identify and access a feature in a set of layers.
Definition: qgsgeometrycheckerutils.h:52
QgsGeometryCheckError::status
Status status() const
The status of the error.
Definition: qgsgeometrycheckerror.h:145
QgsGeometryCheckError::StatusFixFailed
@ StatusFixFailed
A fix has been tried on the error but failed.
Definition: qgsgeometrycheckerror.h:45
QgsGeometryCheckError::vidx
const QgsVertexId & vidx() const
The id of the affected vertex.
Definition: qgsgeometrycheckerror.h:140
QgsGeometryCheckError::QgsGeometryCheckError
QgsGeometryCheckError(const QgsGeometryCheck *check, const QgsGeometryCheckerUtils::LayerFeature &layerFeature, const QgsPointXY &errorLocation, QgsVertexId vidx=QgsVertexId(), const QVariant &value=QVariant(), ValueType valueType=ValueOther)
Create a new geometry check error with the parent check and for the layerFeature pair at the errorLoc...
Definition: qgsgeometrycheckerror.cpp:40
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsGeometryCheckError::isEqual
virtual bool isEqual(QgsGeometryCheckError *other) const
Check if this error is equal to other.
Definition: qgsgeometrycheckerror.cpp:115
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsGeometryCheckError::mGeometry
QgsGeometry mGeometry
Definition: qgsgeometrycheckerror.h:229
QgsGeometryCheck::ChangePart
@ ChangePart
This change happens on part level.
Definition: qgsgeometrycheck.h:132
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsGeometryCheck::ChangeRing
@ ChangeRing
This change happens on ring level.
Definition: qgsgeometrycheck.h:133
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1059
QgsGeometryCheckError::update
virtual void update(const QgsGeometryCheckError *other)
Update this error with the information from other.
Definition: qgsgeometrycheckerror.cpp:207
QgsVertexId::ringEqual
bool ringEqual(QgsVertexId o) const SIP_HOLDGIL
Returns true if this vertex ID belongs to the same ring as another vertex ID (i.e.
Definition: qgsabstractgeometry.h:1106
QgsVertexId::ring
int ring
Ring number.
Definition: qgsabstractgeometry.h:1134
QgsGeometryCheckError::mCheck
const QgsGeometryCheck * mCheck
Definition: qgsgeometrycheckerror.h:226
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:996
QgsGeometryCheckError::mLayerId
QString mLayerId
Definition: qgsgeometrycheckerror.h:227
QgsGeometryCheckError::StatusObsolete
@ StatusObsolete
The error is obsolete because of other modifications.
Definition: qgsgeometrycheckerror.h:47
QgsGeometryCheckError::layerId
const QString & layerId() const
The id of the layer on which this error has been detected.
Definition: qgsgeometrycheckerror.h:85
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
QgsGeometryCheckError
This represents an error reported by a geometry check.
Definition: qgsgeometrycheckerror.h:36
QgsGeometryCheckError::icon
virtual QIcon icon() const
Returns an icon that should be shown for this kind of error.
Definition: qgsgeometrycheckerror.cpp:199
QgsGeometryCheckContext::transformContext
const QgsCoordinateTransformContext transformContext
The coordinate transform context with which transformations will be done.
Definition: qgsgeometrycheckcontext.h:66
QgsGeometryCheckError::geometry
QgsGeometry geometry() const
The geometry of the error in map units.
Definition: qgsgeometrycheckerror.cpp:83
QgsGeometryCheck::LayerFeatureIds::LayerFeatureIds
LayerFeatureIds()=default
QgsGeometryCheckError::mResolutionMessage
QString mResolutionMessage
Definition: qgsgeometrycheckerror.h:235
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsGeometryCheckError::involvedFeatures
virtual QMap< QString, QgsFeatureIds > involvedFeatures() const
Returns a list of involved features.
Definition: qgsgeometrycheckerror.cpp:194