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)
and when i changes 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)'));
it works as it should, returning true:
I can use below query to be content but Problem is when database is too large it takes too much time
Please can someone tell me
how to make query 1 to work right (as intended, returning true), or
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 with which query 1 dont work are (5 5) (10 10) (-10 -10) and much more
The first query fails because you are using a
geographytype with something >180° wide. If it were something more realistic, such as'MULTIPOLYGON(((100 0,100 50,0 50,0 0,100 0)))', it will return TRUE.There is no direct way to find the maximum diameter of the exterior rings of the MultiPolygon geography types, but you can attempt to hunt these particular cases down with something like:
examine the ones that are > 180, and see if each of the parts are also > 180. If so, these should be regarded as invalid geographies.
The only reason why the second query returns TRUE is because ST_AsText converts to WKT, which is then re-interpreted back to WKB as
geometrytypes (and implicitly callsST_Covers(geometry, geometry), rather thanST_Covers(geography, geography)). This query is slow since it converts from WKB to WKT to WKB, with possible loss of precision in between conversions. A faster version of this is to cast the geography column to geometry using::geometry, e.g.:Geometry types use simple “flat earth” Cartesian logic for ST_Covers, which is why you see TRUE for what you expect. Geography types use a different “round earth” logic, which uses more complicated spherical logic, but is easy to see if you have a globe handy.