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

Martin Dobias wonder.sk at gmail.com
Fri Aug 12 04:21:16 PDT 2016


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