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

Casper Børgesen (CABO) CABO at NIRAS.DK
Fri Aug 12 05:26:41 PDT 2016


Hi Martin

Going through the layer object works much better, thanks yet again :)

Since I cannot remember all the syntaxes and ways to do stuff I found this page, which I have found so many times now:

http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html

The page suggest that you go through the data provider. Perhaps that is not the best way to go anymore, or does it depend on what you want to do?


Regards, Casper

-----Original Message-----
From: Martin Dobias [mailto:wonder.sk at gmail.com] 
Sent: 12. august 2016 13:21
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(...)

Hi Casper

On Fri, Aug 12, 2016 at 12:56 PM, Casper Børgesen (CABO) <CABO at niras.dk> wrote:
> 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.

Use layer.changeGeometry() and layer.addFeatures() instead of the methods from data provider. They do not notify the rest of the app about the changes, so the index does not get updated.

Maybe even better you can use QgsVectorLayerEditUtils.splitFeatures()
to cut features (it will do the splitGeometry calls, change geometries and add features).

Cheers
Martin


More information about the Qgis-developer mailing list