Mongodb:检查点是否在存储的多边形内 [英] Mongodb : Check if a point is inside a stored polygon

查看:268
本文介绍了Mongodb:检查点是否在存储的多边形内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将一些代表国家边界的多边形与国家名称一起存储在数据库中。
现在我想要做的是检查一个点是在哪个国家。
例如,如果我给我自己的地理定位,我想得到我所在的国家。



有没有办法用mongodb做到这一点?也许与geoWithin?



谢谢

解决方案

位置数据如下:

  {loc:
{coordinates:[
[
[1.0,1.0],
[1.0,10.0],
[10.0,10.0],
[10.0,1.0],
[1.0,1.0]


type:Polygon
}
}

然后发送 $ geoIntersects 查询

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


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

解决方案

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

这篇关于Mongodb:检查点是否在存储的多边形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