Bug report #10956

QgsSnapper crash/freeze/issues

Added by Matthias Kuhn over 9 years ago. Updated over 9 years ago.

Status:Closed
Priority:Severe/Regression
Assignee:-
Category:Digitising
Affected QGIS version:2.4.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:19304

Description

Executing the following python code and then clicking the map canvas works on QGIS 2.2.

On QGIS 2.4 it needs to be adapted, so a new snapper gets created in the callback to restore functionality (Minor issue as the workaround is backwards compatible).

At the same time, a warning gets printed to the log about deprecation of .mapRenderer() and a suggestion to use .mapSettings() instead.

If this is done

  • QGIS 2.4 freezes
  • QGIS 2.5 (master) crashes (sometimes) in the crs transformation code and never returns any snapped points

also when moving the snapper instantiation to the callback there are no snapping results.

Code working in 2.2 but no longer in 2.4

from qgis.gui import QgsMapToolEmitPoint
from PyQt4.QtCore import QPoint

l = iface.activeLayer()
sl = QgsSnapper.SnapLayer()
sl.mLayer = l
sl.mTolerance = 10
sl.mUnitType = QgsTolerance.Pixels
sl.mSnapTo = QgsSnapper.SnapToVertex
s = QgsSnapper( iface.mapCanvas().mapRenderer() )
s.setSnapLayers( [sl] )

mt = QgsMapToolEmitPoint( iface.mapCanvas() )

def canvasclicked( mpt, e ):
    # transform map to device coordinate
    dpt = iface.mapCanvas().getCoordinateTransform().transform( mpt )
    ( _, sp ) = s.snapPoint( QPoint( dpt.x(), dpt.y() ) )
    print '{} points found'.format( len( sp ) )

mt.canvasClicked.connect( canvasclicked )
iface.mapCanvas().setMapTool( mt )

QGIS 2.4 deprecation adapted code: no snapping results are found

from qgis.gui import QgsMapToolEmitPoint
from PyQt4.QtCore import QPoint

l = iface.activeLayer()

mt = QgsMapToolEmitPoint( iface.mapCanvas() )

def canvasclicked( mpt, e ):
    sl = QgsSnapper.SnapLayer()
    sl.mLayer = l
    sl.mTolerance = 10
    sl.mUnitType = QgsTolerance.Pixels
    sl.mSnapTo = QgsSnapper.SnapToVertex
    s = QgsSnapper( iface.mapCanvas().mapSettings() )
    s.setSnapLayers( [sl] )
    # transform map to device coordinate
    dpt = iface.mapCanvas().getCoordinateTransform().transform( mpt )
    ( _, sp ) = s.snapPoint( QPoint( dpt.x(), dpt.y() ) )
    print '{} points found'.format( len( sp ) )

mt.canvasClicked.connect( canvasclicked )
iface.mapCanvas().setMapTool( mt )

Associated revisions

Revision 43c4cd72
Added by Martin Dobias over 9 years ago

Fix #10956 (QgsSnapper crash/freeze/issues)

QgsMapSettings instance of QgsMapCanvas may have been prematurely deleted by SIP.
The KeepReference annotation will force QgsMapCanvas wrapper to keep one more ref
to it, so its reference count will not go to zero while map canvas is still alive.

History

#1 Updated by Giovanni Manghi over 9 years ago

  • Priority changed from Normal to Severe/Regression

#2 Updated by Jürgen Fischer over 9 years ago

  • Category set to Digitising

#3 Updated by Martin Dobias over 9 years ago

  • Status changed from Open to Closed

#4 Updated by Martin Dobias over 9 years ago

Btw. the code with QgsMapRenderer works for me with master...

Also available in: Atom PDF