2

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?
Thanks in advance!

Update:
Also, those points is got found when we decrease polygon area, for example, 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 like 100 of such points. For example one more strange point:

{"type":"Point","coordinates":[-76.537071,42.058731]}

Update:
Here is a file with points collection dump (around 700 points).

ofetisov
  • 44
  • 5
  • How about remove the last repeated coordinate array because I remember in mongodb it will automatically link the first And last coordinate node. – Andrew Carl Oct 12 '14 at 01:45
  • Nope, it causes an error: `"$err" : "can't parse query (2dsphere): ...".` Look at Alan's comment [here](http://stackoverflow.com/questions/19981372/howto-get-any-item-point-linestring-polygon-within-a-bounding-box-in-mongodb?rq=1#comment30190415_20181336) – ofetisov Oct 12 '14 at 01:51
  • That is really odd. I tested another point that is more obviously inside, db.testing.insert({"point":{"type":"Point", "coordinates":[-76,43]}}), and this was returned by the query. However, I also tested your original point and polygon in Postgis, using ST_Contains and, of course, it returns true. Sorry, I have no answer, just confirm what you have found and no idea why. – John Powell Oct 12 '14 at 06:03
  • If you could provide more sample points that fail, and shouldn't, it would be useful. – John Powell Oct 13 '14 at 09:34
  • I've added file with dump at the end of the post. – ofetisov Oct 13 '14 at 13:48
  • I confirm this on MongoDB 2.6.5 as well. Could you [file a SERVER ticket](https://jira.mongodb.org/browse/SERVER) on the MongoDB JIRA and link to it from here? Thanks! – wdberkeley Oct 13 '14 at 15:16
  • I've already done it yesterday: https://jira.mongodb.org/browse/SERVER-15609 – ofetisov Oct 13 '14 at 15:45

1 Answers1

0

MongoDB (2d sphere index) uses Geodesic to build polygons, which means the shortest line between two points.

This is how it looks on sphere:
Explanation on sphere

And this is how it looks on plain:
Explanation on plain

Zoomed in image shows that point really not in square: Explanation on plain zoomed in

ofetisov
  • 44
  • 5
  • In my case (polygon = box) I've used simple comparison query: box.lat1 <= point.lat <= box.lat2 and box.lng1 <= point.lng <= box.lng2 – ofetisov Aug 09 '15 at 16:32