3

2 つの経度点と緯度点の間の距離を見つけようとしています。大円距離を使おうとしています。これは式です: 代替テキスト

理由はわかりませんが、プログラムが機能しません。これは私が得ている結果です:

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";
4

3 に答える 3

2

平面近似を使用して封筒裏の計算を行いましょう。緯度の差は0.006°、経度の差は0.01°ですが、緯度の正弦を掛けると0.0075°になります。ピタゴラスを適用します。

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

これは約0.000167ラジアンで、計算にかなり近い値です。(さらに封筒裏の計算:度は約69マイルで、100 kmを少し超えるので、0.01°は1 kmを少し超える必要があります。)

ですから、あなたの計算ではなく、あなたの主張する「正しい距離」が間違っていると思います。

于 2010-11-30T14:11:43.217 に答える
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);
于 2010-11-30T14:13:10.353 に答える
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;
    }
}
于 2010-11-30T14:13:49.877 に答える