Re: how to check if a point is contained in a polygon ?

From: Richard Huxton <dev(at)archonet(dot)com>
To: Julien Cigar <jcigar(at)ulb(dot)ac(dot)be>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: how to check if a point is contained in a polygon ?
Date: 2008-05-07 14:38:17
Message-ID: 4821BED9.60607@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Julien Cigar wrote:
> Hello,
>
> I'm busy to work on an application where the user can select (with
> precision) an area on a map (for example the contours of a lake) and I
> have to retrieve all the data (specimen observations) within this area.
>
> I have a list of coordinates pair [(lat1, long1), (lat2, long2), (lat3,
> long3), (..., ...)] which form a polygon. In my database every specimen
> has a pair of coordinates where it has been collected.

Are you familiar with the PostGIS project - if you're doing a lot of
this, it might be worthwhile looking into.

http://www.postgis.org/

> Is there an SQL function to check if a point is contained in a polygon
> shape (before I start to write my own) ?
>
> I tried something like :
>
> rodentia=> select point '(-8,25)' <@ polygon
> '((-3,10),(8,18),(-3,30),(-10,20))';

Works both ways around here:

SELECT point '(-8,25)' <@ polygon '((-3,10),(8,18),(-3,30),(-10,20))';

SELECT polygon '((-3,10),(8,18),(-3,30),(-10,20))' @> point '(-8,25)';

I'm running 8.3, but it seems to be in 8.2 too.

Try \do '<@' from psql to see what operators are available.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2008-05-07 15:12:29 Re: how to check if a point is contained in a polygon ?
Previous Message Woody Woodring 2008-05-07 14:36:26 Insert with pl/pgsql trigger