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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 11, 2020 at 18:22

1 Answer 1

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.

answered Jun 11, 2020 at 19:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.