Skip to content

Instantly share code, notes, and snippets.

@mtravis
Last active October 17, 2015 15:59
Show Gist options
  • Save mtravis/72c3a80397bd67421a25 to your computer and use it in GitHub Desktop.
Save mtravis/72c3a80397bd67421a25 to your computer and use it in GitHub Desktop.
turn snapping on/off in QGIS
from qgis.core import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def snapping_toggle():
layer = QgsMapLayerRegistry.instance().mapLayersByName("topographicline")[0]
#layer = iface.activeLayer()
_, enabled, _, _, _, _ = QgsProject.instance().snapSettingsForLayer(layer.id())
QgsProject.instance().setSnapSettingsForLayer(layer.id(), not enabled, 0, 1, 10, True)
print "set {} snapping {}".format(layer.name(), "Off" if not enabled else "On")
shortcut = QShortcut(QKeySequence(Qt.SHIFT + Qt.Key_S), iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(snapping_toggle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment