<div dir="ltr"><div dir="ltr">Laurent and list,</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Aug 18, 2025 at 7:58 AM celati Laurent via QGIS-User <<a href="mailto:qgis-user@lists.osgeo.org">qgis-user@lists.osgeo.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Dear all,<br>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.<br>Of these 4,000 polygons, 3,000 are actually small polygons less than 500 m².</div><div> 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.</div><div> If not possible to do that, with the one with the lowest "hierarchy" value.</div><div> Among these 3,000 polygons, some have no neighbors. </div><div>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? </div><div>How can I ideally do this with QGIS or PostGIS?</div><div>Thanks so much.</div><div><br></div></div></blockquote><div>If I were trying to solve this problem, I would attempt it in PostgreSQL / PostGIS.</div><div><br></div><div>This post on GIS StackExchange <a href="https://gis.stackexchange.com/questions/464418/unifying-touching-polygon-within-polygon-geometry-in-postgis">https://gis.stackexchange.com/questions/464418/unifying-touching-polygon-within-polygon-geometry-in-postgis</a> 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.</div><div><br></div><div>Then in your case you need to subdivide those potential clusters according to the hierarchy value, maybe something like this (quoting the accepted solution):</div><div><br></div><div><pre class="gmail-lang-sql gmail-s-code-block"><code class="gmail-hljs gmail-language-sql"><span class="gmail-hljs-keyword">with</span> clusters <span class="gmail-hljs-keyword">as</span> (
<span class="gmail-hljs-keyword">select</span> st_clusterdbscan(geom, <span class="gmail-hljs-number">0</span>, <span class="gmail-hljs-number">2</span>) <span class="gmail-hljs-keyword">over</span>() cluster_id, geom
<span class="gmail-hljs-keyword">from</span> test.minigrid)

<span class="gmail-hljs-keyword">select</span> st_union(geom) <span class="gmail-hljs-keyword">as</span> geom
<span class="gmail-hljs-keyword">from</span> clusters <span class="gmail-hljs-keyword">where</span> cluster_id <span class="gmail-hljs-keyword">is</span> <span class="gmail-hljs-keyword">not</span> <span class="gmail-hljs-keyword">null</span>
<span class="gmail-hljs-keyword">group</span> <span class="gmail-hljs-keyword">by</span> cluster_id,hierarchy_value -- <<< hierarchy value included in the grouping

<span class="gmail-hljs-keyword">union</span>

<span class="gmail-hljs-keyword">select</span> geom <span class="gmail-hljs-keyword">from</span> clusters <span class="gmail-hljs-keyword">where</span> cluster_id <span class="gmail-hljs-keyword">is</span> <span class="gmail-hljs-keyword">null</span>
</code></pre>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.</div><div><br></div><div>In particular this situation seems complicated to me:</div><div>- imagine three adjacent polygons, A, B and C</div><div>- A has confidence level 2A</div><div>- B has confidence level 2B</div><div>- C has confidence level 2C</div><div>- A touches the north side of B and C touches the south side, so they are in the same "potential cluster"</div><div><br></div><div>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?</div><div><br></div><div>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?</div></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr">Chris Hermansen · clhermansen "at" gmail "dot" com<br><br>C'est ma façon de parler.</div></div></div>