Conditions | 2 |
Paths | 2 |
Total Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
28 | public function getDistance(Coordinate $point1, Coordinate $point2): float |
||
29 | { |
||
30 | if ($point1->getEllipsoid()->getName() !== $point2->getEllipsoid()->getName()) { |
||
31 | throw new NotMatchingEllipsoidException('The ellipsoids for both coordinates must match'); |
||
32 | } |
||
33 | |||
34 | $lat1 = deg2rad($point1->getLat()); |
||
35 | $lat2 = deg2rad($point2->getLat()); |
||
36 | $lng1 = deg2rad($point1->getLng()); |
||
37 | $lng2 = deg2rad($point2->getLng()); |
||
38 | |||
39 | $dLat = $lat2 - $lat1; |
||
40 | $dLng = $lng2 - $lng1; |
||
41 | |||
42 | $radius = $point1->getEllipsoid()->getArithmeticMeanRadius(); |
||
43 | |||
44 | $distance = 2 * $radius * asin( |
||
45 | sqrt( |
||
46 | (sin($dLat / 2) ** 2) |
||
47 | + cos($lat1) * cos($lat2) * (sin($dLng / 2) ** 2) |
||
48 | ) |
||
49 | ); |
||
50 | |||
51 | return round($distance, 3); |
||
52 | } |
||
53 | } |
||
54 |