1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Domain\Geolocation; |
12
|
|
|
|
13
|
|
|
use Cubiche\Domain\Model\ValueObjectInterface; |
14
|
|
|
use Cubiche\Domain\System\Real; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Coordinate Class. |
18
|
|
|
* |
19
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Coordinate implements ValueObjectInterface |
22
|
|
|
{ |
23
|
|
|
const EARTH_RADIUS = 6378136.0; //m |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Latitude |
27
|
|
|
*/ |
28
|
|
|
protected $latitude; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Longitude |
32
|
|
|
*/ |
33
|
|
|
protected $longitude; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param float $latitude |
37
|
|
|
* @param float $longitude |
38
|
|
|
* |
39
|
|
|
* @return Coordinate |
40
|
|
|
*/ |
41
|
|
|
public static function fromLatLng($latitude, $longitude) |
42
|
|
|
{ |
43
|
|
|
return new static(new Latitude($latitude), new Longitude($longitude)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Latitude $latitude |
48
|
|
|
* @param Longitude $longitude |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Latitude $latitude, Longitude $longitude) |
51
|
|
|
{ |
52
|
|
|
$this->latitude = $latitude; |
53
|
|
|
$this->longitude = $longitude; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Latitude |
58
|
|
|
*/ |
59
|
|
|
public function latitude() |
60
|
|
|
{ |
61
|
|
|
return $this->latitude; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Longitude |
66
|
|
|
*/ |
67
|
|
|
public function longitude() |
68
|
|
|
{ |
69
|
|
|
return $this->longitude; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Coordinate $coordinate |
74
|
|
|
* @param DistanceUnit $unit |
75
|
|
|
*/ |
76
|
|
|
public function distance(Coordinate $coordinate, DistanceUnit $unit = null) |
77
|
|
|
{ |
78
|
|
|
$latA = \deg2rad($this->latitude()); |
79
|
|
|
$lngA = \deg2rad($this->longitude()); |
80
|
|
|
$latB = \deg2rad($coordinate->latitude()); |
81
|
|
|
$lngB = \deg2rad($coordinate->longitude()); |
82
|
|
|
$degrees = \acos(\sin($latA) * \sin($latB) + \cos($latA) * \cos($latB) * \cos($lngB - $lngA)); |
83
|
|
|
$unit = $unit === null ? DistanceUnit::METER() : $unit; |
84
|
|
|
|
85
|
|
|
return (new Distance(Real::fromNative($degrees * self::EARTH_RADIUS), DistanceUnit::METER()))->in($unit); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function equals($other) |
92
|
|
|
{ |
93
|
|
|
return $other instanceof self && |
94
|
|
|
$this->latitude()->equals($other->latitude()) && |
95
|
|
|
$this->longitude()->equals($other->longitude()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function hashCode() |
102
|
|
|
{ |
103
|
|
|
return \sprintf('%F-%F', $this->latitude()->toNative(), $this->longitude()->toNative()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function __toString() |
110
|
|
|
{ |
111
|
|
|
return \sprintf('[%F, %F]', $this->latitude()->toNative(), $this->longitude()->toNative()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|