MongoDB How to find which polygon contains a specified Point?

一曲冷凌霜 提交于 2019-12-10 06:14:58

问题


I insert many polygons into MongoDB(2.4.8), and hope find the polygon a specified Point sits in. It seems a common question. But after reading all docs from google, I didn't get the result. So create this question.

e.g.

db.addr_poly.insert(
{ loc :
   { type : "Polygon" ,
     coordinates : [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
} })
db.addr_poly.insert(
{ loc :
   { type : "Polygon" ,
     coordinates : [ [ [ 0 , 0 ] , [ -3 , -6 ] , [ -6 , -1 ] , [ 0 , 0 ] ] ]
} })

Now how to find the polygon which contains Point(1,1)?

Anybody can help me? Thanks a lot!


回答1:


Use the $geoIntersects operator. It queries for all shapes which intersect the GeoJSON object you pass. When you pass a point to it, it should return all shapes which include that point. Keep in mind that the $geoIntersects operator only works for 2dsphere indexes, not for 2d indexes.




回答2:


As @philipp said, you should use the $geoIntesects operator. I was having the hardest time trying to figure out the query. I figured I would share what it looks and it might save someone the trouble later.

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


来源:https://stackoverflow.com/questions/20161180/mongodb-how-to-find-which-polygon-contains-a-specified-point

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