1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Polyline Implementation |
6
|
|
|
* |
7
|
|
|
* PHP version 5 |
8
|
|
|
* |
9
|
|
|
* @author Marcus Jaschen <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT |
11
|
|
|
* @link https://github.com/mjaschen/phpgeo |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Location; |
15
|
|
|
|
16
|
|
|
use Location\Distance\DistanceInterface; |
17
|
|
|
use Location\Formatter\Polyline\FormatterInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Polyline Implementation |
21
|
|
|
* |
22
|
|
|
* @author Marcus Jaschen <[email protected]> |
23
|
|
|
* @license https://opensource.org/licenses/MIT |
24
|
|
|
* @link https://github.com/mjaschen/phpgeo |
25
|
|
|
*/ |
26
|
|
|
class Polyline implements GeometryInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Coordinate[] |
30
|
|
|
*/ |
31
|
|
|
protected $points = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Coordinate $point |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function addPoint(Coordinate $point) |
39
|
|
|
{ |
40
|
|
|
$this->points[] = $point; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Coordinate[] |
45
|
|
|
*/ |
46
|
|
|
public function getPoints(): array |
47
|
|
|
{ |
48
|
|
|
return $this->points; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function getNumberOfPoints(): int |
55
|
|
|
{ |
56
|
|
|
return count($this->points); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param FormatterInterface $formatter |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function format(FormatterInterface $formatter) |
65
|
|
|
{ |
66
|
|
|
return $formatter->format($this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Line[] |
71
|
|
|
*/ |
72
|
|
|
public function getSegments(): array |
73
|
|
|
{ |
74
|
|
|
$length = count($this->points); |
75
|
|
|
$segments = []; |
76
|
|
|
|
77
|
|
|
if ($length <= 1) { |
78
|
|
|
return $segments; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
for ($i = 1; $i < $length; $i++) { |
|
|
|
|
82
|
|
|
$segments[] = new Line($this->points[$i - 1], $this->points[$i]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $segments; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Calculates the length of the polyline. |
90
|
|
|
* |
91
|
|
|
* @param DistanceInterface $calculator instance of distance calculation class |
92
|
|
|
* |
93
|
|
|
* @return float |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function getLength(DistanceInterface $calculator): float |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$distance = 0.0; |
98
|
|
|
|
99
|
|
|
if (count($this->points) <= 1) { |
100
|
|
|
return $distance; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
foreach ($this->getSegments() as $segment) { |
104
|
|
|
$distance += $segment->getLength($calculator); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $distance; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create a new polyline with reversed order of points, i. e. reversed |
112
|
|
|
* polyline direction. |
113
|
|
|
* |
114
|
|
|
* @return Polyline |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function getReverse(): Polyline |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$reversed = new static(); |
119
|
|
|
|
120
|
|
|
foreach (array_reverse($this->points) as $point) { |
121
|
|
|
$reversed->addPoint($point); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $reversed; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return Bounds |
129
|
|
|
*/ |
130
|
|
|
public function getBounds(): Bounds |
131
|
|
|
{ |
132
|
|
|
$latMin = 90.0; |
133
|
|
|
$latMax = -90.0; |
134
|
|
|
$lngMin = 180.0; |
135
|
|
|
$lngMax = -180.0; |
136
|
|
|
|
137
|
|
|
foreach ($this->points as $point) { |
138
|
|
|
$latMin = min($point->getLat(), $latMin); |
139
|
|
|
$lngMin = min($point->getLng(), $lngMin); |
140
|
|
|
$latMax = max($point->getLat(), $latMax); |
141
|
|
|
$lngMax = max($point->getLng(), $lngMax); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return new Bounds( |
145
|
|
|
new Coordinate($latMax, $lngMin), |
146
|
|
|
new Coordinate($latMin, $lngMax) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.