Hello,<br><br>Many thanks for your help Kevin. I can confim one thing: isvalid only returns the first error. Just today, I realized on the big amount of errors of my data, as you said. Indeed, I was reading the same link that you provided me about geometry validity, and I split my multipolygon into single polygons to check them. My conclusions are the same as yours: what a mess!<br>

<br>Oh, and thanks again for your response to my original question. It helped me a lot... Maybe is it time to ask for new data, without errors? If not, how should I "clean" my data? I tried with ST_Buffer(geom, 0.0) (I read in this link <a href="http://www.bostongis.com/postgis_extent_expand_buffer_distance.snippet">http://www.bostongis.com/postgis_extent_expand_buffer_distance.snippet</a> that is useful to repair data with self-interesections), but doesn't seem to work for me...<br>

<br>Regards<br>Jorge<br>
<br><br><div class="gmail_quote">On Thu, May 7, 2009 at 6:58 PM, Kevin Neufeld <span dir="ltr"><<a href="mailto:kneufeld@refractions.net">kneufeld@refractions.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

Hi Jorge,<br>
<br>
So, I had a quick look at your data ... I'm sorry to say that you've got a lot of problems with your ocean polygon.<br>
Paul can correct me since he did the work on isvalidreason, but I think st_isvalid only returns the first thing it finds wrong with your input geometry.<br>
<br>
I did this and found all kinds of things:<br>
<br>
-- Extract the POLYGONs from the MULTI<br>
CREATE TABLE water_polys AS<br>
SELECT (ST_Dump(the_geom)).geom AS the_geom<br>
FROM water;<br>
<br>
SELECT ST_IsValidReason(the_geom)<br>
FROM water_polys<br>
WHERE NOT ST_IsValid(the_geom);<br>
<br>
So, looking at the individual polygons (of which only the first error is again reported), I found over 66 self-intersections (figure 8's and so forth) and several whose boundaries loop back and touch itself.<br>
<br>
Also, in response to your original question about why a point on your airport also intersects the ocean polygon: it turns out that the island in question is not a hole in your ocean at all ... but a second polygon on top of your ocean polygon.  This is why your point query returns true - there are two surfaces (the island and the water) that intersect the point (which, by the way, also makes an invalid multipolygon).<br>


<br>
A while back I put together some isvalid/issimple geometry descriptions with soem pretty pictures:<br>
<a href="http://postgis.refractions.net/documentation/manual-svn/ch04.html#OGC_Validity" target="_blank">http://postgis.refractions.net/documentation/manual-svn/ch04.html#OGC_Validity</a><br>
<br>
You may want to have a look through it and clean up your dataset.  The invalidity will most certainly result in more unexpected behaviour.<br>
<br>
Cheers,<br>
Kevin<br>
<br>
<br>
Jorge Arévalo wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">
Hello,<br>
<br>
I have additional info. When running the method "isvalid", I get this warning message:<br>
<br>
NOTICE:  Self-intersection at or near point -2749.99 4.7955e+06<br>
<br>
What is exactly a "self-intersection"? May this "self-intersection" be the cause of my problem?<br>
<br>
Thanks<br>
Jorge<br>
<br>
<br>
<br></div>
2009/5/6 Jorge Arévalo <<a href="mailto:jorge.arevalo@gmail.com" target="_blank">jorge.arevalo@gmail.com</a> <mailto:<a href="mailto:jorge.arevalo@gmail.com" target="_blank">jorge.arevalo@gmail.com</a>>><div>

<div></div><div class="h5"><br>
<br>
    Hi,<br>
<br>
    Thanks for your response Kevin. I can provide some data:<br>
<br>
    - Screenshot (over geoserver) of the "water" multipolygon. You can<br>
    see the shape of Spain and the Balearic Islands, and a hole on the<br>
    left (Portugal). The multipolygon is open, isn't it? --><br>
    <a href="http://www.nebulared.com/tmp_geo/water_multipolygon.jpg" target="_blank">http://www.nebulared.com/tmp_geo/water_multipolygon.jpg</a><br>
<br>
    - Zoom over Menorca (one of the Balearic Islands). As you can see,<br>
    the point tested is inside the "airports" multipolygon, but<br>
    ST_Within and ST_Contains returns that the point is inside both,<br>
    "water" and "airports" multipolygons --><br>
    <a href="http://www.nebulared.com/tmp_geo/zoom_in_water_multipolygon.jpg" target="_blank">http://www.nebulared.com/tmp_geo/zoom_in_water_multipolygon.jpg</a><br>
<br>
    - The "ST_AsText" data for the "airport" multipolygon --><br>
    <a href="http://www.nebulared.com/tmp_geo/airports_multipolygon.txt" target="_blank">http://www.nebulared.com/tmp_geo/airports_multipolygon.txt</a><br>
<br>
    - The "ST_AsText" data for the "water" multipolygon (rar file, 2.5<br>
    MB of plain text...) --><br>
    <a href="http://www.nebulared.com/tmp_geo/water_multipolygon.rar" target="_blank">http://www.nebulared.com/tmp_geo/water_multipolygon.rar</a><br>
<br>
    So, how can I check the validity of my multipolygons? Do someone<br>
    could help me with this?<br>
<br>
    Now, the query. My context is the next:<br>
    - I have a table with polygons that I want to check (to say if they<br>
    are in an airport, in a forest, in water, etc). We can call this<br>
    table T1. I use the centroid of the polygons to check, not the<br>
    entire polygon.<br>
    - In other different table, that we'll call T2, I have all the<br>
    multipolygons ("water", "airports", etc). The name of the field that<br>
    says if the multipolygon is "water" or "airport" or anything is<br>
    "clutter"<br>
<br>
    With this code, I pretend to loop over T1, checking if the centroid<br>
    of its polygons are inside of one (and ONLY one) of the<br>
    multipolygons of T2.  (Is part of a procedure)<br>
<br>
    (...)<br>
    BEGIN<br>
      FOR s IN SELECT * FROM T1 LOOP<br>
            select clutter from T2 where<br>
    ST_Within(ST_Centroid(T1.wkb_geometry), T2.wkb_geometry))<br>
            (...)<br>
      END LOOP;<br>
      RETURN;<br>
    END;<br>
<br>
    Most of the times, the select inside the loop, returns more than one<br>
    result ("water" and other one, normally). That's my problem<br>
<br>
    I have another version of the query, with Java. Again, I loop over<br>
    the table T1, and check the centroid of its polygons with all the<br>
    multipolygons (water, airports...) that are in T2. Then, my query,<br>
    inside the loop, is:<br>
<br>
    SELECT clutter FROM T2 WHERE ST_Within(?, T2.wkb_geometry)<br>
<br>
    The '?' is replaced with the centroid of the polygon in T1, of<br>
    course. Basically, is the same idea of the previous SQL code, but<br>
    with Java.<br>
<br>
    Apart from this, other problem is that ST_Within takes a long time.<br>
    1 sec per query. And I have like 2 millions of polygons (queries) to<br>
    check...<br>
<br>
    My multipolygons' table (T2) has only 29 rows (small table with<br>
    large geometries), and I found this<br>
    <a href="http://postgis.refractions.net/documentation/manual-1.3/ch05.html" target="_blank">http://postgis.refractions.net/documentation/manual-1.3/ch05.html</a>.<br>
    Seems to be good for me. But using "SET enable_seqscan TO off", the<br>
    queries take the same time (~ 1 sec). Maybe the other approach<br>
    (create an additional column that "caches" the bbox, and matching<br>
    against this) could help me. Any ideas on how to create this bbox<br>
    with my huge multipolygons?<br>
