3

I have a line layer with lines potentially having multiple vertices (aside from start and end point) and I need the concerned features to be split at the vertex.

The explodelines algorithm does this but it creates a new layer. I need imperatively to edit my input layer as opposed to create a new one. I can't also create a new .shp layer and work with that one instead since I need to run a bunch of operations on it.

So far I've found these two threads Building a QGIS 3.10 script without an output layer and Modify the input layer when using processing algorithms from the console but I can't find a solution in neither.

If there's somehow a way to directly edit input layer directly using processing algorithms that would be the best case scenario as a lot of operations are covered by readily available algorithms already.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
IKindaNeedAHand
  • 311
  • 1
  • 9

2 Answers2

7

You can run Processing algorithms to edit input features in-place (that is, to modify the INPUT layer instead of modifying a copy) in this way:

from processing.gui.AlgorithmExecutor import execute_in_place

Get algorithm instance

registry = QgsApplication.instance().processingRegistry() alg = registry.algorithmById("native:explodelines")

Run the algorithm using 'in-place' edits

line_layer = iface.activeLayer() params = {'INPUT': line_layer} execute_in_place(alg, params)


Notes:

  • It is not required that your INPUT layer is the current active layer. Just pass a reference to a layer and you're done.
  • Make sure you don't have any feature selection on your input layer before running the algorithm. Otherwise, changes will only be applied to selected features.
  • After the algorithm execution, your layer will be in edit mode waiting for you to save changes (just like when you run it from the GUI). As you're using a script, it makes sense to save changes also automatically. For that, just write line_layer.commitChanges() at the end.
  • The native:explodelines only expects the INPUT/OUTPUT layer parameters. For other algorithms, you'd pass the whole set of parameters (see how to get parameters of a specific algorithm).
  • There is no need of passing the OUTPUT parameter to run an algorithm in-place, but if you pass it, the algorithm will still work in-place mode.
  • It may be useful to check if an algorithm can be run in-place on a specific layer, before running it. For that, you can use alg.supportInPlaceEdit(line_layer), which returns a boolean result.
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • 1
    Thank you, that does the trick. I ended up finding this thread while looking around https://gis.stackexchange.com/questions/327858/in-place-modification-from-python looking around but your explanation will help me a lot. – IKindaNeedAHand Sep 22 '21 at 09:35
3

Some of the processing scripts you can use "in place" just press the inplace button. Explode lines is one of them. There will no new layer produced.

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
eurojam
  • 10,762
  • 1
  • 13
  • 27
  • Hi, thanks for the quick response. I can get snap geometries to layer https://i.stack.imgur.com/scfP6.png but I don't get it for explode lines https://i.stack.imgur.com/vURTF.png

    Also, would there a way to do it through a python script as opposed to running it from the processing toolbox ?

    – IKindaNeedAHand Sep 20 '21 at 10:48
  • While running the snap geometries to layer algorithm while "Edit features in palce" enabled, the "source layer" argument is missing, so I can only the reference layer so I can't pick the point layer which it should snap to. @eurojam – IKindaNeedAHand Sep 20 '21 at 10:56
  • you have to select the layer before searching explode, if your active layer will be a pointlayer, there will no explode – eurojam Sep 20 '21 at 11:08
  • Great @eurojam - did not know about this possibility! – Babel Sep 20 '21 at 12:05