给定用户的 lat lng,如何从数千个 lat lng 的数据库中找到最近的 lat lng?
我在 MySQL 中有数千个传感器的位置数据。我想识别最接近用户位置的传感器并显示该特定传感器的数据。所有位置数据均以经纬度形式提供。
据我所知,一种方法是使用半正弦公式查找原点与所有传感器之间的位移,并选择距离最短的一个。这里的问题是有数以万计的传感器。
有什么建议/线索吗?
I have data of locations of thousands of sensors in MySQL. I want to identify the sensor closest to the user's location and show that specific sensor's data. All the location data is available as lat lng.
I understand that one approach can be to find displacements between the origin and all the sensors using Haversine formula and select the one with the shortest distance. The problem here is that there are tens of thousands of sensors.
Any suggestions/leads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
空间索引允许高效查询任何特定距离内的点。当然,问题是人们可能不知道特定情况下所需的搜索半径。不幸的是,较大的半径会导致查询效率低下,而较小的半径可能会导致根本不匹配。
一种可能的解决方案是增加半径进行搜索,直到搜索返回一些结果,然后找到其中最接近的结果。
本文介绍了 BigQuery 的此解决方案,需要对 MySQL 脚本方言进行一些调整:
https://mentin.medium.com/nearest-neighbor-using- bq-scripting-373241f5b2f5
Spatial index allows efficient query of points within any specific distance. The problem of course is one might not know the search radius needed in specific case. Unfortunately, a large radius causes inefficient queries, and a small radius might result in no match at all.
A possible solution is to search with increasing radius, until the search returns some results, and then find the closest result among those.
This article describes this solution for BigQuery, would require some adaptation for MySQL script dialect:
https://mentin.medium.com/nearest-neighbor-using-bq-scripting-373241f5b2f5
不是您正在寻找的 MySQL 答案,但 Postgresql 流行的 PostGIS 扩展有一个 内置 K 最近邻运算符类)。另请参阅其文档。效果很好!
另外,我知道这个 Go 库允许您在使用传感器位置构建四叉树后在内存中执行 KNN。
Not the MySQL answer you are looking for but Postgresql's popular PostGIS extension has an inbuilt K Nearest Neighbor operator class). Also, see its documentation. It works great!
Also, I am aware of this Go library that allows you to do KNN in memory after building a Quadtree with your sensor locations.
对于数千个,具有两个 2 列索引的简单边界框可能足够快。
为了获得更好的速度,请参阅
SPATIAL
索引。有关这两个解决方案以及两个更快的解决方案的详细信息,请参阅查找最近的
For only thousands, a simple bounding box with two 2-column indexes may be fast enough.
For better speed, see
SPATIAL
indexing.For details on those two solutions, plus two faster ones, see Find Nearest