[postgis-users] How to locate adjacent polygons?

pcreso at pcreso.com pcreso at pcreso.com
Wed Feb 16 10:13:57 PST 2011


Hi Colin,

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:
  
foreach polygon (a)
   foreach different polygon (b)
      foreach a.vertex
         foreach b.vertex
           get count of polygons intersected by a line between a.vertex and b.vertex
           if count > 2 then (a) and (b) are not adjacent
       next b.vertex
    next (a) vertex
    write result  
  next (b) polygon
next (a) polygon

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 :-)


HTH,

   Brent Wood



--- On Thu, 2/17/11, Colin <colinflack at fastmail.co.uk> wrote:

From: Colin <colinflack at fastmail.co.uk>
Subject: Re: [postgis-users] How to locate adjacent polygons?
To: postgis-users at postgis.refractions.net
Date: Thursday, February 17, 2011, 2:09 AM

sorry for double post - working from dodgy pda

Hi Steve / Brent

Thanks for replies

Steve: Yes I did look at buffer but I discounted it because a small
number of the polys are at distances well outside the mean and I didnt
see any easy way of including them.
Brents solution with st_distance and a tolerance factor might suffer
from the same problem

And in fact the definition of adjacent doesnt include distance - its
simply that no other polygon crosses some path between any 2

I also have the centroids calced and stored so in my spatial innocence i
devised a solution which almost works and uses ST_Crosses(ST_MakeLine
something like this:

select a.id, b.id, ST_Crosses(ST_MakeLine(a.centroid, b.centroid),
b.geom) as stcrosses 
from a, b where a.id = nnn and a.id!=b.id order by stcrosses

I run this per id from program code. I then filter the return to give
adjacents

I'm not at my dev machine right now and I suspect thats not exacty
right(at all!) but what I have isnt affected by distance and almost
works - except occasionally for the very complex and where there are
larger gaps between polys.

so maybe a combination of these methods?

Or since the data wont change often, maybe I just plot them in qgis and
do it manually! ;-)

Colin


On 02/16/2011 05:12 AM, Stephen Woodbridge wrote:
> Colin,
> 
> Did you look at buffer?
> 
> Not tested but something like this might work where b.id are the 
> adjacent id's to a.id with the distance of <tolerance>.
> 
> select a.id, b.id from mypolys a, mypolys b
> where a.id != b.id
> and buffer(a.geom, <tolerance>) && b.geom
> and intersects(buffer(a.geom, <tolerance>), b.geom)
> 
> -Steve
> 
> On 2/15/2011 5:38 PM, Colin wrote:
>> Hi
>>
>> I'm quite new to postgis and spatial databases.
>> competent with sql and db's
>>
>> My problem: How to locate adjacent polygons.
>>
>> I have around around 2k irregular polygons.
>> They've have been calculated as alpha / concave hulls from point sets.
>> They're saved into pg as multipolygons
>>
>> id | description | geom
>>
>> The polygons dont have any regularity with regard to location and
>> interaction
>> the majority are close to a neighbour, but not touching. Typically
>> within +/- 1 - 5% of polygon max dim
>> a small number slightly overlap 1 or more neighbours, usually to quite a
>> small extent
>> their irregular shape can include 'undercuts'
>>
>>
>> I need to identify the adjacent neighbours for each polygon
>>
>> I looked at various methods that might allow me to do this but I cant
>> get a 100% solution
>>
>> Can anyone suggest methods that might work
>>
>> thanks
>>
>>
>> Colin


-- 
http://www.fastmail.fm - IMAP accessible web-mail

_______________________________________________
postgis-users mailing list
postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20110216/5b5f868c/attachment.html>


More information about the postgis-users mailing list