Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - $geoWithin query using $polygon or $geometry gives different result

Tags:

mongodb

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.

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
    ]
}})

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:

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 ] 
                ]                 
        } 
    } 
})
like image 708
Gbacc Avatar asked Jun 07 '16 10:06

Gbacc


1 Answers

Got the answer from the mongodb people : https://jira.mongodb.org/browse/SERVER-24549?focusedCommentId=1293398&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1293398

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

Now the problem is that i cannot use $polygon on $geoIntersect for exemple or with other geometry then a polygon.

like image 183
Gbacc Avatar answered Oct 18 '22 01:10

Gbacc