-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Coordinate.php
160 lines (139 loc) · 3.38 KB
/
Coordinate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Location;
use Location\Distance\DistanceInterface;
use Location\Formatter\Coordinate\FormatterInterface;
/**
* Coordinate Implementation
*
* @author Marcus Jaschen <mjaschen@gmail.com>
*/
class Coordinate implements GeometryInterface
{
/**
* @var float
*/
protected $lat;
/**
* @var float
*/
protected $lng;
/**
* @var Ellipsoid
*/
protected $ellipsoid;
/**
* @param float $lat -90.0 .. +90.0
* @param float $lng -180.0 .. +180.0
* @param Ellipsoid $ellipsoid if omitted, WGS-84 is used
*
* @throws \InvalidArgumentException
*/
public function __construct(float $lat, float $lng, Ellipsoid $ellipsoid = null)
{
if (! $this->isValidLatitude($lat)) {
throw new \InvalidArgumentException("Latitude value must be numeric -90.0 .. +90.0 (given: {$lat})");
}
if (! $this->isValidLongitude($lng)) {
throw new \InvalidArgumentException("Longitude value must be numeric -180.0 .. +180.0 (given: {$lng})");
}
$this->lat = (float)$lat;
$this->lng = (float)$lng;
if ($ellipsoid !== null) {
$this->ellipsoid = $ellipsoid;
return;
}
$this->ellipsoid = Ellipsoid::createDefault();
}
/**
* @return float
*/
public function getLat(): float
{
return $this->lat;
}
/**
* @return float
*/
public function getLng(): float
{
return $this->lng;
}
/**
* Returns an array containing the point
*
* @return array
*/
public function getPoints(): array
{
return [$this];
}
/**
* @return Ellipsoid
*/
public function getEllipsoid(): Ellipsoid
{
return $this->ellipsoid;
}
/**
* Calculates the distance between the given coordinate
* and this coordinate.
*
* @param Coordinate $coordinate
* @param DistanceInterface $calculator instance of distance calculation class
*
* @return float
*/
public function getDistance(Coordinate $coordinate, DistanceInterface $calculator): float
{
return $calculator->getDistance($this, $coordinate);
}
/**
* @param FormatterInterface $formatter
*
* @return mixed
*/
public function format(FormatterInterface $formatter)
{
return $formatter->format($this);
}
/**
* Validates latitude
*
* @param float $latitude
*
* @return bool
*/
protected function isValidLatitude(float $latitude): bool
{
return $this->isNumericInBounds($latitude, -90.0, 90.0);
}
/**
* Validates longitude
*
* @param float $longitude
*
* @return bool
*/
protected function isValidLongitude(float $longitude): bool
{
return $this->isNumericInBounds($longitude, -180.0, 180.0);
}
/**
* Checks if the given value is (1) numeric, and (2) between lower
* and upper bounds (including the bounds values).
*
* @param float $value
* @param float $lower
* @param float $upper
*
* @return bool
*/
protected function isNumericInBounds(float $value, float $lower, float $upper): bool
{
if ($value < $lower || $value > $upper) {
return false;
}
return true;
}
}