Completed
Push — master ( 8dc1e9...7ba148 )
by Antoine
23:27 queued 08:42
created

Vertex::getOtherCoordinate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the Geotools library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\Geotools\Vertex;
13
14
use League\Geotools\AbstractGeotools;
15
use League\Geotools\Coordinate\Coordinate;
16
use League\Geotools\Coordinate\CoordinateInterface;
17
use League\Geotools\Coordinate\Ellipsoid;
18
19
/**
20
 * Vertex class
21
 *
22
 * @author Antoine Corcy <[email protected]>
23
 */
24
class Vertex extends AbstractGeotools implements VertexInterface
25
{
26
    /**
27
     * @var integer
28
     */
29
    protected $gradient;
30
31
    /**
32
     * @var integer
33
     */
34
    protected $ordinateIntercept;
35
36
    /**
37
     * @var integer
38
     */
39
    private $precision = 8;
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 51
    public function setFrom(CoordinateInterface $from)
45
    {
46 51
        $this->from = $from;
47
48 51
        if (empty($this->to) || ($this->to->getLatitude() - $this->from->getLatitude() === 0)) {
49 51
            return $this;
50
        }
51
52
        $this->gradient = ($this->to->getLongitude() - $this->from->getLongitude()) / ($this->to->getLatitude() - $this->from->getLatitude());
0 ignored issues
show
Documentation Bug introduced by
The property $gradient was declared of type integer, but ($this->to->getLongitude...s->from->getLatitude()) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
53
        $this->ordinateIntercept = $this->from->getLongitude() - $this->from->getLatitude() * $this->gradient;
0 ignored issues
show
Documentation Bug introduced by
The property $ordinateIntercept was declared of type integer, but $this->from->getLongitud...ude() * $this->gradient is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
54
        return $this;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 1
    public function getFrom()
61
    {
62 1
        return $this->from;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 42
    public function setTo(CoordinateInterface $to)
69
    {
70 42
        $this->to = $to;
71
72 42
        if (empty($this->from) || ($this->to->getLatitude() - $this->from->getLatitude() === 0)) {
73 6
            return $this;
74
        }
75
76 36
        $this->gradient = ($this->to->getLongitude() - $this->from->getLongitude()) / ($this->to->getLatitude() - $this->from->getLatitude());
0 ignored issues
show
Documentation Bug introduced by
The property $gradient was declared of type integer, but ($this->to->getLongitude...s->from->getLatitude()) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
77 36
        $this->ordinateIntercept = $this->from->getLongitude() - $this->from->getLatitude() * $this->gradient;
0 ignored issues
show
Documentation Bug introduced by
The property $ordinateIntercept was declared of type integer, but $this->from->getLongitud...ude() * $this->gradient is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
78
79 36
        return $this;
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 1
    public function getTo()
86
    {
87 1
        return $this->to;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 5
    public function getGradient()
94
    {
95 5
        return $this->gradient;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 2
    public function getOrdinateIntercept()
102
    {
103 2
        return $this->ordinateIntercept;
104
    }
105
106
    /**
107
     * @return integer
108
     */
109 5
    public function getPrecision()
110
    {
111 5
        return $this->precision;
112
    }
113
114
    /**
115
     * @param  integer $precision
116
     * @return $this
117
     */
118
    public function setPrecision($precision)
119
    {
120
        $this->precision = $precision;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Returns the initial bearing from the origin coordinate
127
     * to the destination coordinate in degrees.
128
     *
129
     * @return float The initial bearing in degrees
130
     */
131 14
    public function initialBearing()
132
    {
133 14
        Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
134
135 14
        $latA = deg2rad($this->from->getLatitude());
136 14
        $latB = deg2rad($this->to->getLatitude());
137 14
        $dLng = deg2rad($this->to->getLongitude() - $this->from->getLongitude());
138
139 14
        $y = sin($dLng) * cos($latB);
140 14
        $x = cos($latA) * sin($latB) - sin($latA) * cos($latB) * cos($dLng);
141
142 14
        return (float) (rad2deg(atan2($y, $x)) + 360) % 360;
143
    }
144
145
    /**
146
     * Returns the final bearing from the origin coordinate
147
     * to the destination coordinate in degrees.
148
     *
149
     * @return float The final bearing in degrees
150
     */
151 14
    public function finalBearing()
152
    {
153 14
        Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
154
155 14
        $latA = deg2rad($this->to->getLatitude());
156 14
        $latB = deg2rad($this->from->getLatitude());
157 14
        $dLng = deg2rad($this->from->getLongitude() - $this->to->getLongitude());
158
159 14
        $y = sin($dLng) * cos($latB);
160 14
        $x = cos($latA) * sin($latB) - sin($latA) * cos($latB) * cos($dLng);
161
162 14
        return (float) ((rad2deg(atan2($y, $x)) + 360) % 360 + 180 ) % 360;
163
    }
164
165
    /**
166
     * Returns the initial cardinal point / direction from the origin coordinate to
167
     * the destination coordinate.
168
     * @see http://en.wikipedia.org/wiki/Cardinal_direction
169
     *
170
     * @return string The initial cardinal point / direction
171
     */
172 7
    public function initialCardinal()
173
    {
174 7
        Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
175
176 7
        return $this->cardinalPoints[(integer) round($this->initialBearing() / 22.5)];
177
    }
178
179
    /**
180
     * Returns the final cardinal point / direction from the origin coordinate to
181
     * the destination coordinate.
182
     * @see http://en.wikipedia.org/wiki/Cardinal_direction
183
     *
184
     * @return string The final cardinal point / direction
185
     */
186 7
    public function finalCardinal()
187
    {
188 7
        Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
189
190 7
        return $this->cardinalPoints[(integer) round($this->finalBearing() / 22.5)];
191
    }
192
193
    /**
194
     * Returns the half-way point / coordinate along a great circle
195
     * path between the origin and the destination coordinates.
196
     *
197
     * @return CoordinateInterface
198
     */
199 7
    public function middle()
200
    {
201 7
        Ellipsoid::checkCoordinatesEllipsoid($this->from, $this->to);
202
203 7
        $latA = deg2rad($this->from->getLatitude());
204 7
        $lngA = deg2rad($this->from->getLongitude());
205 7
        $latB = deg2rad($this->to->getLatitude());
206 7
        $lngB = deg2rad($this->to->getLongitude());
207
208 7
        $bx = cos($latB) * cos($lngB - $lngA);
209 7
        $by = cos($latB) * sin($lngB - $lngA);
210
211 7
        $lat3 = rad2deg(atan2(sin($latA) + sin($latB), sqrt((cos($latA) + $bx) * (cos($latA) + $bx) + $by * $by)));
212 7
        $lng3 = rad2deg($lngA + atan2($by, cos($latA) + $bx));
213
214 7
        return new Coordinate(array($lat3, $lng3), $this->from->getEllipsoid());
215
    }
216
217
    /**
218
     * Returns the destination point with a given bearing in degrees travelling along a
219
     * (shortest distance) great circle arc and a distance in meters.
220
     *
221
     * @param integer $bearing  The bearing of the origin in degrees.
222
     * @param integer $distance The distance from the origin in meters.
223
     *
224
     * @return CoordinateInterface
225
     */
226 9
    public function destination($bearing, $distance)
227
    {
228 9
        $lat = deg2rad($this->from->getLatitude());
229 9
        $lng = deg2rad($this->from->getLongitude());
230
231 9
        $bearing = deg2rad($bearing);
232
233 9
        $endLat = asin(sin($lat) * cos($distance / $this->from->getEllipsoid()->getA()) + cos($lat) *
234 9
            sin($distance / $this->from->getEllipsoid()->getA()) * cos($bearing));
235 9
        $endLon = $lng + atan2(sin($bearing) * sin($distance / $this->from->getEllipsoid()->getA()) * cos($lat),
236 9
            cos($distance / $this->from->getEllipsoid()->getA()) - sin($lat) * sin($endLat));
237
238 9
        return new Coordinate(array(rad2deg($endLat), rad2deg($endLon)), $this->from->getEllipsoid());
239
    }
240
241
    /**
242
     * Returns true if the vertex passed on argument is on the same line as this object
243
     *
244
     * @param  Vertex  $vertex The vertex to compare
245
     * @return boolean
246
     */
247 5
    public function isOnSameLine(Vertex $vertex) {
248 5
        if (is_null($this->getGradient()) && is_null($vertex->getGradient()) && $this->from->getLongitude() == $vertex->getFrom()->getLongitude()) {
249
            return true;
250 5
        } elseif (!is_null($this->getGradient()) && !is_null($vertex->getGradient())) {
251
            return (
252 5
                bccomp($this->getGradient(), $vertex->getGradient(), $this->getPrecision()) === 0
253 5
                &&
254 2
                bccomp($this->getOrdinateIntercept(), $vertex->getOrdinateIntercept(), $this->getPrecision()) ===0
255 5
            );
256
        } else {
257
            return false;
258
        }
259
    }
260
261
    /**
262
     * Returns the other coordinate who is not the coordinate passed on argument
263
     * @param  Coordinate $point
0 ignored issues
show
Bug introduced by
There is no parameter named $point. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
264
     * @return null|Coordinate
265
     */
266
    public function getOtherCoordinate(CoordinateInterface $coordinate) {
267
        if ($coordinate->isEqual($this->from)) {
268
            return $this->to;
269
        } else if ($coordinate->isEqual($this->to)) {
270
            return $this->from;
271
        }
272
        return null;
273
    }
274
275
}
276