I have a layer which contains line features and I to want dissolve selected features with those that are connected to each other in the selection.

I tried Dissolve algorithm. It will create the separate memory layer but I would like to be able to perform this action, similar to other processing tools that have the 'Edit Features In-Place' option available for it.

This would mean updating existing line features in the current layer. The assumption is that the tool will update the first selected record and maintain the attributes, and remove any subsequently selected records.

QgsGeometry class contains mergeLines method.

Im not sure if this is the correct method to use?

  • What if features have different attribute values? Sep 25, 2021 at 10:52
  • We can choose any one feature attributes Sep 25, 2021 at 12:02
  • As far as I understand, QgsGeometry.mergeLines converts multiline geometry (of one feature) to singleline geometry if it is possible to represent as a single line. I mean, it doesn't merge different features' geometry to one feature geometry. I could be wrong, though. Sep 25, 2021 at 19:49

2 Answers 2


If I'm not misunderstanding your question, the code below will do the trick.

layer = iface.activeLayer()

ids = layer.selectedFeatureIds()
fs = list(layer.selectedFeatures()) # fs: features

# combine selected geometries
g = fs[0].geometry()
for i in range(len(fs) - 1):
    g = fs[i+1].geometry().combine(g)

# add new feature, set attribute and geometry
new_feature = QgsFeature(fs[0].fields())

# delete selected features


enter image description here


One approach could be:

  • Use "Dissolve" algorithm in Processing Toolbox

  • Then, use "Multiparts to singleparts" algorithm

  • Then, use "Field calculator" algorithm using previous resulting layer as an input with an expression like below to concatenate into a new or existing field the list of identifiers from initial layer

 filter:=contains(geometry(@parent), $geometry)))

Not sure why you want to "update the existing geometries". If you have for instance 3 records/lines, amongst them, 1 alone, 2 connected, you will get only 2 geometries as a result to update 3 records/lines. Except duplicating the geometry for connected lines, I don't see how it could work.

  • I think to clarify the 'update the existing geometries', the logic would be, given lines A and B to dissolve, update record A to have the geometry of A and B and attributes of A(or B, perhaps as an option) and then remove record B entirely.
    – nr_aus
    May 1, 2023 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.