| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | // The approximate radius of the earth in meters, according to http://en.wikipedia.org/wiki/Earth_radius. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use DataValues\Geo\Values\LatLongValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | define( 'Maps_EARTH_RADIUS', 6371000 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Static class containing geographical functions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @since 0.6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author Pnelnik | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Matěj Grabovský | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class MapsGeoFunctions { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * Returns the geographical distance between two coordinates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * See http://en.wikipedia.org/wiki/Geographical_distance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @since 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @param LatLongValue $start | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @param LatLongValue $end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @return float Distance in m. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	public static function calculateDistance( LatLongValue $start, LatLongValue $end ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 		$northRad1 = deg2rad( $start->getLatitude() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 		$eastRad1 = deg2rad( $start->getLongitude() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 		$cosNorth1 = cos( $northRad1 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 		$cosEast1 = cos( $eastRad1 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 		$sinNorth1 = sin( $northRad1 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 		$sinEast1 = sin( $eastRad1 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 		$northRad2 = deg2rad( $end->getLatitude() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		$eastRad2 = deg2rad( $end->getLongitude() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		$cosNorth2 = cos( $northRad2 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		$cosEast2 = cos( $eastRad2 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		$sinNorth2 = sin( $northRad2 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		$sinEast2 = sin( $eastRad2 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 		$term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 		$term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 		$term3 = $sinNorth1 - $sinNorth2; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 		$distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 		$distance = 2 * Maps_EARTH_RADIUS * asin( sqrt( $distThruSquared ) / 2 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		assert( $distance >= 0 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 		return $distance; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * Finds a destination given a starting location, bearing and distance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @since 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @param LatLongValue $startingCoordinates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param float $bearing The initial bearing in degrees. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @param float $distance The distance to travel in km. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @return array The destination coordinates, as non-directional floats in an array with lat and lon keys. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	public static function findDestination( LatLongValue $startingCoordinates, $bearing, $distance ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$startingCoordinates = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			'lat' => deg2rad( $startingCoordinates->getLatitude() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			'lon' => deg2rad( $startingCoordinates->getLongitude() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$radBearing = deg2rad( (float)$bearing ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$angularDistance = $distance / Maps_EARTH_RADIUS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$lat = asin( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			sin( $startingCoordinates['lat'] ) * cos( $angularDistance ) + cos( $startingCoordinates['lat'] ) * sin( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				$angularDistance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			) * cos( $radBearing ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$lon = $startingCoordinates['lon'] + atan2( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				sin( $radBearing ) * sin( $angularDistance ) * cos( $startingCoordinates['lat'] ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				cos( $angularDistance ) - sin( $startingCoordinates['lat'] ) * sin( $lat ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			'lat' => rad2deg( $lat ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			'lon' => rad2deg( $lon ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  | } |