MySQL GIS Lat/Lon 到 X/Y:哪个是哪个?
MySQL 中的 GIS 点是否应存储为 POINT($latitude $longitude)
或 POINT($longitude $latitude)
是否存在约定?
经度对应于笛卡尔地图上的 X,在视觉上更有意义,北指向上,但常见的说法是“纬度和经度”。
Is there a convention for whether GIS points in MySQL should be stored as POINT($latitude $longitude)
or POINT($longitude $latitude)
?
Having longitude correspond to X on a cartesian map would visually make more sense with north pointing up, but common parlance is to say "latitude and longitude."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 MySQL 中,您可能会使用 GeomFromText() 函数在空间字段中插入数据。此函数使用 WKT(众所周知的文本)格式来定义几何图形,并在POINT 情况下,它被定义为:
In MySQL you will probably use the
GeomFromText()
function to insert data in a spatial field. This function uses the WKT (Well-Known Text) format to define the geometries, and in the POINT case, it is defined as:对于在 MySQL 8+ 中使用 GPS 坐标,接受的答案是不正确的,并且它会遇到麻烦(尚未使用以前版本的 MySQL 进行测试)。
TL;DR; 在 MySQL 8+ 中使用“POINT($lat $long)”作为 WKT 字符串,但使用 POINT($long, $lat) 与 POINT() 函数。
完整答案:
在使用 SRID 4326(您应该用于 GPS 坐标系的坐标系)时使用 WKT 表示法作为“POINT($longitude $latitude)”会导致不正确的距离计算,即使在整个过程中始终使用应用程序。请继续阅读以了解详细信息。
例如,让我们考虑一下多伦多的加拿大国家电视塔和纽约的世界贸易中心一号大楼之间的直接距离约为。根据 Google 地图,549,18 公里。
GPS 坐标:
预期距离:549.18km
以下查询将产生正确的结果结果:
但是,如果您在 WKT 中首先提供经度(按照接受的答案中的建议),则会计算出错误的距离:
正如您所看到的 POINT($long $lat) WKT字符串方法不正确,大约有偏差。与 POINT($lat $long) 方法相比,距离为 51 公里,误差几乎为 10%。实际上,你走得越远,情况就会变得更糟。
解释:
这似乎是因为当 MySQL 在 GPS 坐标上下文中考虑 WKT 字符串时,它会将第一个参数视为纬度,将第二个参数视为经度。尝试运行以下查询:
请注意,当使用 POINT(x, y) 函数而不是 WKT 字符串时,情况恰恰相反!
例子:
The accepted answer is NOT CORRECT for working with GPS coordinates in MySQL 8+ and it will get into trouble (haven't tested it with previous version of MySQL).
TL;DR; Use 'POINT($lat $long)' as WKT string but POINT($long, $lat) with the POINT() function in MySQL 8+.
Full answer:
Using WKT notation as 'POINT($longitude $latitude)' while using SRID 4326 (the one you should use for GPS coordinates system) leads to incorrect distance calculations even if consistently used throughout the app. Read on for details.
For example, let's consider the direct distance between CN Tower in Toronto and One World Trade Center in NYC which is approx. 549,18km according to Google Maps.
GPS coordinates:
Expected distance: 549.18km
Following query yields the correct result:
However, if you provide longitude first in your WKT (as suggested in the accepted answer) you get a wrong distance calculated:
As you can see the POINT($long $lat) WKT string approach is incorrect and is off by approx. 51km compared to POINT($lat $long) approach which is almost 10% error. And it actually gets worse the farther you go.
Explanation:
It seems to happen because when MySQL considers a WKT string in the context of GPS coordinates it considers first argument as latitude and the second one as longitude. Try running the following query:
Beware though, that the opposite is true when using the POINT(x, y) function instead of a WKT string!
Example: