[Qgis-developer] Python: How to allow for more results using QgsSnapper.snapMapPoint(...)

Casper Børgesen (CABO) CABO at NIRAS.DK
Fri Aug 12 03:56:53 PDT 2016


Hi Martin

When using the QgsSnappingUtils and between snapping make some changes to the features in the layer I'm snapping to, the snapping mechanism stops working.

Example: 

1. I have enabled snapping
2. The user snaps to an edge.
3. I then cut up a geometry (the one the user snapped to), replacing the geometry of the snapped feature and insert new features for the remaining geometries from the cut.
4. Snapping now only works on the geometry which weren't changed and part of the changed geometry.

Cut pseudo code:
    geometry = QgsGeometry(f.geometry())
    (result, other, pts) = geometry.splitGeometry([p1, ..., pn])

    # Update feature.
    layer.startEditing()
    layer.dataProvider().changeGeometryValues({f.id() : geometry})
    # Insert new feature.
    f2 = QgsFeature(layer.pendingFields())
    f2.setAttributes(f.attributes())
    f2.setGeometry(other[0])
    layer().dataProvider().addFeatures([f2])
    layer.commitChanges()

I can think of two reasons here. Either I do the cutting in an impropriate way or I need to update an index somewhere.

Can you point me in the right direction?

Regards, Casper

-----Original Message-----
From: Qgis-developer [mailto:qgis-developer-bounces at lists.osgeo.org] On Behalf Of Casper Børgesen (CABO)
Sent: 12. august 2016 10:02
To: Martin Dobias
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [Qgis-developer] Python: How to allow for more results using QgsSnapper.snapMapPoint(...)

Hi Martin

Thanks for the reference to your nodetool and the concept works great for me. Its cool to know about the locators cache too. I have always thought of filters as being an object for the method to use and not an object which can collect and afterwards return information. My mind is expanding...

Hopefully I'm now a step closer to being QGIS 3.0 compliant :)

Regards, Casper

-----Original Message-----
From: Martin Dobias [mailto:wonder.sk at gmail.com] 
Sent: 11. august 2016 18:10
To: Casper Børgesen (CABO)
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [Qgis-developer] Python: How to allow for more results using QgsSnapper.snapMapPoint(...)

On Thu, Aug 11, 2016 at 3:31 PM, Casper Børgesen (CABO) <CABO at niras.dk> wrote:
> Hi Martin
>
> My goal was to use the snapping functionality to snap to the nearest edges/vertices and when a snap occurred I would evaluate the features involved in the snap. If two or more edges where equally close (ex. on top of each other) I needed to remember which ones it was.
>
> Using the methods of QgsSnappingUtils I only get one match back. Is it possible to get all the matches back or do I need to use the QgsPointLocater afterwards to determine if I was snapping to more than one feature? I see no reason to make two lookups if the results I'm looking for is available already.

There are more way how to do it - one as you say would be to check with individual QgsPointLocator instances. Internally the locators build their cache, so subsequent calls are fast anyway.

The other option would be to use QgsSnappingUtils::snapToMap() with a custom filter (an optional argument in various QgsSnappingUtils and QgsPointLocator methods). Your filter does not need to filter out any matches, but instead it can collect any relevant matches that would not be returned otherwise. See for example here [1].

Cheers
Martin

[1] https://github.com/wonder-sk/CadNodeTool/blob/master/nodetool.py#L513
_______________________________________________
Qgis-developer mailing list
Qgis-developer at lists.osgeo.org
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list