Find if given longitude, latitude pair lies in any of the polygon in MongoDB

坚强是说给别人听的谎言 提交于 2020-01-07 05:02:12

问题


As per my current understanding on MongoDB we search our collection by passing an array of [long, lat] pair arrays, to check if that document's [long, lat] pair exits inside the coordinates [[ [], [], [] ]].

i,e.,

db.SomeCollection.find({
    "location":{
        $geoWithin: {
            $geometry: { 
                type: "Polygon", 
                coordinates: [
                    [[1, 2], [1, 3], [1, 4]],
                    [[2, 5], [2, 6], [2, 7]]
                ] }
        }
    }
})

What i am looking for is that i need to feed the [long, lat] pair to a document containing fixed Polygonal Geofence arrays and conclude if the [long, lat] pair exists inside any of those [Polygonal Geofence arrays].

Is this possible in MongoDB? Is my question valid?

Please Note: I am novice to MongoDB as well as Geospatial Data hence forgive if the question contains wrong terminologies.


回答1:


After few days I am returning to my question as i found the answer to it.

The solution is posted in here by Jacob Ribnik.

hence implementing it to my problem it would be something like below:

db.Poly_Geofence_collection.find(
{
    "location": {
        $geoIntersects: {
            $geometry: {
                type: "Point",
                coordinates: [1,1]
            }
        }
    }
});

Where my Poly_Geofence_collection collection looks like:

{
    "_id": ObjectId("SomeRandom12ByteObjectId"),
    "location" : {
        "type" : "Polygon",
        "name" : "Location Name",
        "coordinates" : [
            [
                [0, 0], [1, 2], [1, 1], [2, 1], [0, 0]
            ]
        ]
    }
}


来源:https://stackoverflow.com/questions/46795397/find-if-given-longitude-latitude-pair-lies-in-any-of-the-polygon-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!