By Cord

2019-04-15 16:47:05 8 Comments

I am a fairly new user of QGIS having a question regarding dissolve functionality.

I am working with the NUTS-2 shapefiles provided by EU, which I have imported in QGIS. I have created a new layer with only Denmark and Netherlands as you can see in the picture below.

DK and NL

The corresponding attribute table is the following.

Attribute table

The fields describe the ID and name for each NUTS node, as well as the country code. I have introduced a new attribute field, called Merge_attr.

I have managed to dissolve the polygons with the same attribute value of Merge_attr. However, I would like to dissolve polygons based on the Merge_attr value only when they belong to the same country and potentially only when they are adjacent.

Can we constrain dissolve functionality to only polygons that are adjacent?

This means that the following should not take place (yellow polygons got merged even though not adjacent):

Non adjacent polygons dissolved


@csk 2019-04-15 17:57:09

Option one: Merge features only when they belong to the same country.

The Dissolve tool allows you to select more than one attribute as the Dissolve field. Use these two fields for the Dissolve field:

  • Merge_attr
  • Whichever field is unique for each country. In your dataset it looks like CNTR_CODE is a unique code for each country, so use that field. Or there might be a country name field called something like CNTR_NAME.

Option two: Merge features only when they are adjacent.

This isn't an option. Instead, after dissolving, you can use multipart to singleparts to split non-adjacent features apart. Note that attributes will be lost for some of the features. You can recover the missing attributes as follows:

After Dissolving the layer,

  1. Use this expression in the select by expression tool to select all multipart features in the Dissolve layer:

     regexp_match( geom_to_wkt( $geometry ), 'multi') > 0
  2. Run multipart to singleparts on Selected features only.
  3. Run join attributes by location to add attributes from the original layer to the singlepart features created in step 2.
  4. Use refactor fields to copy the joined field values into the original fields.
  5. Delete the multipart features from the Dissolve layer. These are the features you selected in step 1. If they're not selected anymore, simply repeat step 1. Make sure the Dissolve layer is active, then hit the Delete Selected button on the Digitizing toolbar.
  6. Copy and paste all the singlepart features from step 4 into the Dissolve layer.
  7. Save your edits to this layer. If it's a temporary layer, be sure to export it to a non-temporary layer format before exiting QGIS.

Related Questions

Sponsored Content

1 Answered Questions

0 Answered Questions

Python - Merge shapefile polygons based on criteria

1 Answered Questions

[SOLVED] Dissolve/merge polygons using ST_Union in CARTO

1 Answered Questions

[SOLVED] Polygons not merging in QGIS when dissolving?

1 Answered Questions

[SOLVED] Merging adjacent lines in QGIS

1 Answered Questions

[SOLVED] ArcGIS, detect adjacent polygons with different values

3 Answered Questions

[SOLVED] QGIS Dissolve: Limit output file format to .shp only

2 Answered Questions

[SOLVED] How to merge and dissolve adjacent polygons and sum up their attributes in QGIS?

  • 2016-02-03 11:11:11
  • delViento
  • 3899 View
  • 6 Score
  • 2 Answer
  • Tags:   qgis shapefile

2 Answered Questions

Sponsored Content