[Qgis-developer] Problem definig snapping options programmatically

EFTAS Christian Röttger Christian.Roettger at eftas.com
Thu Jun 23 06:38:58 PDT 2016


Hi developers,

i'm facing a problem which i do not understand. In our plugin I created a function to set snapping options and bound it to a shortcut.
Using Qgis 2.14.3, source code see below.

The assigned shortcut works well, but for the snapping I have to do the following on every QGIS start:
Go to snapping options, mode = advanced, press ok (without changing anything). Then it works afterwards!
But when I print the options before I do this, I definitely get the right options (see below).

Does anyone know what I'm missing? Should I open an bug report? Is there a workaround do do: "Go to snapping options, mode = advanced, press ok" programmatically ?

Thanks for any help
Christian

## Print result
Initialize, when aggroInstance is true
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
checked
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, True, 2, 0, 2.0, False)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)
(True, True, 2, 0, 2.0, False)
(True, False, 2, 0, 2.0, True)
(True, False, 2, 0, 2.0, True)

## function

def toggleSnapping(checked):
    '''
    Toggle snapping option
    :param checked: True if snapping has been activated, False otherwise
    '''
    aggroInstance = initModule.aggroInstance
    print 'Initialize'
    if bool(agroInstance):
        for item in QgsMapLayerRegistry.instance().mapLayers().values():
            QgsProject.instance().setSnapSettingsForLayer(item.id(), False, 2, 0, 2, True)
        fieldLayer = mapUtils.getLayerByName(aggroInstance.dictData['field'])
        backgroundLayer = mapUtils.getLayerByName('bg')
        for item in QgsMapLayerRegistry.instance().mapLayers().values():
            print QgsProject.instance().snapSettingsForLayer(item.id())
        if checked:
            print 'checked'
            # Convenience function to set snap settings per layer.
            # it defines the snapping options:
            # id : the id of your layer, True : to enable the layer snapping, 2 : options (2: vertex+segment), 0: type of unit on map, 2 : tolerance, true : avoidIntersection)
            QgsProject.instance().setSnapSettingsForLayer(fieldLayer.id(), True, 2, 0, 2, False)
            QgsProject.instance().setSnapSettingsForLayer(backgroundLayer.id(), True, 2, 0, 2, False)
            for item in QgsMapLayerRegistry.instance().mapLayers().values():
                print QgsProject.instance().snapSettingsForLayer(item.id())


## initialization in run() method


# Call Snapping Tool by a key shortcut
for action in self.actions:
    if action.text() == 'Toggle Snapping':
        # Action triggered by shortcut key
        self.iface.registerMainWindowAction(action, "c")





--

[eftas3]
Dipl.-Geoinf. Christian Röttger
-Forschung und Entwicklung
E F T A S    Fernerkundung
Technologietransfer GmbH
Oststraße 2-18
48145 Münster
Fon: +49 251 13307-23         E-Mail: christian.roettger at eftas.com<mailto:christian.roettger at eftas.com>
Fax:  +49 251 13307-33        Web:   http://www.eftas.com<http://www.eftas.com/>
Geschäftsführer:
Dipl.-Ing. Georg Altrogge

Sitz der Gesellschaft: Münster
Amtsgericht Münster, HRB 2999
USt.-IdNr. DE 126038986
******************************************************************
[image007]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160623/ee5188af/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 127 bytes
Desc: image001.gif
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160623/ee5188af/attachment-0003.gif>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.gif
Type: image/gif
Size: 3325 bytes
Desc: image002.gif
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160623/ee5188af/attachment-0004.gif>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.gif
Type: image/gif
Size: 199 bytes
Desc: image003.gif
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160623/ee5188af/attachment-0005.gif>


More information about the Qgis-developer mailing list