1

The following expression enables snapping for PyQGIS 2.0 :

QgsProject.instance().setSnapSettingsForLayer(layer.id(), True, 2, 1, 10, True)

What is the equivalent expression in PyQGIS 3.0?

3

In PyQGIS 3, according to the PyQGIS documentation of the QgsProject class, the snapping settings are contained in a QgsSnappingConfig object passed to the QgsProject instance.

So in PyQGIS 3, the expression becomes something like this:

my_snap_config = QgsSnappingConfig()
my_snap_config.setEnabled(True)
my_snap_config.setType(QgsSnappingConfig.VertexAndSegment)
my_snap_config.setUnits(QgsTolerance.Pixels)
my_snap_config.setTolerance(10)
my_snap_config.setIntersectionSnapping(True)

QgsProject.instance().setSnappingConfig(my_snap_config)

using the setMode method of QgsSnappingConfig it is possible to define the snapping for all the layers, for the active layer or to create a per-layer configuration.

  • Many thanks Marioba. It works perfectly. For snapping with all layers, we can add : my_snap_config.setMode(QgsSnappingConfig.AllLayers) – ennine Sep 19 '18 at 19:39

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.