gpt4 book ai didi

MongoDB : Check if a point is inside a stored polygon

转载 作者:IT老高 更新时间:2023-10-28 13:06:24 24 4
gpt4 key购买 nike

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

关于MongoDB : Check if a point is inside a stored polygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com