00001 <?php
00002 Load::components('geocalculator');
00003
00004
00005
00006
00007
00008
00009
00010 class GeoCoordinate {
00011
00012
00013
00014
00015 public $lat;
00016
00017
00018
00019
00020 public $lon;
00021
00022
00023
00024
00025
00026
00027
00028 public function __construct($lat, $lon) {
00029 $this->lat = $lat;
00030 $this->lon = $lon;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 public function to_string($divider = ', ', $precision = 4, $system = false) {
00043 return
00044 $this->lat_to_string($precision, $system) .
00045 $divider .
00046 $this->lon_to_string($precision, $system);
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 public function lat_to_string($precision = 4, $system = false) {
00058 return String::number($this->lat, $precision, $system);
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 public function lon_to_string($precision = 4, $system = false) {
00070 return String::number($this->lon, $precision, $system);
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 public function distance_to($other) {
00080 $ret = false;
00081 if ($this->is_valid() && $other->is_valid()) {
00082 $ret = GeoCalculator::distance($this->lat, $this->lon, $other->lat, $other->lon);
00083 }
00084 return $ret;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 public function center_of($other) {
00094 $ret = false;
00095 if ($this->is_valid() && $other->is_valid()) {
00096 $ret = new GeoCoordinate(
00097 $this->lat + ($other->lat - $this->lat) / 2.0,
00098 $this->lon + ($other->lon - $this->lon) / 2.0
00099 );
00100 }
00101 return $ret;
00102 }
00103
00104
00105
00106
00107
00108 public function is_within($coord1, $coord2) {
00109 $ret = false;
00110 if ($this->is_valid() && $coord1->is_valid() && $coord2->is_valid()) {
00111 $max_lat = max($coord1->lat, $coord2->lat);
00112 $min_lat = min($coord1->lat, $coord2->lat);
00113 $max_lon = max($coord1->lon, $coord2->lon);
00114 $min_lon = min($coord1->lon, $coord2->lon);
00115 $ret =
00116 $this->lat >= $min_lat && $this->lat <= $max_lat &&
00117 $this->lon >= $min_lon && $this->lon <= $max_lon;
00118 }
00119 return $ret;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 public function is_valid() {
00130 return is_numeric($this->lat) && is_numeric($this->lon);
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 public function bounding_box($ns_radius, $we_radius) {
00145 $arr = GeoCalculator::bounding_box($this->lat, $this->lon, $ns_radius, $we_radius);
00146
00147 return array(
00148 'min' => new GeoCoordinate($arr['lat']['min'], $arr['lon']['min']),
00149 'max' => new GeoCoordinate($arr['lat']['max'], $arr['lon']['max'])
00150 );
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 public static function boundig_box_of($arr_coordinates) {
00161 $arr_data = array();
00162
00163 foreach($arr_coordinates as $coord) {
00164 if ($coord->is_valid()) {
00165 $arr_data[] = array('lat' => $coord->lat, 'lon' => $coord->lon);
00166 }
00167 }
00168 $arr = GeoCalculator::bounding_box_of($arr_data);
00169 return $arr ? array(
00170 'min' => new GeoCoordinate($arr['lat']['min'], $arr['lon']['min']),
00171 'max' => new GeoCoordinate($arr['lat']['max'], $arr['lon']['max'])
00172 ) : false;
00173 }
00174 }