1

我一直在尝试用以下方法实现文森蒂的公式

    /* Implemented using Vincenty's formulae from http://en.wikipedia.org/wiki/Vincenty%27s_formulae,
 * answers "Direct Problem".
 * $latlng is a ('lat'=>x1, 'lng'=>y1) array
 * $distance is in miles
 * $angle is in degrees
 */
function addDistance($latlng, $distance, $bearing) {
    //variables
    $bearing = deg2rad($bearing);
    $iterations = 20; //avoid too-early termination while avoiding the non-convergant case

    //knowns
    $f = EARTH_SPHEROID_FLATTENING; //1/298.257223563
    $a = EARTH_RADIUS_EQUATOR_MILES; //3963.185 mi
    $phi1 = deg2rad($latlng['lat']);
    $l1 = deg2rad($latlng['lng']);
    $b = (1 - $f) * $a;

    //first block
    $tanU1 = (1-$f)*tan($phi1);
    $U1 = atan($tanU1);
    $sigma1 = atan($tanU1 / cos($bearing));
    $sinalpha = cos($U1)*sin($bearing);
    $cos2alpha = (1 - $sinalpha) * (1 + $sinalpha);
    $usquared = $cos2alpha * (($a*$a - $b*$b) / 2);
    $A = 1 + ($usquared)/16384 * (4096+$usquared*(-768+$usquared*(320 - 175*$usquared)));
    $B = ($usquared / 1024)*(256*$usquared*(-128 + $usquared * (74 - 47*$usquared)));

    //the loop - determining our value
    $sigma = $distance / ($b * $A);
    for($i = 0; $i < $iterations; ++$i) {
        $twosigmam = 2*$sigma1 + $sigma;
        $delta_sigma = $B * sin($sigma) * (cos($twosigmam)+(1/4)*$B*(cos(-1 + 2*cos(cos($twosigmam))) - (1/6)*$B*cos($twosigmam)*(-3+4*sin(sin($sigma)))*(-3+4*cos(cos($twosigmam)))));
        $sigma = $distance / ($b * $A) + $delta_sigma;
    }

    //second block
    $phi2 = atan((sin($U1)*cos($sigma)+cos($U1)*sin($sigma)*cos($bearing)) / ((1-$f) * sqrt(sin($sinalpha) + pow(sin($U1)*sin($sigma) - cos($U1)*cos($sigma)*cos($bearing), 2))));
    $lambda = atan((sin($sigma) * sin($bearing)) / (cos($U1)*cos($sigma) - sin($U1)*sin($sigma)*cos($bearing)));
    $C = ($f / 16)* $cos2alpha * (4+$f*(4-3*$cos2alpha));
    $L = $lambda - (1 - $C) * $f * $sinalpha * ($sigma + $C*sin($sigma)*(cos($twosigmam)+$C*cos($sigma)*(-1+2*cos(cos($twosigmam)))));
    $alpha2 = atan($sinalpha / (-sin($U1)*sin($sigma) + cos($U1)*cos($sigma)*cos($bearing)));

    //and return our results
    return array('lat' => rad2deg($phi2), 'lng' => rad2deg($lambda));
}

    var_dump(addDistance(array('lat' => 93.129, 'lng' => -43.221), 20, 135);

问题是结果不合理 - 我得到的纬度和经度差异高达 20 度,距离保持在 20 度。它不是以球体上的椭圆距离为单位吗?我误解了什么,还是我的实施有缺陷?

4

2 回答 2

3

维基百科页面直接问题部分的转录存在许多错误:

  • 您的u2表达式2在分母中应该有b2
  • 您的AB表达式对于是否需要用括号括起来才能正确表示a / b * c为不一致(a/b) * c- 没有括号会发生什么是 php 语法问题,我不知道答案,但您应该更清楚;
  • 您应该迭代“直到 sigma 没有显着变化”,这可能会或可能不会在您的固定迭代次数中发生;
  • 您的 DELTA_sigma 公式中有错误:
    • 在维基百科页面上,方括号内的第一个词[cos sigma (-1etc,而你有cos (-1etc,这是非常不同的;
    • 在相同的公式中以及稍后,请注意cos2 x表示(cos x)(cos x)而不是 cos cos x
  • 你的 phi_2 公式有一个sin($sinalpha)它应该有一个sin($sinalpha)*sin($sinalpha);

我想就是这样。

于 2012-07-11T08:24:10.103 回答
0

你有没有试过这个:

https://github.com/treffynnon/Geographic-Calculations-in-PHP

于 2012-07-10T15:43:01.220 回答