.

GeoCalculator Class Reference
[GeoCalc]

Collection of useful functions related to geo coordinates. More...

List of all members.

Static Public Member Functions

static  bounding_box ($lat, $lon, $ns_radius, $we_radius)
  Compute bounding box.
static  bounding_box_of ($arr_coordinates)
  Compute bounding box of given coordinates.
static  distance ($lat1, $lon1, $lat2, $lon2)
  Calculates distance between to points.

Public Attributes

const  R = 6371

Detailed Description

Collection of useful functions related to geo coordinates.

Author:
Gerd Riesselmann

Definition at line 8 of file geocalculator.cls.php.


Member Function Documentation

static GeoCalculator::bounding_box ( lat,
lon,
ns_radius,
we_radius  
) [static]

Compute bounding box.

Parameters:
float  $lat Latitude
float  $lon Longitude
float  $ns_radius Radius of bound box in north to south direction
float  $we_radius Radius of bound box in west to east direction
Returns:
array Array with two elements 'lat' and 'lon', each containing an array with two elements 'min' and 'max

Definition at line 49 of file geocalculator.cls.php.

00049                                                                                 {
00050                 // http://www.movable-type.co.uk/scripts/latlong-db.html
00051                 $d_lat = rad2deg($ns_radius/self::R);
00052                 $d_lon = rad2deg($we_radius/self::R/cos(deg2rad($lat)));
00053                 
00054                 return array(
00055                         'lat' => array('min' => $lat - $d_lat, 'max' => $lat + $d_lat),
00056                         'lon' => array('min' => $lon - $d_lon, 'max' => $lon + $d_lon)
00057                 );
00058         }
static GeoCalculator::bounding_box_of ( arr_coordinates  )  [static]

Compute bounding box of given coordinates.

 GeoCalculator::bounding_box_of(array(
   array('lat' => 50.96951, 'lon' => 7.0446),
   array('lat' => 50.92181, 'lon' => 6.9462)
 );
Parameters:
$arr_coordinates  Array of associative array with keys "lat" and "lon"
Returns:
array Array with two elements 'lat' and 'lon', each containing an array with two elements 'min' and 'max. False if $arr_coordinates is empty

Definition at line 75 of file geocalculator.cls.php.

00075                                                                  {
00076                 $first = array_shift($arr_coordinates);
00077                 if ($first == false) {
00078                         return false;
00079                 }
00080                 $lat_min = Arr::get_item($first, 'lat', 0.0);
00081                 $lat_max = $lat_min;
00082                 $lon_min = Arr::get_item($first, 'lon', 0.0);
00083                 $lon_max = $lon_min;
00084 
00085                 foreach($arr_coordinates as $c) {
00086                         $lat = Arr::get_item($c, 'lat', 0.0);
00087                         $lon = Arr::get_item($c, 'lon', 0.0);
00088                         $lat_min = min($lat_min, $lat);
00089                         $lat_max = max($lat_max, $lat);
00090                         $lon_min = min($lon_min, $lon);
00091                         $lon_max = max($lon_max, $lon);
00092                 }
00093 
00094                 return array(
00095                         'lat' => array('min' => $lat_min, 'max' => $lat_max),
00096                         'lon' => array('min' => $lon_min, 'max' => $lon_max)
00097                 );
00098         }
static GeoCalculator::distance ( lat1,
lon1,
lat2,
lon2  
) [static]

Calculates distance between to points.

Parameters:
float  $lat1 Latitude of point 1;
float  $lon1 Longitude of point 1;
float  $lat2 Latitude of point 2;
float  $lon2 Longitude of point 2;
Returns:
float Distance in km

Definition at line 20 of file geocalculator.cls.php.

00020                                                                     {
00021                 // http://www.movable-type.co.uk/scripts/latlong.html
00022                 $lat1 = deg2rad($lat1);
00023                 $lat2 = deg2rad($lat2);
00024                 $lon1 = deg2rad($lon1);
00025                 $lon2 = deg2rad($lon2);
00026                 
00027                 $dLat = $lat2 - $lat1;
00028                 $dLon = $lon2 - $lon1; 
00029                 $a = 
00030                         sin($dLat/2) * sin($dLat/2) +
00031                         cos($lat1) * cos($lat2) * 
00032                         sin($dLon/2) * sin($dLon/2); 
00033                 $c = 2 * atan2(sqrt($a), sqrt(1-$a)); 
00034                 $d = self::R * $c;
00035                 return $d;
00036         }

Member Data Documentation

const GeoCalculator::R = 6371

Definition at line 9 of file geocalculator.cls.php.


The documentation for this class was generated from the following file: