Mongodb : Check if a point is inside a stored polygon

房东的猫 提交于 2019-11-29 11:22:06

问题


I'm new to the mongodb geolocation features.

I stored some polygons that represent the country borders in a database along with the country name. Now what i would like to do is checking which country a point is in. For example if i give my own geolocation i would like to get the country where i am.

Is there a way to do it with mongodb? Maybe with geoWithin?

Thank you


回答1:


You must store your location data like this schema:

{"loc":
     {"coordinates":[
       [
         [1.0,1.0],
         [1.0,10.0],
         [10.0,10.0],
         [10.0,1.0],
         [1.0,1.0]
       ]
      ],
     "type":"Polygon"
   }
}

and then send $geoIntersects queries

db.polygons.find({"loc":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":[x, y]}}}}



回答2:


You have to use $geoIntersects for that.

db.features.find({mystoredpolygon:{$geoIntersects:{$geometry:{type:'Point', coordinates:[22,38]}}}})



回答3:


Yes, you can use $geoWithin for the check, provided you define the countries as polygons using GeoJSON. For that, try this GitHub repo.



来源:https://stackoverflow.com/questions/15866490/mongodb-check-if-a-point-is-inside-a-stored-polygon

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