STD距离之间计算出的距离差异

STD距离之间计算出的距离差异

本文介绍了javascript函数和sql server STD距离之间计算出的距离差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DECLARE @orig geography = geography::Point(17, 78, 4326);
Select  @orig.STDistance(geography::Point(17.001, 78.00001, 4326))/1000

给予

110.674385214845

110.674385214845

function calculateDistance(lat1, lon1, lat2, lon2, units) {
        var R = 6371; // Radius of the earth in km
        var dLat = deg2rad(lat2 - lat1);  // deg2rad below
        var dLon = deg2rad(lon2 - lon1);
        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var d = R * c; // Distance in km
        return d;
}
function deg2rad(deg) {
        return deg * (Math.PI / 180)
}`

给予0.11120001099379416公里

gives0.11120001099379416 km

0.6 mtrs的差异

difference of 0.6 mtrs

推荐答案

原因是因为 Haversine公式您在上面使用的假设一个球形地球,圆周为6371 km,而Geography数据类型使用 WGS 84椭球.两者之间的主要区别在于,在WGS 84中,地球是扁球形(即在两极被挤压),并假设其围绕赤道的周长为6378.13 km,但围绕两极的周长为6356.75 km.使用WGS 84椭球进行计算的完整方程式非常复杂,在大多数情况下,Haversine被认为是很好的近似值.

The reason is because the Haversine formula you have used above assumes a spherical earth, with a circumference of 6371 km whereas the Geography datatype uses the WGS 84 ellipsoid. The main difference between the two is that in WGS 84 the earth is an oblate spheroid (ie, squashed at the poles) and is assumed to have a circumference of 6378.13 km around the equator, but a circumference of 6356.75 km around the poles. The full equations for calculations using the WGS 84 ellipsoid are quite complex and Haversine is considered a good approximation in most circumstances.

如果您查看此出色资源的介绍,请 http://www .movable-type.co.uk/scripts/latlong.html ,作者指出了这一确切点,并提出忽略椭圆体的平均误差约为0.3%(您的误差约为0.4%).我在这里重复作者的报价,以防万一该页面出现故障.

If you look at the introduction to this excellent resource, http://www.movable-type.co.uk/scripts/latlong.html, the author makes this exact point and suggests that the error from ignoring the ellipsoid is about 0.3% on average (yours is around 0.4%). I repeat the author's quote here, just in case the page ever goes down.

这篇关于javascript函数和sql server STD距离之间计算出的距离差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 21:30