[GIS] “Merge Parts” tool in QGIS

digitizingmergemultipartqgistools

In the advanced digitizing toolbar, QGIS has these 3 tools: Split Features, Split Parts, and Merge Features. Is there a tool or equivalent algorithm for the logical fourth one: "Merge Parts"?

This would have the opposite effect of Split Parts, which takes one part of a multipart feature and splits it into two adjoining parts of the same feature. Conversely, "Merge Parts" would take two (or more) adjoining parts of the same feature and make them a single part of that feature. It would not affect the singlepart/multipart status of the feature (or the layer as a whole).

In the image below there are two multipolygon features, which I realize I should have called Feature A and Feature B (rather than Polygon A and Polygon B). Feature A has 2 parts, and Feature B has 4 parts; note that parts 1 and 2 in Feature B share a common boundary. I've selected Feature B, and I'd like to execute a tool to join parts 1 and 2 into a single part, as shown in the "after" section. Feature B would then still be a multipolygon, but would have only 3 parts instead of 4.
enter image description here

The closest I can find is the "merge lines" algorithm; however this would not work with multipolygon features. It also merges all connected parts of a line feature, instead of just selected ones. (Is it even possible to select individual parts?) Is there anything else I'm missing?

Best Answer

Is there a tool or equivalent algorithm for the logical fourth one: "Merge Parts"?

No, there isn't.

The adjacent parts of a multipolygon are considered a topological error and must be linked with the Fix geometries tool.

Is it even possible to select individual parts?

No, it isn't.

You can select features, not individual parts.

Is there anything else I'm missing?

I don't know. But you can refer to parts of collections with the geometry_n() function in expressions.

For example, you can create a new feature (in a separate layer) with the Geometry by expression tool, that contains the combination of two parts (or more if the function is nested) of a feature or of several, of any layer. If the parts are adjacent, they will form a single part in the new feature.

To create a feature formed by parts 1 and 2, of the feature_ID 0, of the 'multipolygons' layer:

combine( 
    geometry_n( geometry( get_feature_by_id( 'multipolygons', 0)), 1), 
    geometry_n( geometry( get_feature_by_id( 'multipolygons', 0)), 2)
    )  

Note than the Geometry by expression tool copies the table from the input layer. If the input layer have more than one features, more than one features will be created, but only one will have a valid geometry with that expression.


UPDATE:

About the WKT of the Multipolygon provided in the comments (https://pastebin.com/1ibYDzR5), I can't seem a line shared by both parts.

When loading the feature in QGIS, it can be seen (with an appropriate zoom level), that the parts are not adjacent.

Upper:
upper

Lower:
lower

Be sure to snapping when digitizing and editing (here is the manual). Otherwise, you can not merge those parts as they are in any way.