3

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

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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