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

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

问题描述

我是 mongodb 地理定位功能的新手.

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.

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

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

谢谢

推荐答案

你必须像这样存储你的位置数据:

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

然后发送 $geoIntersects 查询

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

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

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