使用半正公式计算纬度/经度之间的距离

时间:2010-11-30 13:21:34

标签: geometry geography

我想找到两个经度和纬度点之间的距离。我正在尝试使用great circle distance。这是公式: alt text

我不知道为什么但是我的程序无效。这是我得到的结果:

Change Angle: 0.00016244370761414
Earth Radius: 6371

RESULTS: 
Correct  Distance: 24.883 km
Computed Distance: 1.0349288612097

来源:

$latStart = 44.638;
$longStart = -63.587;

$latFinish = 44.644;
$longFinish = -63.597;


# Convert Input to Radians
$latStart = deg2Rad($latStart);
$longStart = deg2Rad($longStart);

$latFinish = deg2Rad($latFinish);
$longFinish = deg2Rad($longFinish);

# Because the Earth is not perfectly spherical, no single value serves as its 
# natural radius. Distances from points on the surface to the center range from 
# 6,353 km to 6,384 km (≈3,947–3,968 mi). Several different ways of modeling the 
# Earth as a sphere each yield a convenient mean radius of 6371 km (≈3,959 mi).
# http://en.wikipedia.org/wiki/Earth_radius
$earthRadius = 6371;

# difference in Long/Lat
$latChange = $latFinish - $latStart;
$longChange = $longFinish - $longStart;



# haversine formula 
# numerically stable for small distances
# http://en.wikipedia.org/wiki/Great-circle_distance
$changeAngle = 2 * asin(
                sqrt(
                        pow(sin($latChange/2),2) +
                        cos($latStart) * cos($latFinish) * pow(sin($longChange/2),2)
                )
        );



echo "Change Angle: $changeAngle\n";
echo "Earth Radius: $earthRadius\n";

3 个答案:

答案 0 :(得分:2)

让我们使用平面近似进行背面检查。纬度差为0.006°,经度差为0.01°,但乘以纬度余弦得到0.0075°。申请毕达哥拉斯:

>>> sqrt(0.006 ** 2 + 0.0075 ** 2)
0.0096046863561492727

大约是0.000167弧度,非常接近你的计算。 (甚至更多的背包检查:一个度数大约69英里,这有点超过100公里,所以0.01°应该超过1公里。)

所以我认为这是你所谓的“正确的距离”,这是错误的,而不是你的计算。

答案 1 :(得分:1)

你的方法松散地基于毕达哥拉斯定理 - 我总是以艰难的方式完成它,即类似的东西(实际上,我预先计算轴的值并将它们与数据一起存储在数据库中) :

$startXAxis   = cos(deg2Rad($latStart)) * cos(deg2Rad($longStart));
$startYAxis   = cos(deg2Rad($latStart)) * sin(deg2Rad($longStart));
$startZAxis   = sin(deg2Rad($latStart));
$finishXAxis   = cos(deg2Rad($latFinish)) * cos(deg2Rad($longFinish));
$finishYAxis   = cos(deg2Rad($latFinish)) * sin(deg2Rad($longFinish));
$finishZAxis   = sin(deg2Rad($latFinish));

$changeAngle = acos($startXAxis * $finishXAxis + $startYAxis * $finishYAxis + $startZAxis * $finishZAxis);

答案 2 :(得分:1)

您的公式与我的实施方式不同。不过我的是.NET,但是我已对它进行了单元测试,效果很好。

这是一个稍微重写的版本:http://megocode3.wordpress.com/2008/02/05/haversine-formula-in-c/

/// <summary>
/// Implementation of the Haversine formula
/// For calculating the distance between 2 points on a sphere
/// http://en.wikipedia.org/wiki/Haversine_formula
/// </summary>
public class Haversine
{
    /// <summary>
    /// Calculate the distance between 2 points in miles or kilometers
    /// http://megocode3.wordpress.com/2008/02/05/haversine-formula-in-c/
    /// 
    /// This assumes sea level
    /// </summary>
    public double Distance(LatLon pos1, LatLon pos2, DistanceType type)
    {
        const double RADIUS_OF_EARTH_IN_MILES = 3963.1676;
        const double RADIUS_OF_EARTH_IN_KILOMETERS = 6378.1;

        //radius of the earth
        double R = (type == DistanceType.Miles) ? RADIUS_OF_EARTH_IN_MILES : RADIUS_OF_EARTH_IN_KILOMETERS;

        //Deltas
        double dLat = ToRadian(pos2.Lat - pos1.Lat);
        double dLon = ToRadian(pos2.Lon - pos1.Lon);

        double a = Math.Sin(dLat/2)*Math.Sin(dLat/2) + Math.Cos(ToRadian(pos1.Lat))*Math.Cos(ToRadian(pos2.Lat)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));

        double d = R*c;
        return d;
    }

    /// <summary>
    /// Convert to Radians.
    /// </summary>
    private double ToRadian(double val)
    {
        return (Math.PI / 180) * val;
    }
}