3

I have a polygon layer and I am trying to catch the geometry error by using validateGeometry(). The following snippet is working and finding the reason for the error as well as the location.

layer = iface.activeLayer()
features = layer.getFeatures()

for current, inFeat in enumerate(features):
    geom = inFeat.geometry()
    if not geom.isNull() and not geom.isEmpty():
        errors = list(geom.validateGeometry())
        if errors:
            print(inFeat['ID'], errors)

            reasons = []
            for error in errors:
                errFeat = QgsFeature()
                error_geom = QgsGeometry.fromPointXY(error.where())
                reasons.append(error.what())

My error message with the snippet looks like this

['segments 4 and 6 of line 0 intersect at 357246.699102, 385179.300166', 'segments 4 and 8 of line 0 intersect at 357252.065605, 385179.208431']

However, I would like to have the error message similar Check Validity Tool which shows the error as in the image only the type of the error e.g. Self-intersection

enter image description here

How do I do that?

2

1 Answer 1

3

The error message you get using QgsGeometry.validateGeometry() depends on the validity method you choose.

If you choose QGIS validation, you'll get messages like the one you copied in your question, whereas if you choose GEOS validation, you'll get error messages like in the screenshot you posted.

QGIS validation is the default validation for QgsGeometry.validateGeometry(). Switch to GEOS validation in this way:

geom.validateGeometry(QgsGeometry.ValidatorGeos)

You can read the list of error messages per validation method here:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.