Distance class
Moved the convert unit logic out of the Convert class. Added a method to calculate distances between 2 points
This commit is contained in:
parent
bf95e07591
commit
7fb8265998
3 changed files with 291 additions and 38 deletions
|
@ -18,8 +18,8 @@
|
||||||
/**
|
/**
|
||||||
* Convert Class
|
* Convert Class
|
||||||
*
|
*
|
||||||
* Collection of statically called methods to help aid in converting formats as
|
* Collection of statically called methods to help aid in converting data
|
||||||
* well as distances.
|
* formats.
|
||||||
*/
|
*/
|
||||||
class Convert
|
class Convert
|
||||||
{
|
{
|
||||||
|
@ -117,23 +117,6 @@ class Convert
|
||||||
return $xml;
|
return $xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
// {{{ Meters to Miles
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Meters to Miles
|
|
||||||
*
|
|
||||||
* Converts meters to miles.
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @param mixed $meters meters to convert to miles
|
|
||||||
* @return mixed number of miles
|
|
||||||
*/
|
|
||||||
public static function metersToMiles($meters)
|
|
||||||
{
|
|
||||||
return $meters * 0.00062137119;
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// {{{ To JSON
|
// {{{ To JSON
|
||||||
|
|
||||||
|
|
145
classes/Distance.php
Normal file
145
classes/Distance.php
Normal file
|
@ -0,0 +1,145 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Distance
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* Licensed under The MIT License
|
||||||
|
* Redistribution of these files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @author Josh Sherman <pickles@joshtronic.com>
|
||||||
|
* @copyright Copyright 2007-2012, Josh Sherman
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.html
|
||||||
|
* @package PICKLES
|
||||||
|
* @link https://github.com/joshtronic/pickles
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Distance Class
|
||||||
|
*
|
||||||
|
* Collection of statically called methods to help aid distance-related
|
||||||
|
* conversions and calculations.
|
||||||
|
*/
|
||||||
|
class Distance
|
||||||
|
{
|
||||||
|
// {{{ Call Static
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call Static
|
||||||
|
*
|
||||||
|
* Magic method to power the unit conversion without much code.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @param string $method name of the static method being called
|
||||||
|
* @param array $arguments array of the passed arguments
|
||||||
|
* @return mixed converted units or false
|
||||||
|
*/
|
||||||
|
public static function __callStatic($method, $arguments)
|
||||||
|
{
|
||||||
|
$pieces = explode('to', strtolower($method));
|
||||||
|
|
||||||
|
if (count($pieces) == 2)
|
||||||
|
{
|
||||||
|
var_dump($arguments[0], $pieces[0], $pieces[1]);
|
||||||
|
return Distance::convertUnit($arguments[0], $pieces[0], $pieces[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
// {{{ Convert Unit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Unit
|
||||||
|
*
|
||||||
|
* Converts a distance from one unit to another.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @param mixed $distance starting distance
|
||||||
|
* @param string $from starting unit
|
||||||
|
* @param string $to ending unit
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private static function convertUnit($distance, $from, $to)
|
||||||
|
{
|
||||||
|
$multiplier = 1;
|
||||||
|
|
||||||
|
switch ($from)
|
||||||
|
{
|
||||||
|
case 'kilometers':
|
||||||
|
switch ($to)
|
||||||
|
{
|
||||||
|
case 'miles': $multiplier = 0.621371; break;
|
||||||
|
case 'meters': $multiplier = 1000; break;
|
||||||
|
case 'yards': $multiplier = 1093.61; break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'miles':
|
||||||
|
switch ($to)
|
||||||
|
{
|
||||||
|
case 'kilometers': $multiplier = 1.60934; break;
|
||||||
|
case 'meters': $multiplier = 1609.34; break;
|
||||||
|
case 'yards': $multiplier = 1760; break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'meters':
|
||||||
|
switch ($to)
|
||||||
|
{
|
||||||
|
case 'kilometers': $multiplier = 0.001; break;
|
||||||
|
case 'miles': $multiplier = 0.000621371; break;
|
||||||
|
case 'yards': $multiplier = 1.09361; break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $distance * $multiplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
// {{{ Calculate Distance
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate Distance
|
||||||
|
*
|
||||||
|
* Calculates the distance between two sets of coordinates and returns the
|
||||||
|
* requested units. I really wanted to call this distance() but it seems
|
||||||
|
* you can't do that in PHP due to the backwards compatibility of the
|
||||||
|
* PHP4 constructors that were named the same as the class.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @param mixed $latitude_from starting latitude
|
||||||
|
* @param mixed $longitude_from starting longitude
|
||||||
|
* @param mixed $latitude_from starting latitude
|
||||||
|
* @param mixed $longitude_from starting longitude
|
||||||
|
* @param string $unit optional units to return, miles by default
|
||||||
|
* @return mixed distance between the points in the desired unit
|
||||||
|
*/
|
||||||
|
public static function calculateDistance($latitude_from, $longitude_from, $latitude_to, $latitude_from, $unit = 'miles')
|
||||||
|
{
|
||||||
|
$unit = ucwords(strtolower($unit));
|
||||||
|
$theta = $lontitude_from - $longitude_to;
|
||||||
|
|
||||||
|
$distance =
|
||||||
|
sin(deg2rad($latitude_from))
|
||||||
|
* sin(deg2rad($latitude_to))
|
||||||
|
+ cos(deg2rad($latitude_from))
|
||||||
|
* cos(deg2rad($latitude_to))
|
||||||
|
* cos(deg2rad($theta));
|
||||||
|
|
||||||
|
$distance = acos($distance);
|
||||||
|
$distance = rad2deg($distance);
|
||||||
|
$miles = $distance * 60 * 1.1515;
|
||||||
|
|
||||||
|
$method = 'milesTo' . $unit;
|
||||||
|
|
||||||
|
return Distance::$method($miles);
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
163
jar.php
163
jar.php
|
@ -1529,8 +1529,8 @@ class Controller extends Object
|
||||||
/**
|
/**
|
||||||
* Convert Class
|
* Convert Class
|
||||||
*
|
*
|
||||||
* Collection of statically called methods to help aid in converting formats as
|
* Collection of statically called methods to help aid in converting data
|
||||||
* well as distances.
|
* formats.
|
||||||
*/
|
*/
|
||||||
class Convert
|
class Convert
|
||||||
{
|
{
|
||||||
|
@ -1628,23 +1628,6 @@ class Convert
|
||||||
return $xml;
|
return $xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
// {{{ Meters to Miles
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Meters to Miles
|
|
||||||
*
|
|
||||||
* Converts meters to miles.
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @param mixed $meters meters to convert to miles
|
|
||||||
* @return mixed number of miles
|
|
||||||
*/
|
|
||||||
public static function metersToMiles($meters)
|
|
||||||
{
|
|
||||||
return $meters * 0.00062137119;
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// {{{ To JSON
|
// {{{ To JSON
|
||||||
|
|
||||||
|
@ -2880,6 +2863,148 @@ class Display_XML extends Display_Common
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Distance
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* Licensed under The MIT License
|
||||||
|
* Redistribution of these files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @author Josh Sherman <pickles@joshtronic.com>
|
||||||
|
* @copyright Copyright 2007-2012, Josh Sherman
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.html
|
||||||
|
* @package PICKLES
|
||||||
|
* @link https://github.com/joshtronic/pickles
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Distance Class
|
||||||
|
*
|
||||||
|
* Collection of statically called methods to help aid distance-related
|
||||||
|
* conversions and calculations.
|
||||||
|
*/
|
||||||
|
class Distance
|
||||||
|
{
|
||||||
|
// {{{ Call Static
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call Static
|
||||||
|
*
|
||||||
|
* Magic method to power the unit conversion without much code.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @param string $method name of the static method being called
|
||||||
|
* @param array $arguments array of the passed arguments
|
||||||
|
* @return mixed converted units or false
|
||||||
|
*/
|
||||||
|
public static function __callStatic($method, $arguments)
|
||||||
|
{
|
||||||
|
$pieces = explode('to', strtolower($method));
|
||||||
|
|
||||||
|
if (count($pieces) == 2)
|
||||||
|
{
|
||||||
|
var_dump($arguments[0], $pieces[0], $pieces[1]);
|
||||||
|
return Distance::convertUnit($arguments[0], $pieces[0], $pieces[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
// {{{ Convert Unit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Unit
|
||||||
|
*
|
||||||
|
* Converts a distance from one unit to another.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @param mixed $distance starting distance
|
||||||
|
* @param string $from starting unit
|
||||||
|
* @param string $to ending unit
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private static function convertUnit($distance, $from, $to)
|
||||||
|
{
|
||||||
|
$multiplier = 1;
|
||||||
|
|
||||||
|
switch ($from)
|
||||||
|
{
|
||||||
|
case 'kilometers':
|
||||||
|
switch ($to)
|
||||||
|
{
|
||||||
|
case 'miles': $multiplier = 0.621371; break;
|
||||||
|
case 'meters': $multiplier = 1000; break;
|
||||||
|
case 'yards': $multiplier = 1093.61; break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'miles':
|
||||||
|
switch ($to)
|
||||||
|
{
|
||||||
|
case 'kilometers': $multiplier = 1.60934; break;
|
||||||
|
case 'meters': $multiplier = 1609.34; break;
|
||||||
|
case 'yards': $multiplier = 1760; break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'meters':
|
||||||
|
switch ($to)
|
||||||
|
{
|
||||||
|
case 'kilometers': $multiplier = 0.001; break;
|
||||||
|
case 'miles': $multiplier = 0.000621371; break;
|
||||||
|
case 'yards': $multiplier = 1.09361; break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $distance * $multiplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
// {{{ Calculate Distance
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate Distance
|
||||||
|
*
|
||||||
|
* Calculates the distance between two sets of coordinates and returns the
|
||||||
|
* requested units. I really wanted to call this distance() but it seems
|
||||||
|
* you can't do that in PHP due to the backwards compatibility of the
|
||||||
|
* PHP4 constructors that were named the same as the class.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @param mixed $latitude_from starting latitude
|
||||||
|
* @param mixed $longitude_from starting longitude
|
||||||
|
* @param mixed $latitude_from starting latitude
|
||||||
|
* @param mixed $longitude_from starting longitude
|
||||||
|
* @param string $unit optional units to return, miles by default
|
||||||
|
* @return mixed distance between the points in the desired unit
|
||||||
|
*/
|
||||||
|
public static function calculateDistance($latitude_from, $longitude_from, $latitude_to, $latitude_from, $unit = 'miles')
|
||||||
|
{
|
||||||
|
$unit = ucwords(strtolower($unit));
|
||||||
|
$theta = $lontitude_from - $longitude_to;
|
||||||
|
|
||||||
|
$distance =
|
||||||
|
sin(deg2rad($latitude_from))
|
||||||
|
* sin(deg2rad($latitude_to))
|
||||||
|
+ cos(deg2rad($latitude_from))
|
||||||
|
* cos(deg2rad($latitude_to))
|
||||||
|
* cos(deg2rad($theta));
|
||||||
|
|
||||||
|
$distance = acos($distance);
|
||||||
|
$distance = rad2deg($distance);
|
||||||
|
$miles = $distance * 60 * 1.1515;
|
||||||
|
|
||||||
|
$method = 'milesTo' . $unit;
|
||||||
|
|
||||||
|
return Distance::$method($miles);
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamic Content Class File for PICKLES
|
* Dynamic Content Class File for PICKLES
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue