1 | <?php |
||
22 | class Coordinate implements CoordinateInterface, \JsonSerializable |
||
23 | { |
||
24 | /** |
||
25 | * The latitude of the coordinate. |
||
26 | * |
||
27 | * @var double |
||
28 | */ |
||
29 | protected $latitude; |
||
30 | |||
31 | /** |
||
32 | * The longitude of the coordinate. |
||
33 | * |
||
34 | * @var double |
||
35 | */ |
||
36 | protected $longitude; |
||
37 | |||
38 | /** |
||
39 | * The selected ellipsoid. |
||
40 | * |
||
41 | * @var Ellipsoid |
||
42 | */ |
||
43 | protected $ellipsoid; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * The precision to use to compare big numbers |
||
48 | * |
||
49 | * @var integer |
||
50 | */ |
||
51 | private $precision = 8; |
||
52 | |||
53 | /** |
||
54 | * Set the latitude and the longitude of the coordinates into an selected ellipsoid. |
||
55 | * |
||
56 | * @param Address|array|string $coordinates The coordinates. |
||
57 | * @param Ellipsoid $ellipsoid The selected ellipsoid (WGS84 by default). |
||
58 | * |
||
59 | * @throws InvalidArgumentException |
||
60 | */ |
||
61 | 231 | public function __construct($coordinates, Ellipsoid $ellipsoid = null) |
|
62 | { |
||
63 | 231 | if ($coordinates instanceof Address) { |
|
64 | 37 | $this->setLatitude($coordinates->getLatitude()); |
|
65 | 37 | $this->setLongitude($coordinates->getLongitude()); |
|
66 | 194 | } elseif (is_array($coordinates) && 2 === count($coordinates)) { |
|
67 | 41 | $this->setLatitude($coordinates[0]); |
|
68 | 41 | $this->setLongitude($coordinates[1]); |
|
69 | 159 | } elseif (is_string($coordinates)) { |
|
70 | 154 | $this->setFromString($coordinates); |
|
71 | } else { |
||
72 | 5 | throw new InvalidArgumentException( |
|
73 | 5 | 'It should be a string, an array or a class which implements Geocoder\Model\Address !' |
|
74 | ); |
||
75 | } |
||
76 | |||
77 | 200 | $this->ellipsoid = $ellipsoid ?: Ellipsoid::createFromName(Ellipsoid::WGS84); |
|
78 | 200 | } |
|
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | 200 | public function normalizeLatitude($latitude) |
|
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | 200 | public function normalizeLongitude($longitude) |
|
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | 200 | public function setLatitude($latitude) |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 175 | public function getLatitude() |
|
118 | |||
119 | /** |
||
120 | * {@inheritDoc} |
||
121 | */ |
||
122 | 200 | public function setLongitude($longitude) |
|
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | 175 | public function getLongitude() |
|
134 | |||
135 | /** |
||
136 | * {@inheritDoc} |
||
137 | */ |
||
138 | 77 | public function getEllipsoid() |
|
142 | |||
143 | /** |
||
144 | * Creates a valid and acceptable geographic coordinates. |
||
145 | * |
||
146 | * @param string $coordinates |
||
147 | * |
||
148 | * @throws InvalidArgumentException |
||
149 | */ |
||
150 | 157 | public function setFromString($coordinates) |
|
164 | |||
165 | /** |
||
166 | * @return integer |
||
167 | */ |
||
168 | 3 | public function getPrecision() |
|
172 | |||
173 | /** |
||
174 | * @param integer $precision |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setPrecision($precision) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Converts a valid and acceptable geographic coordinates to decimal degrees coordinate. |
||
187 | * |
||
188 | * @param string $coordinates A valid and acceptable geographic coordinates. |
||
189 | * |
||
190 | * @return array An array of coordinate in decimal degree. |
||
191 | * |
||
192 | * @throws InvalidArgumentException |
||
193 | * |
||
194 | * @see http://en.wikipedia.org/wiki/Geographic_coordinate_conversion |
||
195 | */ |
||
196 | 156 | private function toDecimalDegrees($coordinates) |
|
197 | { |
||
198 | // 40.446195, -79.948862 |
||
199 | 156 | if (preg_match('/(\-?[0-9]{1,2}\.?\d*)[, ] ?(\-?[0-9]{1,3}\.?\d*)$/', $coordinates, $match)) { |
|
200 | 45 | return array($match[1], $match[2]); |
|
201 | } |
||
202 | |||
203 | // 40° 26.7717, -79° 56.93172 |
||
204 | 111 | if (preg_match('/(\-?[0-9]{1,2})\D+([0-9]{1,2}\.?\d*)[, ] ?(\-?[0-9]{1,3})\D+([0-9]{1,2}\.?\d*)$/i', |
|
205 | $coordinates, $match)) { |
||
206 | return array( |
||
207 | 55 | $match[1] + $match[2] / 60, |
|
208 | 55 | $match[3] < 0 |
|
209 | 55 | ? $match[3] - $match[4] / 60 |
|
210 | 55 | : $match[3] + $match[4] / 60 |
|
211 | ); |
||
212 | } |
||
213 | |||
214 | // 40.446195N 79.948862W |
||
215 | 94 | if (preg_match('/([0-9]{1,2}\.?\d*)\D*([ns]{1})[, ] ?([0-9]{1,3}\.?\d*)\D*([we]{1})$/i', $coordinates, $match)) { |
|
216 | return array( |
||
217 | 6 | 'N' === strtoupper($match[2]) ? $match[1] : -$match[1], |
|
218 | 6 | 'E' === strtoupper($match[4]) ? $match[3] : -$match[3] |
|
219 | ); |
||
220 | } |
||
221 | |||
222 | // 40°26.7717S 79°56.93172E |
||
223 | // 25°59.86′N,21°09.81′W |
||
224 | 88 | if (preg_match('/([0-9]{1,2})\D+([0-9]{1,2}\.?\d*)\D*([ns]{1})[, ] ?([0-9]{1,3})\D+([0-9]{1,2}\.?\d*)\D*([we]{1})$/i', |
|
225 | $coordinates, $match)) { |
||
226 | 3 | $latitude = $match[1] + $match[2] / 60; |
|
227 | 3 | $longitude = $match[4] + $match[5] / 60; |
|
228 | |||
229 | return array( |
||
230 | 3 | 'N' === strtoupper($match[3]) ? $latitude : -$latitude, |
|
231 | 3 | 'E' === strtoupper($match[6]) ? $longitude : -$longitude |
|
232 | ); |
||
233 | } |
||
234 | |||
235 | // 40:26:46N, 079:56:55W |
||
236 | // 40:26:46.302N 079:56:55.903W |
||
237 | // 40°26′47″N 079°58′36″W |
||
238 | // 40d 26′ 47″ N 079d 58′ 36″ W |
||
239 | 85 | if (preg_match('/([0-9]{1,2})\D+([0-9]{1,2})\D+([0-9]{1,2}\.?\d*)\D*([ns]{1})[, ] ?([0-9]{1,3})\D+([0-9]{1,2})\D+([0-9]{1,2}\.?\d*)\D*([we]{1})$/i', |
|
240 | $coordinates, $match)) { |
||
241 | 58 | $latitude = $match[1] + ($match[2] * 60 + $match[3]) / 3600; |
|
242 | 58 | $longitude = $match[5] + ($match[6] * 60 + $match[7]) / 3600; |
|
243 | |||
244 | return array( |
||
245 | 58 | 'N' === strtoupper($match[4]) ? $latitude : -$latitude, |
|
246 | 58 | 'E' === strtoupper($match[8]) ? $longitude : -$longitude |
|
247 | ); |
||
248 | } |
||
249 | |||
250 | 27 | throw new InvalidArgumentException( |
|
251 | 27 | 'It should be a valid and acceptable ways to write geographic coordinates !' |
|
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * {@inheritDoc} |
||
257 | */ |
||
258 | public function jsonSerialize() |
||
262 | |||
263 | /** |
||
264 | * Returns a boolean determining coordinates equality |
||
265 | * @param Coordinate $coordinate |
||
266 | * @return boolean |
||
267 | */ |
||
268 | 3 | public function isEqual(Coordinate $coordinate) { |
|
271 | } |
||
272 |