# [postgis-users] How to locate adjacent polygons?

pcreso at pcreso.com pcreso at pcreso.com
Tue Feb 15 23:45:45 PST 2011

```Would it not be simpler to use ST_Distance?

Unless I'm missing something, the tolerance is the minimum distance between two polygons, so:

select a.id as poly_a,
b.id as poly_b
from mypolys a, mypolys b
where a.id != b.id
and ST_Distance(a.geom, b.geom) <= <tolerance>;

to add some intelligence, if the tolerance is proportional to the max dim of a.geom, then a rule of thumb will be that tolerance is vaguely proportional to the sqrt(ST_area(a.geom))

so the sql becomes:
select a.id as poly_a, b.id as poly_b from mypolys a, mypolys b

where a.id != b.id

and ST_Distance(a.geom, b.geom) <= sqrt(ST_area(a.geom)) * <scale factor>;

A scale actor of 0.05 would roughly get you neighbouring polygons within about 5% as you mention. This does not account for polygon shape irregularity, but may get you something close enough.

If you do need to take irregularity into account, a similar rough estimate can be derived by ST_NumPoints(ST_Simplify(a.geom,0.0)). Generally the more vertices that are required to define a polygon, the more complex a shape (or irregular) it is. The where clause tolerance can derive from this value as well if that would help.

You could also

select a.id as poly_a,

b.id as poly_b

ST_Distance(a.geom,b.geom) as ab_dist
from mypolys a, mypolys b

where a.id != b.id

and ST_Distance(a.geom, b.geom) <= <tolerance>;

so in the output you also have stored a measure of just how adjacent (far apart) each pair of polygons returned is.

These will not measure exactly what you are asking, but may give a result that is close enough.

This assumes you are OK with the stored multipolygons as the shapes you are working with, and don't need to compare the constituent polygons

HTH,

Brent Wood

--- On Wed, 2/16/11, Stephen Woodbridge <woodbri at swoodbridge.com> wrote:

From: Stephen Woodbridge <woodbri at swoodbridge.com>
Subject: Re: [postgis-users] How to locate adjacent polygons?
To: postgis-users at postgis.refractions.net
Date: Wednesday, February 16, 2011, 6:12 PM

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
>

_______________________________________________
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/20110215/25c95b4d/attachment.html>
```