[Qgis-developer] Setting snapping settings via Python

John Stevenson john.stevenson at thinkwhere.com
Fri Jan 22 07:00:16 PST 2016

I have now managed to change the settings for an individual layer, via the QgsProject class.  But this method only works if the snapping mode is set to 'Advanced' in the GUI dialog (Settings > Snapping Options).

proj = QgsProject.instance()
proj.setSnapSettingsForLayer(layer.id(), True, QgsSnapper.SnapToVertexAndSegment, QgsTolerance.LayerUnits, 5.0, False)

Is there a way to set this snapping mode via Python?


John Stevenson
Senior Developer

[cid:image6f4c18.PNG at f30681fe.4690ed71]<http://www.thinkwhere.com>

t: 01786 476060 (Office)
t: 01786 476093 (Direct Dial)

Glendevon House
Castle Business Park
Stirling FK9 4TZ

[cid:imagecff972.PNG at 262d976b.4a8cbca9]<https://twitter.com/thinkWhere1> [cid:image43e08f.PNG at b83ed55e.4f9fea1d] <http://www.linkedin.com/company/1509510?trk=companies_home_ycp_logo_forth-valley-gis>

This email and its attachments are private and confidential. If you believe you have received this email in error, please contact the sender immediately on 01786 476060. If you are not the intended recipient, you may not rely on, use or disclose this email or any attachments in any way.

thinkWhere Limited does not accept any liability for any damage that may be caused to the recipient's system or data by this email or any attachments. Please note that our email system may be subject to random monitoring by us. This Company accepts no liability for personal emails.

thinkWhere is a limited company registered in Scotland with Registered Number SC315349 and having its Registered Office at Glendevon House, Castle Business Park, Stirling, FK9 4TZ.

From: John Stevenson
Sent: 22 January 2016 11:57
To: Thomas Gratier; Tom Chadwin
Cc: qgis-developer
Subject: RE: [Qgis-developer] Setting snapping settings via Python

Thanks for help so far.

These suggestions seem to be more complicated than I require.  I do not need to use the snapper to modify geometries in a custom tool, just to turn snapping on for the current layer when the user begins editing it.  I tried the following, based on Etienne's inasafe example, but nothing happens:

layer = canvas.layer(0)
point_locator = QgsPointLocator(layer)
units = QgsTolerance.LayerUnits
layer_config = QgsSnappingUtils.LayerConfig(layer, point_locator.Types(), 50, units)
snapper = QgsSnappingUtils()

Is there another snapper already created by QGIS when editing begins that I can access?

From: Qgis-developer [qgis-developer-bounces at lists.osgeo.org] on behalf of Thomas Gratier [osgeo.mailinglist at gmail.com]
Sent: 21 January 2016 11:04
To: Tom Chadwin
Cc: qgis-developer
Subject: Re: [Qgis-developer] Setting snapping settings via Python


I'm maybe wrong but you'd better look at QgsSnappingUtils http://qgis.org/api/classQgsSnappingUtils.html
It seems more simple and should do the job for you.


Thomas Gratier

2016-01-20 20:32 GMT+01:00 Tom Chadwin <tom.chadwin at nnpa.org.uk<mailto:tom.chadwin at nnpa.org.uk>>:
This old thread might also help:


View this message in context: http://osgeo-org.1560.x6.nabble.com/Setting-snapping-settings-via-Python-tp5246362p5246373.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
Qgis-developer mailing list
Qgis-developer at lists.osgeo.org<mailto:Qgis-developer at lists.osgeo.org>
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160122/b8a41500/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image6f4c18.PNG
Type: image/png
Size: 7004 bytes
Desc: image6f4c18.PNG
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160122/b8a41500/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: imagecff972.PNG
Type: image/png
Size: 3618 bytes
Desc: imagecff972.PNG
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160122/b8a41500/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image43e08f.PNG
Type: image/png
Size: 3751 bytes
Desc: image43e08f.PNG
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160122/b8a41500/attachment-0005.png>

More information about the Qgis-developer mailing list