2

How is it possible to change the "Avoid Intersections" parameter with PyQGIS in QGIS 3.10?

I want to enable "avoid interceptions", so that in edit mode, when the new polygon overlaps the polygons of the active layer, the part where there is an overlap is automatically deleted (auto-complete editing of the polygon).

In QGIS 2.X the QgsProject().setSnapSettingsForLayer() function included the "Avoid Intersections" parameter but this is not included in the QgsSnappingConfig().IndividualLayerSettings() function in QGIS 3.10.

layer = qgis.utils.iface.activeLayer()

snap_config = QgsSnappingConfig()
snap_config.setEnabled(True)
snap_config.setType(QgsSnappingConfig.VertexAndSegment)
snap_config.setUnits(QgsTolerance.Pixels)
snap_config.setTolerance(10)
snap_config.setIntersectionSnapping(True)
snap_config.setMode(QgsSnappingConfig.SnappingMode.AdvancedConfiguration)

lyr_settings = QgsSnappingConfig.IndividualLayerSettings(
    True, QgsSnappingConfig.SnappingType.VertexAndSegment,
    10, QgsTolerance.Pixels)
snap_config.setIndividualLayerSettings(layer, lyr_settings)

QgsProject.instance().setSnappingConfig(snap_config)
2

You were not far from the answer :)

Use the following (I consider iface.activeLayer() is a polygon layer)

# To set "avoid overlap" on active layer
QgsProject.instance().setAvoidIntersectionsLayers([iface.activeLayer()])
# To empty the list of "avoid overlap" layers
QgsProject.instance().setAvoidIntersectionsLayers([]) 

I've checked by manually setting the config at GUI level and testing QgsProject.instance().avoidIntersectionsLayers() to confirm my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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