<br>
<br>
    Thanks in advance<br>
<br>
    Regards<br>
    Jorge<br>
<br>
<br>
    On Wed, May 6, 2009 at 4:28 AM, Kevin Neufeld<br></div></div><div><div></div><div class="h5">
    <<a href="mailto:kneufeld@refractions.net" target="_blank">kneufeld@refractions.net</a> <mailto:<a href="mailto:kneufeld@refractions.net" target="_blank">kneufeld@refractions.net</a>>> wrote:<br>
<br>
        Jorge Arévalo wrote:<br>
<br>
            ... If is useful, I tested the method "ST_isvalid" with the<br>
            multipolygon and returns "false". Maybe the multipolygon is<br>
            not closed? I loaded the data from a shapefile. Is it<br>
            possible to create a "non-valid" multipolygon? Does PostGIS<br>
            accept this?<br>
<br>
<br>
        Ah, yes.  Most spatial predicates in PostGIS assume the input<br>
        geometry is valid.  Suppose you had defined a POLYGON with a<br>
        hole or inner ring outside of the exterior ring.  What is the<br>
        area?  Does the question even make sense?  PostGIS allows<br>
        invalid geometries in the database so users can make full use of<br>
        the PostGIS toolset to do whatever they need (ie. breakdown a<br>
        polygon to it's constituent linework and rebuild it back up<br>
        again to a valid polygon)<br>
