fgalan
fgalan

Reputation: 12312

Earth-wide polygon query in MongoDB doesn't return geo-located document

I have the following document in collection c in a MongoDB database (version 4.4.1):

> db.c.find()
{ "_id" : ObjectId("609adb1049c7a87a323c2c16"), "loc" : { "type" : "Point", "coordinates" : [ -4, 36 ] } }

There is a 2dsphere index on loc field in that collection:

> db.c.getIndexes()
[
    ...
    {
        "v" : 2,
        "key" : {
            "loc" : "2dsphere"
        },
        "name" : "loc_2dsphere",
        "2dsphereIndexVersion" : 3
    }
]

If I do a geo-within query with the polygon box (-50, -50) (50, 50) the document is retrieved:

> db.c.find({ "loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -50.0, -50.0 ], [ 50.0, -50.0 ], [ 50.0, 50.0 ], [ -50.0, 50.0 ], [ -50.0, -50.0 ] ] ] } } } })
{ "_id" : ObjectId("609adb1049c7a87a323c2c16"), "loc" : { "type" : "Point", "coordinates" : [ -4, 36 ] } }

So far so good.

However, if I enlarge the box to entire Earth, i.e. (-180, -90) (180, 90) surprisingly the document is not retrieved:

> db.c.find({ "loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -180.0, -90.0 ], [ 180.0, -90.0 ], [ 180.0, 90.0 ], [ -180.0, 90.0 ], [ -180.0, -90.0 ] ] ] } } } }).count()
0

Just for test if some "edget effect" could be happening, I have also tested with (-179, -89) (179, 89) but the result is the same:

> db.c.find({ "loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -179.0, -89.0 ], [ 179.0, -89.0 ], [ 179.0, 89.0 ], [ -179.0, 89.0 ], [ -179.0, -89.0 ] ] ] } } } }).count()
0

This is pretty weird, as if the document is retrieved with the (-50, -50) (50, 50) polygon, any larger polygon containing it, as (-180, -90) (180, 90) and (-179, -89) (179, 89), should also retrieve the document.

Any idea of what can be happening, please?

Thanks!

EDIT: I have found an interesting fact:

If I try (-90, 90) (90, 90), it works:

> db.c.find({ "loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -90.0, -90.0 ], [ 90.0, -90.0 ], [ 90.0, 90.0 ], [ -90.0, 90.0 ], [ -90.0, -90.0 ] ] ] } } } })
{ "_id" : ObjectId("609adb1049c7a87a323c2c16"), "loc" : { "type" : "Point", "coordinates" : [ -4, 36 ] } }

However, if I increase a little in the longitude dimension, i.e. (-90.5, 90) (90.5, 90) it fails:

> db.c.find({ "loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -90.5, -90.0 ], [ 90.5, -90.0 ], [ 90.5, 90.0 ], [ -90.5, 90.0 ], [ -90.5, -90.0 ] ] ] } } } }).count()
0

For me this makes no sense... does MongoDB has some limitation in longitude coordinate for polygon-based queries?

Upvotes: 0

Views: 128

Answers (1)

fgalan
fgalan

Reputation: 12312

As explained in this piece of MongoDB document the solution for queries greater than a single hemisphere is to specify the coordinate reference system with the csr field:

> db.c.find({ "loc" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -180, -90.0 ], [ 180, -90.0 ], [ 180, 90.0 ], [ -180, 90.0 ], [ -180, -90.0 ] ] ], crs: {type: "name", properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" } } } } } })
{ "_id" : ObjectId("609ba0ae49c7a87a323c2c17"), "loc" : { "type" : "Point", "coordinates" : [ -4, 36 ] } }

Upvotes: 1

Related Questions