Reputation: 501
I'm inserting a Point in a 2dsphere-indexed collection, and trying to find it within a Polygon:
c = db.foo;
c.ensureIndex({'value.geometry': '2dsphere'});
c.insert({value: {geometry: {type: "Point", coordinates: [0, 0]}}});
c.findOne({'value.geometry': {$geoWithin: {$geometry:
{"type":"Polygon","coordinates":[[[-90,-90],[90,-90],[90,90],[-90,90],[-90,-90]]]}}}})
// Point is found
However, when I do the same with a Polygon whose width exceeds 180°, the Point is not found:
c = db.foo;
c.ensureIndex({'value.geometry': '2dsphere'});
c.insert({value: {geometry: {type: "Point", coordinates: [0, 0]}}});
c.findOne({'value.geometry': {$geoWithin: {$geometry:
{"type":"Polygon","coordinates":[[[-90.1,-90],[90.1,-90],[90.1,90],[-90.1,90],[-90.1,-90]]]}}}})
// no result -- why?
I could not find any information on this in the MongoDB manual. Why the limit?
Upvotes: 2
Views: 711
Reputation: 501
After experimenting with this, I came to the conclusion the drmirrors solution does not lead to the desired results: you would have to insert lots of points not just around the boundaries of the box, but also the inside of the box to really find the intersecting shapes.
As for whether this is intended behavior, the MongoDB docs actually state: "Any geometry specified with GeoJSON to $geoIntersects queries, must fit within a single hemisphere. MongoDB interprets geometries larger than half of the sphere as queries for the smaller of the complementary geometries."
http://docs.mongodb.org/manual/reference/operator/geoIntersects/#op._S_geoIntersects
I resorted to disabling the filtering altogether for queries that include more than one hemisphere.
Upvotes: 0
Reputation: 3760
I guess that if your polygon exceeds 180 degrees of longitude, it might "close" the other way round the earth. So point (0,0) is actually not inside your polygon anymore. Point (180,0) probably is. You could probably create the polygon you wanted by adding a few more points on the "front" part of the globe, like (0,-90) and (0,90).
Upvotes: 2