Class: QgsGeometryCheck

Base class for geometry checks.

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:

  • compatibleGeometryTypes(): A list of geometry types to which this check applies

  • availableResolutionMethods(): A list of resolution methods that can be used to fix errors of this type

  • description(): A description for the geometry check.

  • id(): A unique id for this check.

  • checkType(): One of QgsGeometryCheck.LayerCheck, QgsGeometryCheck.FeatureCheck, QgsGeometryCheck.FeatureNodeCheck

  • collectErrors(): 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:

  • QgsGeometryCheckFactory.createGeometryCheck(): Needs to return a new subclassed QgsGeometryCheck object that has been written in the previous step.

  • QgsGeometryCheckFactory.id(): A unique id for this geometry check.

  • QgsGeometryCheckFactory.description(): A description for this geometry check that can be presented to the user for more explanation.

  • QgsGeometryCheckFactory.isCompatible(): Returns a boolean that determines if this check is available for a given layer. This often checks for the geometry type of the layer.

  • QgsGeometryCheckFactory.flags(): Returns additional flags for a geometry check. If unsure return QgsGeometryCheck.AvailableInValidation.

  • QgsGeometryCheckFactory.checkType(): 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 reference
checkFactory = MyGeometryCheckFactory()
QgsAnalysis.geometryCheckRegistry().registerGeometryCheck(checkFactory)

Note

This class is a technology preview and unstable API.

Added in version 3.4.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: checkType(), collectErrors(), compatibleGeometryTypes(), description(), id()

Class Hierarchy

Inheritance diagram of qgis.analysis.QgsGeometryCheck

Subclasses

QgsSingleGeometryCheck

Base class for geometry checks for a single geometry without any context of the layer or other layers in the project.

Abstract Methods

checkType

Returns the check type.

collectErrors

The main worker method.

compatibleGeometryTypes

A list of geometry types for which this check can be performed.

description

Returns a human readable description for this check.

id

Returns an id for this check.

Methods

context

Returns the context

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsGeometryCheck. See the FAQ for more details.

availableResolutionMethods

Returns a list of available resolution methods.

flags

Flags for this geometry check.

isCompatible

Returns if this geometry check is compatible with layer.

prepare

Will be run in the main thread before collectErrors() is called (which may be run from a background thread).

resolutionMethods

Returns a list of descriptions for available resolutions for errors.

Attributes

AvailableInValidation

ChangeAdded

ChangeChanged

ChangeFeature

ChangeNode

ChangePart

ChangeRemoved

ChangeRing

FeatureCheck

FeatureNodeCheck

LayerCheck

class qgis.analysis.QgsGeometryCheck[source]

Bases: object

__init__(context: QgsGeometryCheckContext | None, configuration: Dict[str, Any])

Create a new geometry check.

Parameters:
__init__(a0: QgsGeometryCheck)
Parameters:

a0 (QgsGeometryCheck)

AvailableInValidation = 2
class Change

Bases: object

Descripts a change to fix a geometry.

Added in version 3.4.

type: QgsGeometryCheck.ChangeType

What action this change performs.

vidx: QgsVertexId

The index of the part / ring / vertex, depending on what().

what: QgsGeometryCheck.ChangeWhat

What level this change affects.

ChangeAdded = 0
ChangeChanged = 2
ChangeFeature = 0
ChangeNode = 3
ChangePart = 1
ChangeRemoved = 1
ChangeRing = 2
class ChangeType

Bases: int

class ChangeWhat

Bases: int

class CheckType

Bases: int

FeatureCheck = 1
FeatureNodeCheck = 0
class Flag

Bases: int

class Flags
class Flags(f: QgsGeometryCheck.Flags | QgsGeometryCheck.Flag)
class Flags(a0: QgsGeometryCheck.Flags)

Bases: object

LayerCheck = 2
class LayerFeatureIds

Bases: object

A list of layers and feature ids for each of these layers. In C++, the member ids can be accessed directly. In Python some accessor methods will need to be written.

Added in version 3.4.

virtual availableResolutionMethods(self) List[QgsGeometryCheckResolutionMethod]

Returns a list of available resolution methods.

Added in version 3.12.

Return type:

List[QgsGeometryCheckResolutionMethod]

abstract checkType(self) QgsGeometryCheck.CheckType[source]

Returns the check type.

Added in version 3.4.

Return type:

QgsGeometryCheck.CheckType

abstract collectErrors(self, featurePools: Any, errors: Iterable[QgsGeometryCheckError], messages: Iterable[str | None], feedback: QgsFeedback | None, ids: QgsGeometryCheck.LayerFeatureIds = QgsGeometryCheck.LayerFeatureIds())[source]

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.

Added in version 3.4.

Parameters:
abstract compatibleGeometryTypes(self) List[Qgis.GeometryType]

A list of geometry types for which this check can be performed.

Added in version 3.4.

Return type:

List[Qgis.GeometryType]

context(self) QgsGeometryCheckContext | None[source]

Returns the context

Added in version 3.4.

Return type:

Optional[QgsGeometryCheckContext]

abstract description(self) str[source]

Returns a human readable description for this check.

Added in version 3.4.

Return type:

str

virtual flags(self) QgsGeometryCheck.Flags[source]

Flags for this geometry check.

Return type:

QgsGeometryCheck.Flags

abstract id(self) str[source]

Returns an id for this check.

Added in version 3.4.

Return type:

str

virtual isCompatible(self, layer: QgsVectorLayer | None) bool[source]

Returns if this geometry check is compatible with layer. By default it checks for the geometry type in compatibleGeometryTypes().

Added in version 3.4.

Parameters:

layer (Optional[QgsVectorLayer])

Return type:

bool

virtual prepare(self, context: QgsGeometryCheckContext | None, configuration: Dict[str, Any])[source]

Will be run in the main thread before collectErrors() is called (which may be run from a background thread).

Added in version 3.10.

Parameters:
virtual resolutionMethods(self) List[str][source]

Returns a list of descriptions for available resolutions for errors. The index will be passed as method to fixError().

Deprecated since version 3.12: Use availableResolutionMethods() instead.

Added in version 3.4.

Return type:

List[str]