Browse files

Initial checking provides base classes, instructions and distance alg…

…orithm implementation.
  • Loading branch information...
0 parents commit b6294fbd1053012d9d15ef298f7edaf4a107fe5b @weejames committed Apr 24, 2011
Showing with 63 additions and 0 deletions.
  1. 0 README
  2. +3 −0 config/autoload.php
  3. +13 −0 libraries/geopoint.php
  4. +47 −0 libraries/geotools.php
0 README
No changes.
3 config/autoload.php
@@ -0,0 +1,3 @@
+<?php
+
+$autoload['libraries'] = array('geotools');
13 libraries/geopoint.php
@@ -0,0 +1,13 @@
+<?php
+
+class Geopoint {
+
+ public $latitude;
+ public $longitude;
+
+ public function __construct($latitude, $longitude) {
+ $this->latitude = $latitude;
+ $this->longitude = $longitude;
+ }
+
+}
47 libraries/geotools.php
@@ -0,0 +1,47 @@
+<?php
+
+class Geotools {
+
+ private $defaultUnit = 'km';
+ private $kmPerMile = 1.609;
+ private $earthRadius = array();
+
+ public function __construct() {
+ $this->earthRadius['miles'] = 3963.19;
+ $this->earthRadius['km'] = $this->earthRadius['miles'] * $this->kmPerMile;
+ }
+
+ public function distanceBetween(Geopoint $pointA, Geopoint $pointB, $algorithm = 'flat', $unit = null) {
+
+ if (!$unit) $unit = $this->defaultUnit;
+
+ switch ($algorithm) {
+ case 'haversine':
+ $theta = ($pointA->longitude - $pointB->longitude);
+ $dist = sin(deg2rad($pointA->latitude)) * sin(deg2rad($pointB->latitude)) + cos(deg2rad($pointA->latitude)) * cos(deg2rad($pointB->latitude)) * cos(deg2rad($theta));
+ $dist = acos($dist);
+
+ $distance = rad2deg($dist);
+ break;
+ case 'flat':
+ default:
+ $distanceEW = ($pointB->longitude - $pointA->longitude) * cos($pointA->latitude);
+ $distanceNS = $pointB->latitude - $pointA->latitude;
+
+
+ $distance = sqrt( ($distanceEW * $distanceEW) + ($distanceNS * $distanceNS));
+ break;
+ }
+
+ $distance *= 2 * pi() * $this->earthRadius[$unit] / 360.0;
+
+ return $distance;
+
+ }
+
+ public function geopoint($latitude, $longitude) {
+ include_once('geopoint.php');
+ return new Geopoint((float)$latitude, (float)$longitude);
+ }
+
+}

0 comments on commit b6294fb

Please sign in to comment.