[postgis-users] Problem with ST_Within, Polygons and Multipolygons

Kevin Neufeld kneufeld at refractions.net
Tue May 5 19:28:21 PDT 2009


Jorge Arévalo wrote:
> ... If is useful, I tested the method "ST_isvalid" with the 
> multipolygon and returns "false". Maybe the multipolygon is not 
> closed? I loaded the data from a shapefile. Is it possible to create a 
> "non-valid" multipolygon? Does PostGIS accept this?

Ah, yes.  Most spatial predicates in PostGIS assume the input geometry 
is valid.  Suppose you had defined a POLYGON with a hole or inner ring 
outside of the exterior ring.  What is the area?  Does the question even 
make sense?  PostGIS allows invalid geometries in the database so users 
can make full use of the PostGIS toolset to do whatever they need (ie. 
breakdown a polygon to it's constituent linework and rebuild it back up 
again to a valid polygon)
>
> Ok, being even more specific. I'm working with data about Spain. I 
> have a HUGE multipolygon that represents "water" (this is, the coasts 
> around Spain and its islands). Then, the "holes" inside this 
> multypolygon have the shape of Spain, Balearic Islands and Canary 
> Islands. Of course, I have more multipolygons that represent 
> "forests", "airports", "cities", etc, that fit into these holes.
>
> Really, my problem is with some points that belong to an airport in an 
> island. Using "ST_Within" and "ST_Contains", the result is that these 
> points belong to the multipolygon "airport" and multipolygon "water" 
> at same time. Obviously, the island (and its airport) is surrounded by 
> water, but the airport's points shouldn't be part of the multopolygon 
> "water". And, as I said, when I apply "ST_isvalid" to the multipolygon 
> "water", returns false. Maybe is not closed?

Yeah, as mentioned before, a point that is not on the surface of a 
(multi)polygon (whether in a hole or completely outside) is not 
considered within the (multi)polygon.  It does sound like your ocean 
polygon has validity issues.

>
> Oh, btw, what's the difference between "ST_Within" and "Within". Does 
> "ST_Within" use index instead of geometry? Am I right?
No, not instead of.  Both use the actual geometry for testing within.  
ST_Within will also use the index to narrow down the candidate list first.
As you can see, the definition of ST_Within is just a simple SQL wrapper 
that first invokes the index.
postgis=# select prosrc from pg_proc where proname = 'st_within';
                prosrc                
---------------------------------------
 SELECT $1 && $2 AND _ST_Within($1,$2)
(1 row)

http://postgis.refractions.net/documentation/manual-svn/ST_Within.html

Cheers,
Kevin




More information about the postgis-users mailing list