[GIS] ST_Covers: Geography Implementation is not working, Geometry Implementation taking too long

postgispostgis-1.5postgresql

I have a query: (This query uses geographical implementation of ST_Covers function)

SELECT ST_Covers(ST_GeoGraphyFromText('MULTIPOLYGON(((179 -89,179 89,-179 89,-179 -89,179 -89)))'),ST_GeographyFromText('POINT(20 30)'));

When I run this query it should return true but it returns false. I don't know whats wrong with PostGIS (or with this query). When I change the geographical implementation with geometrical one, and rearrange the query to be like below:

SELECT ST_Covers(ST_ASTEXT(ST_GeoGraphyFromText('MULTIPOLYGON(((179 -89,179 89,-179 89,-179 -89,179 -89)))')),text('POINT(20 30)'));

This query works as it should, returning true:

I can use this query to be content but the problem is when database is large it takes too much time. Can someone tell me

  1. how to make query 1 to work right (as intended, returning true)? or

  2. how to make query 2 work fast with large tables?

(please do not suggest that i should remove ST_GeoGraphyFromText('MULTIPOLYGON(((179 -89,179 89,-179 89,-179 -89,179 -89) because it only represents geographical data that will be replaced by data from the column of table )

Other values which fail with query 1, are (5 5) (10 10) (-10 -10) (and many more)

Best Answer

This return true as it should

SELECT ST_Covers(ST_GeoGraphyFromText('MULTIPOLYGON(((179 1, 179 89,1 89, 1 1 ,179 1)))'),ST_GeoGraphyFromText('POINT(20 30)'));

So my best quess is that st_covers has bug or feature that it does not work when polygon fills all quartes of globe.

EDIT:

Played with this (PostGIS 2.0)

select 
  ST_Covers(
    ST_CollectionHomogenize(
      ST_Multi( -- this multi is probably useless here. it needed collectionhomogenize to get multiplogyn 
         ST_Split( -- Has to be geometry 
         ST_Split( -- Has to be geometry 
         ST_GeomFromText('MULTIPOLYGON(((179 -89,179 89,-179 89,-179 -89,179 -89)))')
    ,ST_GeomFromText('LINESTRING(0 -89, 0 89)'))
       , ST_GeomFromText('LINESTRING(-179 0, 179 0)')))
    )::geography
  , ST_GeographyFromText('POINT(20 30)')
  )

So it splits polygon to its quarters and casts geometry to geography and then compares point to polygon. It has its own problems It Uses Geometry to split because split doesn't support grography, but if you split it from meridian and equator it shouldn't be problem because lines are "straight" ( i may be wrong , but i'm fairly sure about it )

This solution is nice because you can convert all your polygons to multipolygons, or if you really want you can do it run time.

Related Question