如何在 Mysql 中有效利用经度/纬度值?
感谢维基解密,在英国我们现在可以访问每个邮政编码的经度和纬度坐标 维基解密英国邮政编码 重要。请勿在公共应用程序中使用此数据 - 人们担心数据已被故意损坏,并且您可能会被视为违反版权法。尝试使用来自这样的开源网站的数据 Postcode UK site
现在,可以计算使用以下计算方法计算每个点之间的距离(最初来自精美书籍 Pro Mysql):
两点 (x1,y1) 和 (x2,y2) 之间的距离 d 可以通过以下公式计算(只要 x 值为纬度,y 值为以弧度表示的经度,r 为球体半径也就是 3956 英里):
d= acos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y2-y1)) * r
现在这足够好了,还是我应该使用新的 GIS数据类型和函数,如果是这样,如何将经度和纬度引用转换为 Point 数据类型?我意识到,由于地球不是一个完美的球体,所以我上面引用的距离计算并不完美;不过对于我的目的来说已经足够了。使用新的 GIS 功能是否会 a) 使距离计算更快 b) 使距离计算更准确?
Thanks to Wikileaks, here in the UK we can now access our longitude and latitude co-ordinates for each post code wikileaks postcodes uk IMPORTANT. Do not use this data in public applications - There's concern that the data has been deliberately corrupted and you could be held as being in breach of copyright law. Try using data from an open source site like this Postcode UK site
Now, it's possible to calculate distances between each point using the following calculation (originally from the fine book Pro Mysql):
The distance d between two points (x1,y1) and (x2,y2) can be calculated from the following equation (so long as x values are latitude and y values are longitude in radians, r is the radius of the sphere which is 3956 in miles):
d= acos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y2-y1)) * r
Now is this good enough, or should I use the new GIS data types and functions, and if so, how do I convert my longitude and latitude references to the Point data type? I realise that because the Earth is not a perfect sphere, so the distance calculation I quote above is not perfect; however it's good enough for my purposes. Would using the new GIS functionality a) make calculation of distance quicker b) make the distance calculation more exact?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重点关注 (a):
过去,我已经预先计算了部分,存储了纬度、经度、x 轴、y 轴和 zxai,其中 x、y 和 zxai 是z 定义为:
然后可以使用 SQL 松散地计算距离,如
(acos( xaxis * $xaxis + yaxis * $yaxis + zaxis * $zaxis ) * 6367.0 / 1.852)
(其中以a $ 以与上述相同的方式针对所讨论的起点进行预先计算)以这种方式进行预计算将相对昂贵的触发推送到一次性事件,并简化了查询。
To focus on (a):
In the past, I've precomputed parts, storing the lat, long, xaxis, yaxis and zxais, where the x, y & z are defined as:
The distance can then be calculated using SQL loosely like
(acos( xaxis * $xaxis + yaxis * $yaxis + zaxis * $zaxis ) * 6367.0 / 1.852)
(where those starting with a $ are precomputed for the start point in question in the same manner as above)Pre-computing in this manner pushes the relatively expensive trig to a one time event, and simplifies the query.
好吧,要回答你的“B”问题,因为你的方程假设是一个完美的球体,所以你会有点偏离(而且很可能,两点相距越远,情况就会变得越来越糟)。如果 GIS 考虑到了这一点,那么距离计算就会更加准确。
至于从纬度/经度转换为点,我可以发誓这是在 Google 地图 API。 (请参阅 GLatLng 参考。)
Well, to answer your "B" question, because your equation assumes a perfect sphere, you're going to be a little off (and it would, most likely, get progressively worse the farther the two points are from one another). If GIS accounts for this, then you would get the distance calculations to be more exact.
As for converting from lat/long to point, I could have sworn that this was provided within the Google Maps API. (See the GLatLng reference.)