<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Colin,<br><br>Given your definition of adjacent, you are probably better off with a script using sqls to retrieve data/write result & the script to implement the logic, iterating through the polygons:<br>  <br>foreach polygon (a)<br>   foreach different polygon (b)<br>      foreach a.vertex<br>         foreach b.vertex<br>           get count of polygons intersected by a line between a.vertex and b.vertex<br>           if count > 2 then (a) and (b) are not adjacent<br>       next b.vertex<br>    next (a) vertex<br>    write result  <br>  next (b) polygon<br>next (a) polygon<br><br>This would define adjacency as having no
 intervening polygons between any two polygons, partial adjacency (where parts of two polygons are & other parts are separated) would require a slight change in the logic, so if any a.vertex & b.vertex have no intervening polygons, then (a) and (b) are adjacent. How you define & implement adjacency is up to you. As is the choice of scripting language :-)<br><br><br>HTH,<br><br>   Brent Wood<br><br><br><br>--- On <b>Thu, 2/17/11, Colin <i><colinflack@fastmail.co.uk></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Colin <colinflack@fastmail.co.uk><br>Subject: Re: [postgis-users] How to locate adjacent polygons?<br>To: postgis-users@postgis.refractions.net<br>Date: Thursday, February 17, 2011, 2:09 AM<br><br><div class="plainMail">sorry for double post - working from dodgy pda<br><br>Hi Steve / Brent<br><br>Thanks for replies<br><br>Steve: Yes I
 did look at buffer but I discounted it because a small<br>number of the polys are at distances well outside the mean and I didnt<br>see any easy way of including them.<br>Brents solution with st_distance and a tolerance factor might suffer<br>from the same problem<br><br>And in fact the definition of adjacent doesnt include distance - its<br>simply that no other polygon crosses some path between any 2<br><br>I also have the centroids calced and stored so in my spatial innocence i<br>devised a solution which almost works and uses ST_Crosses(ST_MakeLine<br>something like this:<br><br>select a.id, b.id, ST_Crosses(ST_MakeLine(a.centroid, b.centroid),<br>b.geom) as stcrosses <br>from a, b where a.id = nnn and a.id!=b.id order by stcrosses<br><br>I run this per id from program code. I then filter the return to give<br>adjacents<br><br>I'm not at my dev machine right now and I suspect thats not exacty<br>right(at all!) but what I have isnt affected by
 distance and almost<br>works - except occasionally for the very complex and where there are<br>larger gaps between polys.<br><br>so maybe a combination of these methods?<br><br>Or since the data wont change often, maybe I just plot them in qgis and<br>do it manually! ;-)<br><br>Colin<br><br><br>On 02/16/2011 05:12 AM, Stephen Woodbridge wrote:<br>> Colin,<br>> <br>> Did you look at buffer?<br>> <br>> Not tested but something like this might work where b.id are the <br>> adjacent id's to a.id with the distance of <tolerance>.<br>> <br>> select a.id, b.id from mypolys a, mypolys b<br>> where a.id != b.id<br>> and buffer(a.geom, <tolerance>) && b.geom<br>> and intersects(buffer(a.geom, <tolerance>), b.geom)<br>> <br>> -Steve<br>> <br>> On 2/15/2011 5:38 PM, Colin wrote:<br>>> Hi<br>>><br>>> I'm quite new to postgis and spatial databases.<br>>> competent with sql
 and db's<br>>><br>>> My problem: How to locate adjacent polygons.<br>>><br>>> I have around around 2k irregular polygons.<br>>> They've have been calculated as alpha / concave hulls from point sets.<br>>> They're saved into pg as multipolygons<br>>><br>>> id | description | geom<br>>><br>>> The polygons dont have any regularity with regard to location and<br>>> interaction<br>>> the majority are close to a neighbour, but not touching. Typically<br>>> within +/- 1 - 5% of polygon max dim<br>>> a small number slightly overlap 1 or more neighbours, usually to quite a<br>>> small extent<br>>> their irregular shape can include 'undercuts'<br>>><br>>><br>>> I need to identify the adjacent neighbours for each polygon<br>>><br>>> I looked at various methods that might allow me to do this but I cant<br>>> get a 100%
 solution<br>>><br>>> Can anyone suggest methods that might work<br>>><br>>> thanks<br>>><br>>><br>>> Colin<br><br><br>-- <br><a href="http://www.fastmail.fm" target="_blank">http://www.fastmail.fm</a> - IMAP accessible web-mail<br><br>_______________________________________________<br>postgis-users mailing list<br><a ymailto="mailto:postgis-users@postgis.refractions.net" href="/mc/compose?to=postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br><a href="http://postgis.refractions.net/mailman/listinfo/postgis-users" target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br></div></blockquote></td></tr></table>