QgsGeometryCheck class
This class implements a geometry check.
Contents
Geometry checks run over a set of features and can detect errors like topological or other issues which are reported in the geometry validation panel in QGIS and help a user to create valid geometries.
Implementing a custom geometry check consists of the following parts
Writing the check
A new subclass of QgsGeometryCheck needs to be written and at least the following abstract methods need to be implemented:
A list of geometry types to which this check applies
A list of names for (automated) resolution methods that can be used to fix errors of this type
A description for the geometry check.
A unique id for this check.
One of QgsGeometryCheck.LayerCheck, QgsGeometryCheck.FeatureCheck,QgsGeometryCheck.FeatureNodeCheck
collectErrors(featurePools, errors, messages, feedback, ids)This method will be called to validate geometries. All geometries which should be validated are passed into this method with the parameter ids and should be retrieved from the available featurePools to make use of caching. New errors should be appended to the error list and other message strings to messages. The method needs to return a tuple (errors, messages).
Creating a geometry check factory
A Geometry check factory manages meta information for checks. There will always be one single geometry check factory created per check type, but it's possible that multiple QgsGeometryCheck instances are created and used in parallel.
A new subclass of QgsGeometryCheckFactory needs to be written and at least the following abstract methods need to be implemented:
createGeometryCheck(context, configuration)Needs to return a new subclassed QgsGeometryCheck object that has been written in the previous step.
A unique id for this geometry check.
A description for this geometry check that can be presented to the user for more explanation.
QgsGeometryCheckFactory::isCompatible(layer)Returns a boolean that determines if this check is available for a given layer. This often checks for the geometry type of the layer.
Returns additional flags for a geometry check. If unsure return QgsGeometryCheck.AvailableInValidation.
Returns the type of this geometry check.
Registering the geometry check
Finally the geometry check factory needs to be registered in QGIS, so the system is aware of the available geometry checks.
# Make sure you always keep a checkFactory = MyGeometryCheckFactory() QgsAnalysis.geometryCheckRegistry().registerGeometryCheck(checkFactory)
Derived classes
- class QgsGeometryGapCheck
- Checks for gaps between neighbouring polygons.
- class QgsGeometryMissingVertexCheck
- A topology check for missing vertices.
- class QgsGeometryOverlapCheck
- Checks if geometries overlap.
- class QgsSingleGeometryCheck
- Base class for geometry checks for a single geometry without any context of the layer or other layers in the project.
Public types
- struct Change
- Descripts a change to fix a geometry.
- struct LayerFeatureIds
- A list of layers and feature ids for each of these layers.
-
using Changes = QMap<QString, QMap<QgsFeatureId, QList<QgsGeometryCheck::
Change>>> - A collection of changes.
- enum ChangeType { ChangeAdded, ChangeRemoved, ChangeChanged }
- Description of the type of a change.
- enum ChangeWhat { ChangeFeature, ChangePart, ChangeRing, ChangeNode }
- Description of a change to indicate at which level a change occurred.
- enum CheckType { FeatureNodeCheck, FeatureCheck, LayerCheck }
- The type of a check.
- enum Flag { AvailableInValidation = 1 << 1 }
- Flags for geometry checks.
Constructors, destructors, conversion operators
- QgsGeometryCheck(const QgsGeometryCheckContext* context, const QVariantMap& configuration)
- Create a new geometry check.
Public functions
- auto checkType() const -> CheckType pure virtual
- Returns the check type.
-
void collectErrors(const QMap<QString, QgsFeaturePool*>& featurePools,
QList<QgsGeometryCheckError*>& errors,
QStringList& messages,
QgsFeedback* feedback,
const LayerFeatureIds& ids = QgsGeometryCheck::
LayerFeatureIds()) const pure virtual - The main worker method.
-
auto compatibleGeometryTypes() const -> QList<QgsWkbTypes::
GeometryType> pure virtual - A list of geometry types for which this check can be performed.
-
template<class T>auto configurationValue(const QString& name, const QVariant& defaultValue = QVariant()) -> T
- Returns the configuration value with the name, saved in the QGIS settings for this geometry check.
- auto context() const -> const QgsGeometryCheckContext*
- Returns the context.
- auto description() const -> QString pure virtual
- Returns a human readable description for this check.
- void fixError(const QMap<QString, QgsFeaturePool*>& featurePools, QgsGeometryCheckError* error, int method, const QMap<QString, int>& mergeAttributeIndices, Changes& changes) const virtual
- Fix the error error with the specified method.
- auto flags() const -> QgsGeometryCheck::Flags virtual
- Flags for this geometry check.
- auto id() const -> QString pure virtual
- Returns an id for this check.
- auto isCompatible(QgsVectorLayer* layer) const -> bool virtual
- Returns if this geometry check is compatible with layer.
- auto resolutionMethods() const -> QStringList pure virtual
- Returns a list of descriptions for available resolutions for errors.
Protected functions
- auto allLayerFeatureIds(const QMap<QString, QgsFeaturePool*>& featurePools) const -> QMap<QString, QgsFeatureIds>
- Returns all layers and feature ids.
- void deleteFeatureGeometryPart(const QMap<QString, QgsFeaturePool*>& featurePools, const QString& layerId, QgsFeature& feature, int partIdx, Changes& changes) const
- Deletes a part of a feature geometry.
- void deleteFeatureGeometryRing(const QMap<QString, QgsFeaturePool*>& featurePools, const QString& layerId, QgsFeature& feature, int partIdx, int ringIdx, Changes& changes) const
- Deletes a ring in a feature geometry.
- void replaceFeatureGeometryPart(const QMap<QString, QgsFeaturePool*>& featurePools, const QString& layerId, QgsFeature& feature, int partIdx, QgsAbstractGeometry* newPartGeom, Changes& changes) const
- Replaces a part in a feature geometry.
- auto scaleFactor(const QPointer<QgsVectorLayer>& layer) const -> double
- Determines the scale factor of a layer to the map coordinate reference system.
Enum documentation
enum QgsGeometryCheck:: ChangeType
Description of the type of a change.
| Enumerators | |
|---|---|
| ChangeAdded |
Something has been added. |
| ChangeRemoved |
Something has been removed. |
| ChangeChanged |
Something has been updated. |
enum QgsGeometryCheck:: ChangeWhat
Description of a change to indicate at which level a change occurred.
| Enumerators | |
|---|---|
| ChangeFeature |
This change happens on feature level. |
| ChangePart |
This change happens on part level. |
| ChangeRing |
This change happens on ring level. |
| ChangeNode |
This change happens on node level. |
enum QgsGeometryCheck:: CheckType
The type of a check.
| Enumerators | |
|---|---|
| FeatureNodeCheck |
The check controls individual nodes. |
| FeatureCheck |
The check controls geometries as a whole. |
| LayerCheck |
The check controls a whole layer (topology checks) |
enum QgsGeometryCheck:: Flag
Flags for geometry checks.
| Enumerators | |
|---|---|
| AvailableInValidation |
This geometry check should be available in layer validation on the vector layer peroperties. |
Typedef documentation
typedef QMap<QString, QMap<QgsFeatureId, QList<QgsGeometryCheck:: Change>>> QgsGeometryCheck:: Changes
A collection of changes.
Grouped by layer id and feature id.
Function documentation
void QgsGeometryCheck:: collectErrors(const QMap<QString, QgsFeaturePool*>& featurePools,
QList<QgsGeometryCheckError*>& errors,
QStringList& messages,
QgsFeedback* feedback,
const LayerFeatureIds& ids = QgsGeometryCheck:: LayerFeatureIds()) const pure virtual
The main worker method.
Check all features available from featurePools and write errors found to errors. Other status messages can be written to messages. Progress should be reported to feedback. Only features and layers listed in ids should be checked.
QList<QgsWkbTypes:: GeometryType> QgsGeometryCheck:: compatibleGeometryTypes() const pure virtual
A list of geometry types for which this check can be performed.
template<class T>
T QgsGeometryCheck:: configurationValue(const QString& name,
const QVariant& defaultValue = QVariant())
Returns the configuration value with the name, saved in the QGIS settings for this geometry check.
If no configuration could be found, defaultValue is returned.
const QgsGeometryCheckContext* QgsGeometryCheck:: context() const
Returns the context.
QString QgsGeometryCheck:: description() const pure virtual
Returns a human readable description for this check.
void QgsGeometryCheck:: fixError(const QMap<QString, QgsFeaturePool*>& featurePools,
QgsGeometryCheckError* error,
int method,
const QMap<QString, int>& mergeAttributeIndices,
Changes& changes) const virtual
Fix the error error with the specified method.
QString QgsGeometryCheck:: id() const pure virtual
Returns an id for this check.
bool QgsGeometryCheck:: isCompatible(QgsVectorLayer* layer) const virtual
Returns if this geometry check is compatible with layer.
By default it checks for the geometry type in compatibleGeometryTypes().
QStringList QgsGeometryCheck:: resolutionMethods() const pure virtual
Returns a list of descriptions for available resolutions for errors.
The index will be passed as method to
QMap<QString, QgsFeatureIds> QgsGeometryCheck:: allLayerFeatureIds(const QMap<QString, QgsFeaturePool*>& featurePools) const protected
Returns all layers and feature ids.
void QgsGeometryCheck:: deleteFeatureGeometryPart(const QMap<QString, QgsFeaturePool*>& featurePools,
const QString& layerId,
QgsFeature& feature,
int partIdx,
Changes& changes) const protected
Deletes a part of a feature geometry.
void QgsGeometryCheck:: deleteFeatureGeometryRing(const QMap<QString, QgsFeaturePool*>& featurePools,
const QString& layerId,
QgsFeature& feature,
int partIdx,
int ringIdx,
Changes& changes) const protected
Deletes a ring in a feature geometry.
void QgsGeometryCheck:: replaceFeatureGeometryPart(const QMap<QString, QgsFeaturePool*>& featurePools,
const QString& layerId,
QgsFeature& feature,
int partIdx,
QgsAbstractGeometry* newPartGeom,
Changes& changes) const protected
Replaces a part in a feature geometry.
double QgsGeometryCheck:: scaleFactor(const QPointer<QgsVectorLayer>& layer) const protected
Determines the scale factor of a layer to the map coordinate reference system.