42 lines
956 B
PHP
42 lines
956 B
PHP
<?php
|
|
/**
|
|
* Haversine distance from Font del Bosc.
|
|
*
|
|
* @package FontdelboscCombustible
|
|
*/
|
|
|
|
declare( strict_types=1 );
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Distance calculations against the fixed origin point.
|
|
*/
|
|
final class Fontdelbosc_Combustible_Geo {
|
|
|
|
/**
|
|
* Reference latitude of Font del Bosc.
|
|
*/
|
|
public const ORIGIN_LAT = 41.4819;
|
|
|
|
/**
|
|
* Reference longitude of Font del Bosc.
|
|
*/
|
|
public const ORIGIN_LON = 1.6483;
|
|
|
|
/**
|
|
* Great-circle distance in km from Font del Bosc to a point.
|
|
*
|
|
* @param float $lat2 Target latitude.
|
|
* @param float $lon2 Target longitude.
|
|
*/
|
|
public static function haversine( float $lat2, float $lon2 ): float {
|
|
$r = 6371;
|
|
$dlat = deg2rad( $lat2 - self::ORIGIN_LAT );
|
|
$dlon = deg2rad( $lon2 - self::ORIGIN_LON );
|
|
$a = ( sin( $dlat / 2 ) ** 2 )
|
|
+ ( cos( deg2rad( self::ORIGIN_LAT ) ) * cos( deg2rad( $lat2 ) ) * ( sin( $dlon / 2 ) ** 2 ) );
|
|
|
|
return 2 * $r * asin( sqrt( $a ) );
|
|
}
|
|
}
|