[Qgis-user] Merge adjacent polygons based on attribute condition?

chris hermansen clhermansen at gmail.com
Mon Aug 18 08:24:08 PDT 2025


Laurent and list,

On Mon, Aug 18, 2025 at 7:58 AM celati Laurent via QGIS-User <
qgis-user at lists.osgeo.org> wrote:

> Dear all,
> I work with Qgis, I have a polygon layer of 4,000 objects. A 'hierarchy'
> field provides information on the confidence level: 1, 2A, 2B, 3A, 3B.
> Of these 4,000 polygons, 3,000 are actually small polygons less than 500
> m².
>  For these small polygons, I'd like to be able to merge them with one of
> their neighboring polygons, ideally, the one with the closest "hierarchy"
> value.
>  If not possible to do that, with the one with the lowest "hierarchy"
> value.
>  Among these 3,000 polygons, some have no neighbors.
> They are isolated. For these, the idea would be to find a way to first
> select them and then delete them. Does anyone think they can help?
> How can I ideally do this with QGIS or PostGIS?
> Thanks so much.
>
> If I were trying to solve this problem, I would attempt it in PostgreSQL /
PostGIS.

This post on GIS StackExchange
https://gis.stackexchange.com/questions/464418/unifying-touching-polygon-within-polygon-geometry-in-postgis
describes a slightly simpler problem of just merging neighbour polygons.
Using it you will create the "potential clusters" ie all the groups of
neighbour polygons.

Then in your case you need to subdivide those potential clusters according
to the hierarchy value, maybe something like this (quoting the accepted
solution):

with clusters as (select st_clusterdbscan(geom, 0, 2) over()
cluster_id, geomfrom test.minigrid)
select st_union(geom) as geomfrom clusters where cluster_id is not
nullgroup by cluster_id,hierarchy_value -- <<< hierarchy value
included in the grouping
union
select geom from clusters where cluster_id is null

My doubt here is how you would handle your "the one with the lowest
hierarchy value" condition.  You might have to address this condition
before doing the ST_Union.

In particular this situation seems complicated to me:
- imagine three adjacent polygons, A, B and C
- A has confidence level 2A
- B has confidence level 2B
- C has confidence level 2C
- A touches the north side of B and C touches the south side, so they are
in the same "potential cluster"

Do A, B and C all join together in your ideal situation?  If so, is their
"combined confidence level" equal to 2C?  Or what happens?

Now imagine you have 100 polygons that touch in a potential cluster, with
the highest confidence level equal to 1 and the lowest equal to 4D.  How
should that look after processing?
-- 
Chris Hermansen · clhermansen "at" gmail "dot" com

C'est ma façon de parler.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20250818/67d56a75/attachment.htm>


More information about the QGIS-User mailing list