/
Vertex.php
275 lines (231 loc) · 7.94 KB
/
Vertex.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/*
* This file is part of the Geotools library.
*
* (c) Antoine Corcy <contact@sbin.dk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Geotools\Vertex;
use League\Geotools\AbstractGeotools;
use League\Geotools\Coordinate\Coordinate;
use League\Geotools\Coordinate\CoordinateInterface;
use League\Geotools\Coordinate\Ellipsoid;
/**
* Vertex class
*
* @author Antoine Corcy <contact@sbin.dk>
*/
class Vertex extends AbstractGeotools implements VertexInterface
{
/**
* @var integer
*/
protected $gradient;
/**
* @var integer
*/
protected $ordinateIntercept;
/**
* @var integer
*/
private $precision = 8;
/**
* {@inheritDoc}
*/
public function setFrom(CoordinateInterface $from)
{
$this->from = $from;
if (empty($this->to) || ($this->to->getLatitude() - $this->from->getLatitude() === 0)) {
return $this;
}
$this->gradient = ($this->to->getLongitude() - $this->from->getLongitude()) / ($this->to->getLatitude() - $this->from->getLatitude());
$this->ordinateIntercept = $this->from->getLongitude() - $this->from->getLatitude() * $this->gradient;
return $this;
}
/**
* {@inheritDoc}
*/
public function getFrom()
{
return $this->from;
}
/**
* {@inheritDoc}
*/
public function setTo(CoordinateInterface $to)
{
$this->to = $to;
if (empty($this->from) || ($this->to->getLatitude() - $this->from->getLatitude() === 0)) {
return $this;
}
$this->gradient = ($this->to->getLongitude() - $this->from->getLongitude()) / ($this->to->getLatitude() - $this->from->getLatitude());
$this->ordinateIntercept = $this->from->getLongitude() - $this->from->getLatitude() * $this->gradient;
return $this;
}
/**
* {@inheritDoc}
*/
public function getTo()
{
return $this->to;
}
/**
* {@inheritDoc}
*/
public function getGradient()
{
return $this->gradient;
}
/**
* {@inheritDoc}
*/
public function getOrdinateIntercept()
{
return $this->ordinateIntercept;
}
/**
* @return integer
*/
public function getPrecision()
{
return $this->precision;
}
/**
* @param integer $precision
* @return $this
*/
public function setPrecision($precision)
{
$this->precision = $precision;
return $this;
}
/**
* Returns the initial bearing from the origin coordinate
* to the destination coordinate in degrees.
*
* @return float The initial bearing in degrees
*/
public function initialBearing()
{
Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
$latA = deg2rad($this->from->getLatitude());
$latB = deg2rad($this->to->getLatitude());
$dLng = deg2rad($this->to->getLongitude() - $this->from->getLongitude());
$y = sin($dLng) * cos($latB);
$x = cos($latA) * sin($latB) - sin($latA) * cos($latB) * cos($dLng);
return (float) (rad2deg(atan2($y, $x)) + 360) % 360;
}
/**
* Returns the final bearing from the origin coordinate
* to the destination coordinate in degrees.
*
* @return float The final bearing in degrees
*/
public function finalBearing()
{
Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
$latA = deg2rad($this->to->getLatitude());
$latB = deg2rad($this->from->getLatitude());
$dLng = deg2rad($this->from->getLongitude() - $this->to->getLongitude());
$y = sin($dLng) * cos($latB);
$x = cos($latA) * sin($latB) - sin($latA) * cos($latB) * cos($dLng);
return (float) ((rad2deg(atan2($y, $x)) + 360) % 360 + 180 ) % 360;
}
/**
* Returns the initial cardinal point / direction from the origin coordinate to
* the destination coordinate.
* @see http://en.wikipedia.org/wiki/Cardinal_direction
*
* @return string The initial cardinal point / direction
*/
public function initialCardinal()
{
Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
return $this->cardinalPoints[(integer) round($this->initialBearing() / 22.5)];
}
/**
* Returns the final cardinal point / direction from the origin coordinate to
* the destination coordinate.
* @see http://en.wikipedia.org/wiki/Cardinal_direction
*
* @return string The final cardinal point / direction
*/
public function finalCardinal()
{
Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
return $this->cardinalPoints[(integer) round($this->finalBearing() / 22.5)];
}
/**
* Returns the half-way point / coordinate along a great circle
* path between the origin and the destination coordinates.
*
* @return CoordinateInterface
*/
public function middle()
{
Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
$latA = deg2rad($this->from->getLatitude());
$lngA = deg2rad($this->from->getLongitude());
$latB = deg2rad($this->to->getLatitude());
$lngB = deg2rad($this->to->getLongitude());
$bx = cos($latB) * cos($lngB - $lngA);
$by = cos($latB) * sin($lngB - $lngA);
$lat3 = rad2deg(atan2(sin($latA) + sin($latB), sqrt((cos($latA) + $bx) * (cos($latA) + $bx) + $by * $by)));
$lng3 = rad2deg($lngA + atan2($by, cos($latA) + $bx));
return new Coordinate(array($lat3, $lng3), $this->from->getEllipsoid());
}
/**
* Returns the destination point with a given bearing in degrees travelling along a
* (shortest distance) great circle arc and a distance in meters.
*
* @param integer $bearing The bearing of the origin in degrees.
* @param integer $distance The distance from the origin in meters.
*
* @return CoordinateInterface
*/
public function destination($bearing, $distance)
{
$lat = deg2rad($this->from->getLatitude());
$lng = deg2rad($this->from->getLongitude());
$bearing = deg2rad($bearing);
$endLat = asin(sin($lat) * cos($distance / $this->from->getEllipsoid()->getA()) + cos($lat) *
sin($distance / $this->from->getEllipsoid()->getA()) * cos($bearing));
$endLon = $lng + atan2(sin($bearing) * sin($distance / $this->from->getEllipsoid()->getA()) * cos($lat),
cos($distance / $this->from->getEllipsoid()->getA()) - sin($lat) * sin($endLat));
return new Coordinate(array(rad2deg($endLat), rad2deg($endLon)), $this->from->getEllipsoid());
}
/**
* Returns true if the vertex passed on argument is on the same line as this object
*
* @param Vertex $vertex The vertex to compare
* @return boolean
*/
public function isOnSameLine(Vertex $vertex) {
if (is_null($this->getGradient()) && is_null($vertex->getGradient()) && $this->from->getLongitude() == $vertex->getFrom()->getLongitude()) {
return true;
} elseif (!is_null($this->getGradient()) && !is_null($vertex->getGradient())) {
return (
bccomp($this->getGradient(), $vertex->getGradient(), $this->getPrecision()) === 0
&&
bccomp($this->getOrdinateIntercept(), $vertex->getOrdinateIntercept(), $this->getPrecision()) ===0
);
} else {
return false;
}
}
/**
* Returns the other coordinate who is not the coordinate passed on argument
* @param Coordinate $point
* @return null|Coordinate
*/
public function getOtherCoordinate(CoordinateInterface $coordinate) {
if ($coordinate->isEqual($this->from)) {
return $this->to;
} else if ($coordinate->isEqual($this->to)) {
return $this->from;
}
return null;
}
}