[SERVER-24549] $geoWithin query using $polygon or $geometry gives different result Created: 14/Jun/16  Updated: 14/Jul/16  Resolved: 14/Jun/16

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 3.2.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: gbac Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File geometry_data.png    
Operating System: ALL
Steps To Reproduce:

Add a geometry in the collection :

db.cust_5_abcd.insert({
"chps0" : "Texte d'une ligne",
"the_geom" : {
    "type" : "Point",
    "coordinates" : [ 
        1.032715, 
        40.380028
    ]
}})

With $geometry gives NO result:

db.cust_5_abcd.find(  { the_geom: 
 { $geoWithin: 
        { $geometry: 
            {       
                "type": "Polygon", 
                "coordinates": [ 
                    [ 
                        [ -16.237793, 40.162083 ], 
                        [ -16.237793, 51.835778 ], 
                        [ -13.776855, 51.835778 ], 
                        [ -13.776855, 41.426253 ], 
                        [ 14.765625, 41.426253 ], 
                        [ 14.765625, 40.162083 ], 
                        [ -16.237793, 40.162083 ] 
                ] 
                ]
            } 
        } 
    } 
})

With $polygon returns my Point :

db.cust_5_abcd.find( { the_geom: 
 { $geoWithin: 
        { $polygon:
                    [ 
                        [ -16.237793, 40.162083 ], 
                        [ -16.237793, 51.835778 ], 
                        [ -13.776855, 51.835778 ], 
                        [ -13.776855, 41.426253 ], 
                        [ 14.765625, 41.426253 ], 
                        [ 14.765625, 40.162083 ], 
                        [ -16.237793, 40.162083 ] 
                ]                 
        } 
    } 
})

Participants:

 Description   

Using MongoDB 3.2 i'm trying to use 2dsphere queries on a collection of Points.

Lets say i have à collection cust_5_abcd with a 2dsphere index on the_geom field.

Now i'm trying to query this Point using $geoWithin to get all data inside a specific Polygon. This is where i'm getting different result if i use $geometry with the GeoJSON definition , or with $polygon and the strict coordinates. Maybe something in the documentation is missing or something i misunderstood.

With $geometry gives NO result.
With $polygon returns my Point.



 Comments   
Comment by gbac [ 15/Jun/16 ]

Thanks for the answer.
I guess i understand how it works now.

However i'm kind of confused by this implementation logic.
For exemple, $geoNear have an option spherical to use either planar or spherical geometry. It seems this option is not available for other $geoWithin or $geoIntersect and $geoIntersect cannot use the $polygon syntax.

Any chance it will get implemented in futur version ?

Comment by Ramon Fernandez Marina [ 14/Jun/16 ]

gbac, if I'm reading the documentation correctly this is expected behavior.

When using $polygon a planar geometry is calculated, and the point falls inside the polygon you describe. But when you use GeoJSON a spherical geometry is used, and the point falls outside the geometry:

In the future I'd recommend you use this website (which is just a hosted version of this tool) to check your geometries.

Comment by gbac [ 14/Jun/16 ]

The polygon for the query :

"type": "Polygon",
"coordinates": [
[
[ -16.237793, 40.162083 ],
[ -16.237793, 51.835778 ],
[ -13.776855, 51.835778 ],
[ -13.776855, 41.426253 ],
[ 14.765625, 41.426253 ],
[ 14.765625, 40.162083 ],
[ -16.237793, 40.162083 ]
]
]

And my ticket on stackoverflow in case of : http://stackoverflow.com/questions/37676794/mongodb-geowithin-query-using-polygon-or-geometry-gives-different-result?noredirect=1

Generated at Thu Feb 08 04:06:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.