<div dir="ltr"><div class="gmail_default" style="font-family:monospace">Assuming (1) your problem is not trivial (you have more then one "outer most" polygon) and (2) there can be only one type of spatial relationship (polygons are properly contained <a href="https://postgis.net/docs/ST_ContainsProperly.html">https://postgis.net/docs/ST_ContainsProperly.html</a>) below is my take on this:</div><div class="gmail_default" style="font-family:monospace">select distinct on (<a href="http://t1.id">t1.id</a>) <a href="http://t1.id">t1.id</a>,<a href="http://t2.id">t2.id</a><br>from t t1,t t2<br>where ST_Area(t1.geom)>ST_Area(t2.geom)<br> and ST_Intersects(t1.geom,t2.geom)<br>order by <a href="http://t1.id">t1.id</a>,ST_Area(t2.geom) desc;</div><div style="font-family:monospace" class="gmail_default">Enclose this in CTE and then just select "outer" polygons based on left column and "inner" based on right column.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">wt., 30 lis 2021 o 07:30 <<a href="mailto:paul.malm@lfv.se">paul.malm@lfv.se</a>> napisał(a):<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 lang="SV">
<div class="gmail-m_8546879809557756944WordSection1">
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span lang="EN-US">Hi,<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I have a polygon layer where polygons are inside other polygons (could be polygons in polygons in polygons…).
<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I would like to separate them in 2 different layers, one layer with the outer most polygon and one layer with all polygons that has a surrounding polygon.
<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Is there a smart way to do this in SQL?<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Thanks,<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Paul<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">    </span>
<span><img style="width: 1.375in; height: 1.1145in;" id="gmail-m_8546879809557756944Bild_x0020_1" src="cid:17d6fa9a11e4ce8e91" alt="1_LFV_svensk_96" width="132" height="107"><u></u><u></u></span></p>
<table style="width:630pt;border-collapse:collapse" width="0" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr style="height:36.85pt">
<td style="width:315pt;padding:0cm;height:36.85pt" width="420" valign="top">
<p class="MsoNormal" style="margin-top:2pt;line-height:10pt">
<span style="font-size:9pt;color:rgb(51,51,51)">  </span><span style="font-size:9pt">Paul Malm<span style="color:rgb(51,51,51)"><u></u><u></u></span></span></p>
<p class="MsoNormal" style="margin-top:2pt;line-height:10pt">
<span style="font-size:9pt;color:rgb(51,51,51)"><u></u> <u></u></span></p>
<p class="MsoNormal" style="margin-top:2pt;line-height:10pt">
<span style="font-size:9pt;color:rgb(51,51,51)">  Operations / AIM Flyginfo SE</span><span style="font-size:9pt;color:rgb(51,51,51)"><u></u><u></u></span></p>
</td>
<td style="width:315pt;padding:0cm 0cm 0cm 2.25pt;height:36.85pt" width="420" valign="top">
<p class="MsoNormal" style="margin-top:2pt;line-height:10pt">
<span style="font-size:9pt;color:rgb(51,51,51)"><u></u> <u></u></span></p>
</td>
</tr>
<tr style="height:24.1pt">
<td style="width:315pt;padding:0cm;height:24.1pt" width="420" valign="top">
<p class="MsoNormal" style="margin-bottom:12pt;line-height:9pt">
<span style="font-size:9pt;color:rgb(51,51,51)">  Direkt  08-797 70 23  Mobil 07</span><span style="font-size:9pt">0<span style="color:rgb(51,51,51)">-860 11 15<br>
  <a href="mailto:paul.malm@lfv.se" target="_blank">paul.malm@lfv.se</a>  <u></u><u></u></span></span></p>
<p class="MsoNormal" style="line-height:9pt"><span style="font-size:9pt;color:rgb(51,51,51)">   Besöks- och postadress<br>
   LFV Flyginfo SE<u></u><u></u></span></p>
<p class="MsoNormal" style="line-height:9pt"><span style="font-size:9pt;color:rgb(51,51,51)">   Af Pontins väg 6
<u></u><u></u></span></p>
<p class="MsoNormal" style="line-height:9pt"><span style="font-size:9pt;color:rgb(51,51,51)">   115 21 STOCKHOLM<u></u><u></u></span></p>
</td>
<td style="width:315pt;padding:0cm 0cm 0cm 2.25pt;height:24.1pt" width="420" valign="top">
<p class="MsoNormal" style="line-height:9pt"><span style="font-size:9pt;color:rgb(51,51,51)"><u></u> <u></u></span></p>
</td>
</tr>
<tr>
<td style="width:315pt;padding:0cm" width="420" valign="top">
<p class="MsoNormal" style="margin-bottom:12pt;line-height:9pt">
<span style="font-size:9pt;color:rgb(51,51,51)">  <u></u><u></u></span></p>
</td>
<td style="width:315pt;padding:0cm 0cm 0cm 2.25pt" width="420" valign="top">
<p class="MsoNormal" style="margin-top:2pt;line-height:9pt">
<span style="font-size:9pt;color:rgb(51,51,51)"><u></u> <u></u></span></p>
</td>
</tr>
<tr style="height:3.7pt">
<td style="width:315pt;padding:0cm;height:3.7pt" width="420" valign="top">
<p class="MsoNormal" style="margin-top:6pt;line-height:9pt">
<span style="font-size:9pt;color:rgb(0,204,0)">   Tänk på miljön innan du skriver ut detta e-postmeddelande.</span><span style="font-size:9pt;color:rgb(0,204,0)"><u></u><u></u></span></p>
</td>
<td style="width:315pt;padding:0cm 0cm 0cm 2.25pt;height:3.7pt" width="420" valign="top">
<p class="MsoNormal" style="margin-top:6pt;line-height:9pt">
<span style="font-size:9pt;color:rgb(0,204,0)"><u></u> <u></u></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>

_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a><br>
</blockquote></div>