<br>
<br>
            Ok, being even more specific. I'm working with data about<br>
            Spain. I have a HUGE multipolygon that represents "water"<br>
            (this is, the coasts around Spain and its islands). Then,<br>
            the "holes" inside this multypolygon have the shape of<br>
            Spain, Balearic Islands and Canary Islands. Of course, I<br>
            have more multipolygons that represent "forests",<br>
            "airports", "cities", etc, that fit into these holes.<br>
<br>
            Really, my problem is with some points that belong to an<br>
            airport in an island. Using "ST_Within" and "ST_Contains",<br>
            the result is that these points belong to the multipolygon<br>
            "airport" and multipolygon "water" at same time. Obviously,<br>
            the island (and its airport) is surrounded by water, but the<br>
            airport's points shouldn't be part of the multopolygon<br>
            "water". And, as I said, when I apply "ST_isvalid" to the<br>
            multipolygon "water", returns false. Maybe is not closed?<br>
<br>
<br>
        Yeah, as mentioned before, a point that is not on the surface of<br>
        a (multi)polygon (whether in a hole or completely outside) is<br>
        not considered within the (multi)polygon.  It does sound like<br>
        your ocean polygon has validity issues.<br>
<br>
<br>
<br>
            Oh, btw, what's the difference between "ST_Within" and<br>
            "Within". Does "ST_Within" use index instead of geometry? Am<br>
            I right?<br>
<br>
        No, not instead of.  Both use the actual geometry for testing<br>
        within.  ST_Within will also use the index to narrow down the<br>
        candidate list first.<br>
        As you can see, the definition of ST_Within is just a simple SQL<br>
        wrapper that first invokes the index.<br>
        postgis=# select prosrc from pg_proc where proname = 'st_within';<br>
                      prosrc                       ---------------------------------------<br>
        SELECT $1 && $2 AND _ST_Within($1,$2)<br>
        (1 row)<br>
<br>
        <a href="http://postgis.refractions.net/documentation/manual-svn/ST_Within.html" target="_blank">http://postgis.refractions.net/documentation/manual-svn/ST_Within.html</a><br>
<br>
        Cheers,<br>
        Kevin<br>
<br>
<br>
        _______________________________________________<br>
        postgis-users mailing list<br>
        <a href="mailto:postgis-users@postgis.refractions.net" target="_blank">postgis-users@postgis.refractions.net</a><br></div></div>
        <mailto:<a href="mailto:postgis-users@postgis.refractions.net" target="_blank">postgis-users@postgis.refractions.net</a>><div class="im"><br>
        <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users" target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>
<br>
<br>
<br>
<br></div>
------------------------------------------------------------------------<div class="im"><br>
<br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@postgis.refractions.net" target="_blank">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><div><div></div><div class="h5">
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@postgis.refractions.net" target="_blank">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></div></blockquote></div><br>