Bug report #15421

QgsSnapper return only one result on multiple items

Added by Casper Børgesen over 6 years ago. Updated almost 4 years ago.

Category:PyQGIS Console
Affected QGIS version:master_2 Regression?:No
Operating System:windows Easy fix?:No
Pull Request or Patch supplied:No Resolution:end of life
Crashes QGIS or corrupts data:No Copied to github as #:23348


I am trying to programmatically to snap to the nearest segment or segments. I have a layer with two lines (the corresponding SHAPE layer is attached):

  1. (0,0) -> (3,3)
  2. (1,1) -> (2,2)

These two lines has a common overlap corresponding to the second line.

I have set up the snapper as follows:

Setup the snap layer.
>>> sl = QgsSnapper.SnapLayer()
>>> sl.mLayer = iface.mapCanvas().currentLayer()
>>> sl.mTolerance = 25
>>> sl.mSnapTo = QgsSnapper.SnapToSegment
>>> sl.mUnitType = QgsTolerance.Pixels

Setup the snapper.
>>> sn = QgsSnapper(iface.mapCanvas().mapSettings())
>>> sn.setSnapLayers([sl])

I want more than one result if available.
>>> sn.setSnapMode = QgsSnapper.SnapWithResultsWithinTolerances

Snap using the center point of the common overlap - that should ensure both lines are detected.
>>> (retval, result) = sn.snapMapPoint(QgsPoint(1.5, 1.5))

The result, where I expect two items, but the following only gives me one.
>>> len(result)

According to https://github.com/qgis/QGIS/blob/master/src/core/qgssnapper.cpp#L108 using either QgsSnapper.SnapWithResultsForSamePosition or QgsSnapper.SnapWithResultsWithinTolerances should allow for more results.

What is going wrong for me here?

To reproduce:

  1. Open QGIS.
  2. Load the attached layer.
  3. Paste the above code into the Python console.

line_layer.shp (276 Bytes) Casper Børgesen, 2016-08-11 02:26 AM

line_layer.dbf (87 Bytes) Casper Børgesen, 2016-08-11 02:26 AM

line_layer.shx (116 Bytes) Casper Børgesen, 2016-08-11 02:26 AM


#1 Updated by Giovanni Manghi over 5 years ago

  • Easy fix? set to No
  • Regression? set to No

#2 Updated by Giovanni Manghi almost 4 years ago

  • Resolution set to end of life
  • Status changed from Open to Closed

Also available in: Atom PDF