1

In general this query functions fine. In my PHP application I tested it with a large number of points, and they all seemed to be true or false as appropriate (when I pasted data into two online WKT converters). But there's a significant number of false negatives. Here's the query using a point PostGIS thinks doesn't go in the rectangle even though I'm quite sure it should:

SELECT
    ST_Covers(geog_poly, geog_pt)
        As poly_covers_pt
            FROM (
                SELECT
                    ST_GeogFromText('POLYGON((-53 83,-53 42,-141 42,-141 83,-53 83))')
                        As geog_poly,
                    ST_GeogFromText('POINT (-100 48)')
                        As geog_pt
                )
                    AS result;

That returns poly_covers_pt as f, even though 42 < 48 < 83 and -141 < -100 < -53. Also, mapping the point and the region with online tools shows the region to contain all of North Dakota and the point to be in North Dakota.

By contrast, I can change the 48 to a 60 and this returns a t, as it should:

SELECT
    ST_Covers(geog_poly, geog_pt)
        As poly_covers_pt
    FROM (
        SELECT
            ST_GeogFromText('POLYGON((-53 83,-53 42,-141 42,-141 83,-53 83))')
                As geog_poly,
            ST_GeogFromText('POINT (-100 60)')
                As geog_pt
        )
            AS result;

What's going on?

1 Answer 1

2

Because you are using the geography type instead of the geometry type, the corners of your rectangle are connected by great circles, not lines along the parallels as you might be expecting. Your query works just fine if you cast your rectangle and point to ::geometry.

    SELECT
        ST_Covers(geog_poly::geometry, geog_pt::geometry)
            As poly_covers_pt
                FROM (
                    SELECT
                        ST_GeogFromText('POLYGON((-53 83,-53 42,-141 42,-141 83,-53 83))')
                            As geog_poly,
                        ST_GeogFromText('POINT (-100 48)')
                            As geog_pt
                    )
                        AS result;

Please note that this answer in no way addresses the right way perform your query -- it merely answers your question of "why".

2
  • Okay, I was going to request more elucidation, but re-reading it, re-thinking about it, and making some doodles of the earth helped it click. Thanks! Commented May 19, 2015 at 13:04
  • @ChrisMorrow It would have been difficult to be comprehensive, and you probably just educated yourself better than anyone else could have. Thinking and doodles are good.
    – Scro
    Commented May 19, 2015 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.