MongoDB $ geoIntersments does not find one fully containing row in the polygon, but finds another

I have a MongoDB database that has a collection called fooCollection . This collection contains documents containing geospatial data on the path of a bounded polygon. I am using the C # MongoDB driver in my application. I noticed that he does not find documents with specific spatial queries, although he works with most of them. I emptied the collection, with the exception of one offensive document, and I tried to find it by doing the queries directly.

My document is as follows:

 { "_id" : UUID("12345678-62d9-4024-86dc-123456789012"), "polygon" : { "type" : "Polygon", "coordinates" : [ [ [ 18.414846, -33.9699577 ], [ 18.414846, -26.0991189 ], [ 31.0330578, -26.0991189 ], [ 31.0330578, -33.9699577 ], [ 18.414846, -33.9699577 ] ] ] }, "foo": "bar" } 

I also have this index in this collection:

 [ 1, { "polygon" : "2dsphere" }, "polygon_2dsphere", "data.fooCollection", 3 ] 

The following query correctly returns this document:

 db.getCollection('fooCollection').find( { "polygon": { $geoIntersects: { $geometry: { type: "LineString", coordinates: [[24.7698287, -28.7353533],[28.0423, -26.19793]] }}}}) 

However, this request does not execute:

 db.getCollection('fooCollection').find( { "polygon": { $geoIntersects: { $geometry: { type: "LineString", coordinates: [[27.7706902, -26.1091189],[28.0423, -26.19793]] }}}}) 

If you build these three geometries, I cannot understand why you need to work, and not another.

  • Both lines lie entirely inside the polygon.
  • The working line is much longer
  • The working line has a bearing between 0 ° and 90 °, and the other between 90 ° and 180 °.

Can anyone explain this behavior?

EDIT: I also tested individual points instead of using LineStrings. The only hit is [24.7698287, -28.7353533] . I need LineStrings - the request should be a hit, even if only the edge intersects the polygon, and the points do not lie inside

Here you can see geojson or you can build three geometries yourself by inserting the following line into http://geojson.io/ :

 {"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[18.414846,-33.9699577],[18.414846,-26.0991189],[31.0330578,-26.0991189],[31.0330578,-33.9699577],[18.414846,-33.9699577]]]},{"type":"LineString","coordinates":[[27.7706902,-26.1091189],[28.0423,-26.19793]]},{"type":"LineString","coordinates":[[24.7698287,-28.7353533],[28.0423,-26.19793]]}]} 
+8
c # mongodb mongodb-query geospatial geojson
source share
1 answer

Can anyone explain this behavior?

This is because the Earth is not flat, but rather spherical (curved). The surface of the Earth cannot be represented on a plane without distortion. Thus, this means that each map projection will somehow distort the Earth. See Also Projecting a Map

MongoDB 2dsphere index , supports queries that calculate geometry in the terrestrial sphere.

When you map the coordinate to http://geojson.io , it does not take into account the spherical nature of the Earth. It is shown that the shorter line LineString is inside the "square" polygon.

geojson.io

If you map GeoJSON geometry to https://geodndmap.mongodb.com , which takes into account the fact that there is a spherical distortion, you will see below:

geodndmap.mongodb.com

The shorter LineString is actually outside the "square" polygon. This is what MongoDB 2dsphere index / query is executing.

The effect of the distortion effect increases with increasing long / large areas.

Example:

 {"type":"GeometryCollection","geometries":[ {"type":"Polygon","coordinates":[[[18.414846,-33.9699577],[18.414846,-26.0991189],[31.0330578,-26.0991189],[31.0330578,-33.9699577],[18.414846,-33.9699577]]]}, {"type":"LineString","coordinates":[[24.7698287,-28.7353533],[88.0423,-26.19793]]}]} 

geojson.io

geodndmap.mongodb.com

PS: To use geodndmap.mongodb.com, drag GeoJSON onto the map either as a file or as text from an editing program.

+4
source share

All Articles