In QGIS I'm working with a shapefile including polygons which represent the footprint of a building or parts of it (if parts have different heights).

I would like to merge these parts but I have to include multiple conditions. For example, I would like to merge polygons if their "center" is close enough, if the height difference is below a threshold, or if the smallest size of the polygon is shorter than a value.

Is there a way to modify the Python script of the "Join" command of QGIS to add these conditions?

This is an example of a church divided into portions. The inner red square should NOT be merged as its height is significantly higher than the rest.


In response to comments:

  • Yes, this process should be done iteratively for all polygons (i.e. buildings) in the shapefile.
  • Yes, only polygons with at least one border in common should be considered for the join process. I'm currently looking at the "Join Selected Elements" command.
  • I never did cluster analyses but I'll give it a look.
  • 1
    Should this be done iteratively until there are no more building to merge?
    – Bera
    Commented Dec 3, 2021 at 17:12
  • You could create a new attribute for each condition with true/false and then identify those with all of these conditions = true. By the way: which "Join" command in QGIS do you mean? Only polygons that border (touch) each other should be merged, right?
    – Babel
    Commented Dec 3, 2021 at 17:54
  • I belive this problem is very complicated. Maybe some kind of cluster analysis could work
    – Bera
    Commented Dec 4, 2021 at 10:16
  • Thank you for all the comments. I tried adding more details in the question to be more specific. Commented Dec 4, 2021 at 15:09

2 Answers 2


You may try to do this in two steps. First a full spatial join to join all buildings with one edge in common. Then, you can easily remove the joint buildings that don't fulfil the rest of conditions that are based on attributes.

Would it work in your case?

  • Thank you, I decided to first try to have a "reference" shapefile with all necessary edits done manually. Then, I'll try to look for a way to train the join process so that it returns the same output. The issue is time, as I'm looking to do it for whole cities, with hundres of polygons. Commented Dec 7, 2021 at 10:37

IMHO it is a nice task for the Graphical Modeler in QGIS.

Step 1. Apply "Extract by expression"/"Select by expression" as many times as many conditions you have.

Step 2. "Dissolve" the result of each condition. On this stage, the "Multipart to singleparts" tool can be helpful.

Step 3. "Merge" all outputs from the Step 2 into one layer.

  • Thank you for the detailed answer! I'll surely give it a try! Commented Dec 7, 2021 at 10:38

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.