3

I use the QGIS measure-tool to measure distances. I often would like to deactivate the snapping temporarily while using the measure-tool.

Is it possible to deactivate snapping (for example with a shortcut) while measuring and re-activating is when i digitize again?

5

We could create a function whereby:

  • If the mActionMeasure button is checked, the snapping mode is turned Off
  • If the mActionMeasure button is unchecked, the snapping mode is turned On

Here is the function which you can paste into the Python Console (change the parameters to fit your needs, you don't need to change the lines which include proj.setSnapSettingsForLayer(...)):

def snap_func():
    proj = QgsProject.instance()
    layer = qgis.utils.iface.activeLayer()
    if iface.actionMeasure().isChecked():
        proj.writeEntry('Digitizing', 'DefaultSnapType', 'off') 
        proj.setSnapSettingsForLayer(layer.id(), True, 0, 0, 0, True)
    else:
        proj.writeEntry('Digitizing', 'DefaultSnapType', 'to vertex and segment') 
        proj.writeEntry('Digitizing', 'SnappingMode', 'current_layer') 
        proj.writeEntry('Digitizing','DefaultSnapTolerance', 10.0)
        proj.setSnapSettingsForLayer(layer.id(), True, 0, 0, 0, True)

Then type the following to connect the mActionMeasure button to the function:

iface.actionMeasure().toggled.connect(snap_func)

Edit:

  • To save and restore snapping settings for Current and All visible layers:

    proj = QgsProject.instance()
    # Create dictionary
    snap_options_dict = {}
    def snap_func():
        layer = qgis.utils.iface.activeLayer()  
        if iface.actionMeasure().isChecked():
            # Save current snapping settings        
            snap_options_dict['SnappingMode'] = proj.readEntry('Digitizing','SnappingMode')[0]
            snap_options_dict['DefaultSnapType'] = proj.readEntry('Digitizing','DefaultSnapType')[0]
            snap_options_dict['DefaultSnapTolerance'] = proj.readEntry('Digitizing','DefaultSnapTolerance')[0]
            # Switch snapping off
            proj.writeEntry('Digitizing', 'DefaultSnapType', 'off') 
            proj.writeEntry('Digitizing', 'DefaultSnapTolerance', 0.0) 
            proj.setSnapSettingsForLayer(layer.id(), True, 0, 0, 0, True)
        else:
            # Load snapping settings saved in dictionary
            proj.writeEntry('Digitizing', 'SnappingMode', snap_options_dict['SnappingMode']) 
            proj.writeEntry('Digitizing', 'DefaultSnapType', snap_options_dict['DefaultSnapType'])
            proj.writeEntry('Digitizing','DefaultSnapTolerance', snap_options_dict['DefaultSnapTolerance'])
            proj.setSnapSettingsForLayer(layer.id(), True, 0, 0, 0, True)
    
    iface.actionMeasure().toggled.connect(snap_func)
    
  • To save and restore snapping settings for Advanced:

    proj = QgsProject.instance()
    # Create dictionary
    snap_options_dict = {}
    def snap_func():
        if iface.actionMeasure().isChecked():
            for layer in QgsMapLayerRegistry.instance().mapLayers().values():
                # Save current snapping settings        
                snap_options_dict[layer.id()] = proj.snapSettingsForLayer(layer.id())
                # Switch snapping off
                proj.setSnapSettingsForLayer(layer.id(), False, 0, 0, 0, False)
        else:
            for layer in QgsMapLayerRegistry.instance().mapLayers().values():
                # Load snapping settings saved in dictionary
                proj.setSnapSettingsForLayer(layer.id(), \
                snap_options_dict[layer.id()][1], \
                snap_options_dict[layer.id()][2], \
                snap_options_dict[layer.id()][3], \
                snap_options_dict[layer.id()][4], \
                snap_options_dict[layer.id()][5])
    
    iface.actionMeasure().toggled.connect(snap_func)
    
  • This looks good. Is it also possible to save the current snapping settings and restore them? I use advanced snapping settings with different settings for several layers? So the best option would be saving the current snapping settings and restore them after deactivating the measure-tool. Is this possible, too? – markgraeflerland Apr 19 '17 at 13:58
  • @markgraeflerland - Edited my post (again) to show two methods for saving and restoring snapping settings depending on Layer selection :) – Joseph Apr 20 '17 at 11:34
  • 1
    Great solution. works as expected – markgraeflerland Apr 24 '17 at 7:31
  • @markgraeflerland - Glad it worked :) – Joseph Apr 24 '17 at 8:54

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.