[SERVER-15609] GeoWithin query returns wrong result Created: 12/Oct/14  Updated: 10/Dec/14  Resolved: 14/Oct/14

Status: Closed
Project: Core Server
Component/s: Querying, Shell
Affects Version/s: 2.4.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Oleg Fetisov Assignee: Unassigned
Resolution: Done Votes: 0
Labels: 2dsphere, geoWithin, geojson
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 14.04.1 LTS


Attachments: PNG File polygon.png     PNG File polygon2.png    
Issue Links:
Related
is related to SERVER-12479 $geoWithin picks points outside of po... Closed
Operating System: Linux
Steps To Reproduce:

1. Create collection.

db.createCollection('newTest');

2. Ensure index (2dsphere).

db.newTest.ensureIndex({'point':'2dsphere'});

3. Add document into it.

db.newTest.insert({"point" : {"type" : "Point","coordinates" : [-80.087535,42.054246]}});

4. Query geoWithin.

db.newTest.find({"point":{"$geoWithin":{"$geometry":{"type":"Polygon","coordinates":[[[-80.267831,42.050312],[-80.267831,45.003652],[-73.362579,45.003652],[-73.362579,42.050312],[-80.267831,42.050312]]]}}}});

Participants:

 Description   

I've stuck with a problem of matching items inside a polygon (simple box). I can't figure out why the item which is inside the queried box is not resulting. So here what i have:

>db.testing.getIndexes();
{
    "0" : {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "test.testing",
        "name" : "_id_"
    },
    "1" : {
        "v" : 1,
        "key" : {
            "point" : "2dsphere"
        },
        "ns" : "test.testing",
        "name" : "2dsphere_index"
    }
}

Here is my documents (i've tested different formats):

>db.testing.find();
{
    "_id" : ObjectId("5439c9c61120c95f4c50a369"),
    "point" : {
        "lng" : -80.087535,
        "lat" : 42.054246
    }
}
{
    "_id" : ObjectId("5439cc6d1120c95f4c50a36a"),
    "point" : {
        "type" : "Point",
        "coordinates" : [
            -80.087535,
            42.054246
        ]
    }
} }}
And here is query:

>db.testing.find({"point": {"$geoWithin": {
"$geometry":

{ "type": "Polygon", "coordinates": [ [ [-80.267831,42.050312], [-80.267831,45.003652], [-73.362579,45.003652], [-73.362579,42.050312], [-80.267831,42.050312] ] ] }

}}})

 

But the problem is that it returns zero results!
If you not sure, that point is really in square, than copy this:

{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[-80.087535,42.054246]},{"type":"Polygon","coordinates":[[[-80.267831,42.050312],[-80.267831,45.003652],[-73.362579,45.003652],[-73.362579,42.050312],[-80.267831,42.050312]]]}]}

and check it here.
I'm confused, could somebody help me with this please?
Also, those points is got found when we decrease polygon area to:

[[-80.159937,42.050312],[-80.05204,42.050312],[-80.05204,42.09646],[-80.159937,42.09646],[-80.159937,42.050312]]

If it's needed i could provide near 100 more of such points.
Thanks in advance!



 Comments   
Comment by Thomas Rueckstiess [ 15/Oct/14 ]

Hi Oleg,

Here is some more background on the math involved: http://www.movable-type.co.uk/scripts/latlong.html

I hope that helps. If you intended to work on a flat Euclidian geometry, then you need to use a "2d" index instead of "2dsphere". The latter considers the spherical surface for geo queries.

Thomas

Comment by Oleg Fetisov [ 14/Oct/14 ]

Thomas, thanks for the response! But I still can't get it. Could you please visualize also rectangle in which point got founded? Cause my algorithm is braking big region apart and in smaller regions( which are part of big region) new points got found.
I can't imagine how point with latitude 42.054 could not be between 42.050 and 45.00.
I'm showing my vision of latitude line curvature, please correct me where i did mistake. Explanation image

Comment by Thomas Rueckstiess [ 14/Oct/14 ]

Hi Oleg,

The tool at geojsonlint.com unfortunately does not consider the curvature of the earth and instead just draws straight lines between the polygon coordinates.

I've visualized the polygon in your example but considering geodesics rather than straight lines, and if zoomed in close enough, you can see that the point is actually not inside the polygon on a spherical surface.

overview:

zoomed in:

I suspect the other points that are not being returned are similarly at the edge of the polygon so that they are no longer inside with spherical coordinates.

Regards,
Thomas

Comment by Oleg Fetisov [ 13/Oct/14 ]

Here is a file with around 700 points that also must be in that region:
https://drive.google.com/file/d/0By1thKMatgVybUY4UUk2YW05bWc/view?usp=sharing

Generated at Thu Feb 08 03:38:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.