Setting snapping settings via Python

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting snapping settings via Python

John Stevenson
Hi,

Is there a way to set the snapping settings for a layer via Python?  I would like to turn on snapping with a specified tolerance automatically when a user begins editing within a plugin.

There is a question on gis.stackexchange, but it hasn't been answered.

http://gis.stackexchange.com/questions/133816/how-to-enable-the-snapping-options-for-vector-layer-using-the-python-programming

QSettings stores some values, but these are the defaults:

    qs = QSettings()
    qs.value('Qgis/digitizing/default_snap_mode')
    qs.value('Qgis/digitizing/default_snapping_tolerance')
    qs.value('Qgis/digitizing/default_snapping_tolerance_units')

I have also found the canvas.snappingUtils(), but cannot find functions there to enable snapping modes or set tolerances.

Thanks,
John

John Stevenson
Senior Developer

t: 01786 476060 (Office)
t: 01786 476093 (Direct Dial)
w:www.thinkwhere.com

Glendevon House
Castle Business Park
Stirling FK9 4TZ


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.



_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

Tom Chadwin
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

Tom Chadwin
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

ThomasG
Hi,

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.

Cheers

Thomas Gratier

2016-01-20 20:32 GMT+01:00 Tom Chadwin <[hidden email]>:
This old thread might also help:

http://osgeo-org.1560.x6.nabble.com/getting-QgsMapCanvasSnapper-to-work-td4147067.html



--
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
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

John Stevenson
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()
snapper.setLayers([layer_config])
snapper.setSnapToMapMode(QgsSnappingUtils.SnapAdvanced)

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

Cheers
John

John Stevenson
Senior Developer

t: 01786 476060 (Office)
t: 01786 476093 (Direct Dial)
w:www.thinkwhere.com

Glendevon House
Castle Business Park
Stirling FK9 4TZ


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: Qgis-developer [[hidden email]] on behalf of Thomas Gratier [[hidden email]]
Sent: 21 January 2016 11:04
To: Tom Chadwin
Cc: qgis-developer
Subject: Re: [Qgis-developer] Setting snapping settings via Python

Hi,

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.

Cheers

Thomas Gratier

2016-01-20 20:32 GMT+01:00 Tom Chadwin <[hidden email]>:
This old thread might also help:

http://osgeo-org.1560.x6.nabble.com/getting-QgsMapCanvasSnapper-to-work-td4147067.html



--
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
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

John Stevenson
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?

Thanks
John


John Stevenson
Senior Developer

t: 01786 476060 (Office)
t: 01786 476093 (Direct Dial)
w:www.thinkwhere.com

Glendevon House
Castle Business Park
Stirling FK9 4TZ


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()
snapper.setLayers([layer_config])
snapper.setSnapToMapMode(QgsSnappingUtils.SnapAdvanced)

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

Cheers
John

From: Qgis-developer [[hidden email]] on behalf of Thomas Gratier [[hidden email]]
Sent: 21 January 2016 11:04
To: Tom Chadwin
Cc: qgis-developer
Subject: Re: [Qgis-developer] Setting snapping settings via Python

Hi,

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.

Cheers

Thomas Gratier

2016-01-20 20:32 GMT+01:00 Tom Chadwin <[hidden email]>:
This old thread might also help:

http://osgeo-org.1560.x6.nabble.com/getting-QgsMapCanvasSnapper-to-work-td4147067.html



--
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
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

Tom Chadwin
Hi John

See Matthias's answer on this list just today to a more recent question. Does that do the trick?

Tom
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

Tom Chadwin
In reply to this post by John Stevenson
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

Matthias Kuhn 🌍
On 06/23/2016 05:45 PM, Tom Chadwin wrote:
> Matthias's post is here:
>
> https://lists.osgeo.org/pipermail/qgis-developer/2016-June/043418.html
>
> He suggests:
>
> QgsProject.instance().snapSettingsChanged()


Sorry, should probably be

QgsProject.instance().snapSettingsChanged.emit()

... I didn't verify that it works ...
_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

Laure-Hélène Bruneton
Thank you so very much !!!

QgsProject.instance().writeEntry('Digitizing', '/SnappingMode', 'advanced')
QgsProject.instance().snapSettingsChanged.emit()

This did the trick for me. I'm so surprised the answer wasn't easy to
find...



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

David Erill-2
I'm migrating a plugin to QGIS 3.x
I have this line of code that works in previous versions:
QgsProject.instance().snapSettingsChanged.emit()

But I cannot find this signal anymore in the new API.
I've been looking for another signal that does the same but didn't find.
Anyone can tell me how can I achieve the same with the new API?

Thanks

Missatge de Laure-Hélène Bruneton <[hidden email]> del dia dj., 8 de febr. 2018 a les 13:22:
Thank you so very much !!!

QgsProject.instance().writeEntry('Digitizing', '/SnappingMode', 'advanced')
QgsProject.instance().snapSettingsChanged.emit()

This did the trick for me. I'm so surprised the answer wasn't easy to
find...



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Setting snapping settings via Python

3nids

Le mer. 19 juin 2019 à 14:24, David Erill <[hidden email]> a écrit :
I'm migrating a plugin to QGIS 3.x
I have this line of code that works in previous versions:
QgsProject.instance().snapSettingsChanged.emit()

But I cannot find this signal anymore in the new API.
I've been looking for another signal that does the same but didn't find.
Anyone can tell me how can I achieve the same with the new API?

Thanks

Missatge de Laure-Hélène Bruneton <[hidden email]> del dia dj., 8 de febr. 2018 a les 13:22:
Thank you so very much !!!

QgsProject.instance().writeEntry('Digitizing', '/SnappingMode', 'advanced')
QgsProject.instance().snapSettingsChanged.emit()

This did the trick for me. I'm so surprised the answer wasn't easy to
find...



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer