【发布时间】:2017-07-13 20:10:39
【问题描述】:
我一直在寻找有关如何动态计算距离并将距离返回到 MySQL/Mariadb 表中每一行的一组提供的 GPS 坐标的答案(使用地理空间扩展)。
我的查询如下:(我使用任意 GPS 坐标作为占位符,而不是为提供的坐标动态生成查询)。
SET @distance = 0;
select name, X(location), Y(location),
@distance = (((ACOS(SIN( 17.4681194 * PI() / 180)
* SIN(X(location) * PI() / 180) + COS(17.4681194 * PI() / 180)
* COS(X(location) * PI() / 180) * COS((78.495423 - Y(location))
* PI() / 180)) * 180 / PI()) * 60 * 1.1515)
* 1.609)
as distance from Points
问题在于,在我的输出中,所有距离都是 0 或 1。我仔细地为 Points 表设置了种子,所以我知道这些值是错误的。
我在 SQL 中而不是在 Python(或代码级别)中执行此操作的原因是因为我希望能够在正确完成距离并获取 3 个最近的位置后使用 ORDER BY 和 TOP从表中。
我的输出如下:
+----------------+--------------------+-------------------+----------+
| name | X(location) | Y(location) | distance |
+----------------+--------------------+-------------------+----------+
| randomsr1 | 17.4901059 | 78.4819368 | 0 |
| randomsr2 | 17.4895687 | 78.4830526 | 0 |
| randomsr3 | 17.45602 | 78.448551 | 0 |
| randomsr4 | 17.4681194 | 78.495423 | 1 |
| randomsra | 17.515589 | 78.4749738 | 0 |
| randomsrb | 17.4041657 | 78.4930975 | 0 |
| randomsrc | 17.40658541008292 | 78.47815974049286 | 0 |
| randomsrd | 17.468381171457448 | 78.46161846857831 | 0 |
| randomsr6 | 17.4649552904376 | 78.45982138853833 | 0 |
| location5 | 17.515562 | 78.474973 | 0 |
+----------------+--------------------+-------------------+----------+
【问题讨论】:
-
不错的输出。谢谢你。
-
不客气,但如果你指的是美化,那就是 MySQL/MariaDB。 :)
-
我知道这个问题真的很简单,但是谁能告诉我为什么我被否决了?无论如何,我都不是 MySQL 专家,这对我来说是一次学习经历。也许给我的信息是“在解决你的问题之前先睡一觉”?
标签: mysql mariadb geospatial