/** * @param Coordinate $point * @param Line $line * * @return float */ public function getPerpendicularDistance(Coordinate $point, Line $line) { $ellipsoid = $point->getEllipsoid(); $ellipsoidRadius = $ellipsoid->getArithmeticMeanRadius(); $firstLinePointLat = $this->deg2radLatitude($line->getPoint1()->getLat()); $firstLinePointLng = $this->deg2radLongitude($line->getPoint1()->getLng()); $firstLinePointX = $ellipsoidRadius * cos($firstLinePointLng) * sin($firstLinePointLat); $firstLinePointY = $ellipsoidRadius * sin($firstLinePointLng) * sin($firstLinePointLat); $firstLinePointZ = $ellipsoidRadius * cos($firstLinePointLat); $secondLinePointLat = $this->deg2radLatitude($line->getPoint2()->getLat()); $secondLinePointLng = $this->deg2radLongitude($line->getPoint2()->getLng()); $secondLinePointX = $ellipsoidRadius * cos($secondLinePointLng) * sin($secondLinePointLat); $secondLinePointY = $ellipsoidRadius * sin($secondLinePointLng) * sin($secondLinePointLat); $secondLinePointZ = $ellipsoidRadius * cos($secondLinePointLat); $pointLat = $this->deg2radLatitude($point->getLat()); $pointLng = $this->deg2radLongitude($point->getLng()); $pointX = $ellipsoidRadius * cos($pointLng) * sin($pointLat); $pointY = $ellipsoidRadius * sin($pointLng) * sin($pointLat); $pointZ = $ellipsoidRadius * cos($pointLat); $normalizedX = $firstLinePointY * $secondLinePointZ - $firstLinePointZ * $secondLinePointY; $normalizedY = $firstLinePointZ * $secondLinePointX - $firstLinePointX * $secondLinePointZ; $normalizedZ = $firstLinePointX * $secondLinePointY - $firstLinePointY * $secondLinePointX; $length = sqrt($normalizedX * $normalizedX + $normalizedY * $normalizedY + $normalizedZ * $normalizedZ); $normalizedX /= $length; $normalizedY /= $length; $normalizedZ /= $length; $thetaPoint = $normalizedX * $pointX + $normalizedY * $pointY + $normalizedZ * $pointZ; $length = sqrt($pointX * $pointX + $pointY * $pointY + $pointZ * $pointZ); $thetaPoint /= $length; $distance = abs(M_PI / 2 - acos($thetaPoint)); return $distance * $ellipsoidRadius; }
/** * @param Coordinate $point1 * @param Coordinate $point2 * * @throws NotMatchingEllipsoidException * @throws NotConvergingException * * @return float */ public function getDistance(Coordinate $point1, Coordinate $point2) { if ($point1->getEllipsoid() != $point2->getEllipsoid()) { throw new NotMatchingEllipsoidException("The ellipsoids for both coordinates must match"); } $lat1 = deg2rad($point1->getLat()); $lat2 = deg2rad($point2->getLat()); $lng1 = deg2rad($point1->getLng()); $lng2 = deg2rad($point2->getLng()); $a = $point1->getEllipsoid()->getA(); $b = $point1->getEllipsoid()->getB(); $f = 1 / $point1->getEllipsoid()->getF(); $L = $lng2 - $lng1; $U1 = atan((1 - $f) * tan($lat1)); $U2 = atan((1 - $f) * tan($lat2)); $iterationLimit = 100; $lambda = $L; $sinU1 = sin($U1); $sinU2 = sin($U2); $cosU1 = cos($U1); $cosU2 = cos($U2); do { $sinLambda = sin($lambda); $cosLambda = cos($lambda); $sinSigma = sqrt($cosU2 * $sinLambda * ($cosU2 * $sinLambda) + ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosLambda) * ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosLambda)); if ($sinSigma == 0) { return 0.0; } $cosSigma = $sinU1 * $sinU2 + $cosU1 * $cosU2 * $cosLambda; $sigma = atan2($sinSigma, $cosSigma); $sinAlpha = $cosU1 * $cosU2 * $sinLambda / $sinSigma; $cosSqAlpha = 1 - $sinAlpha * $sinAlpha; if ($cosSqAlpha == 0) { $cos2SigmaM = 0; } else { $cos2SigmaM = $cosSigma - 2 * $sinU1 * $sinU2 / $cosSqAlpha; } $C = $f / 16 * $cosSqAlpha * (4 + $f * (4 - 3 * $cosSqAlpha)); $lambdaP = $lambda; $lambda = $L + (1 - $C) * $f * $sinAlpha * ($sigma + $C * $sinSigma * ($cos2SigmaM + $C * $cosSigma * (-1 + 2 * $cos2SigmaM * $cos2SigmaM))); } while (abs($lambda - $lambdaP) > 1.0E-12 && --$iterationLimit > 0); if ($iterationLimit == 0) { throw new NotConvergingException(); } $uSq = $cosSqAlpha * ($a * $a - $b * $b) / ($b * $b); $A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq))); $B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq))); $deltaSigma = $B * $sinSigma * ($cos2SigmaM + $B / 4 * ($cosSigma * (-1 + 2 * $cos2SigmaM * $cos2SigmaM) - $B / 6 * $cos2SigmaM * (-3 + 4 * $sinSigma * $sinSigma) * (-3 + 4 * $cos2SigmaM * $cos2SigmaM))); $s = $b * $A * ($sigma - $deltaSigma); return round($s, 3); }
/** * @param Coordinate $point1 * @param Coordinate $point2 * * @throws NotMatchingEllipsoidException * * @return float */ public function getDistance(Coordinate $point1, Coordinate $point2) { if ($point1->getEllipsoid() != $point2->getEllipsoid()) { throw new NotMatchingEllipsoidException("The ellipsoids for both coordinates must match"); } $lat1 = deg2rad($point1->getLat()); $lat2 = deg2rad($point2->getLat()); $lng1 = deg2rad($point1->getLng()); $lng2 = deg2rad($point2->getLng()); $dLat = $lat2 - $lat1; $dLng = $lng2 - $lng1; $radius = $point1->getEllipsoid()->getArithmeticMeanRadius(); $s = 2 * $radius * asin(sqrt(pow(sin($dLat / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($dLng / 2), 2))); return round($s, 3); }
private function inverseVincenty(Coordinate $point1, Coordinate $point2) { $φ1 = deg2rad($point1->getLat()); $φ2 = deg2rad($point2->getLat()); $λ1 = deg2rad($point1->getLng()); $λ2 = deg2rad($point2->getLng()); $a = $point1->getEllipsoid()->getA(); $b = $point1->getEllipsoid()->getB(); $f = 1 / $point1->getEllipsoid()->getF(); $L = $λ2 - $λ1; $tanU1 = (1 - $f) * tan($φ1); $cosU1 = 1 / sqrt(1 + $tanU1 * $tanU1); $sinU1 = $tanU1 * $cosU1; $tanU2 = (1 - $f) * tan($φ2); $cosU2 = 1 / sqrt(1 + $tanU2 * $tanU2); $sinU2 = $tanU2 * $cosU2; $λ = $L; $iterations = 0; do { $sinλ = sin($λ); $cosλ = cos($λ); $sinSqσ = $cosU2 * $sinλ * ($cosU2 * $sinλ) + ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ) * ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ); $sinσ = sqrt($sinSqσ); if ($sinσ == 0) { return 0; } $cosσ = $sinU1 * $sinU2 + $cosU1 * $cosU2 * $cosλ; $σ = atan2($sinσ, $cosσ); $sinα = $cosU1 * $cosU2 * $sinλ / $sinσ; $cosSqα = 1 - $sinα * $sinα; if ($cosSqα == 0.0) { $cos2σM = 0; } else { $cos2σM = $cosσ - 2 * $sinU1 * $sinU2 / $cosSqα; } $C = $f / 16 * $cosSqα * (4 + $f * (4 - 3 * $cosSqα)); $λp = $λ; $λ = $L + (1 - $C) * $f * $sinα * ($σ + $C * $sinσ * ($cos2σM + $C * $cosσ * (-1 + 2 * $cos2σM * $cos2σM))); } while (abs($λ - $λp) > 1.0E-12 && ++$iterations < 200); if ($iterations >= 200) { throw new NotConvergingException('Inverse Vincenty Formula did not converge'); } $uSq = $cosSqα * ($a * $a - $b * $b) / ($b * $b); $A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq))); $B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq))); $Δσ = $B * $sinσ * ($cos2σM + $B / 4 * ($cosσ * (-1 + 2 * $cos2σM * $cos2σM) - $B / 6 * $cos2σM * (-3 + 4 * $sinσ * $sinσ) * (-3 + 4 * $cos2σM * $cos2σM))); $s = $b * $A * ($σ - $Δσ); $α1 = atan2($cosU2 * $sinλ, $cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ); $α2 = atan2($cosU1 * $sinλ, -$sinU1 * $cosU2 + $cosU1 * $sinU2 * $cosλ); $α1 = fmod($α1 + 2 * M_PI, 2 * M_PI); $α2 = fmod($α2 + 2 * M_PI, 2 * M_PI); $s = round($s, 3); return ['distance' => $s, 'bearing_initial' => rad2deg($α1), 'bearing_final' => rad2deg($α2)]; }
/** * @covers Location\Coordinate::getEllipsoid */ public function testGetEllipsoid() { $this->assertEquals($this->ellipsoid, $this->coordinate->getEllipsoid()); }