Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...