1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Location; |
5
|
|
|
|
6
|
|
|
use Location\Distance\DistanceInterface; |
7
|
|
|
use Location\Formatter\Polyline\FormatterInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Polyline Implementation |
11
|
|
|
* |
12
|
|
|
* @author Marcus Jaschen <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Polyline implements GeometryInterface |
15
|
|
|
{ |
16
|
|
|
use GetBoundsTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Coordinate[] |
20
|
|
|
*/ |
21
|
|
|
protected $points = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Coordinate $point |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function addPoint(Coordinate $point) |
29
|
|
|
{ |
30
|
|
|
$this->points[] = $point; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add unique point |
35
|
|
|
* |
36
|
|
|
* @param Coordinate $pointToAdd |
37
|
|
|
*/ |
38
|
|
|
public function addUniquePoint(Coordinate $pointToAdd) |
39
|
|
|
{ |
40
|
|
|
foreach($this->points as $point){ |
41
|
|
|
/* @var $point Coordinate */ |
42
|
|
|
if(($pointToAdd->getLat() == $point->getLat()) && ($pointToAdd->getLng() == $point->getLng())) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->points[] = $pointToAdd; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function getPoints(): array |
54
|
|
|
{ |
55
|
|
|
return $this->points; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
public function getNumberOfPoints(): int |
62
|
|
|
{ |
63
|
|
|
return count($this->points); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param FormatterInterface $formatter |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function format(FormatterInterface $formatter): string |
72
|
|
|
{ |
73
|
|
|
return $formatter->format($this); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Line[] |
78
|
|
|
*/ |
79
|
|
|
public function getSegments(): array |
80
|
|
|
{ |
81
|
|
|
$length = count($this->points); |
82
|
|
|
$segments = []; |
83
|
|
|
|
84
|
|
|
if ($length <= 1) { |
85
|
|
|
return $segments; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
for ($i = 1; $i < $length; $i++) { |
|
|
|
|
89
|
|
|
$segments[] = new Line($this->points[$i - 1], $this->points[$i]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $segments; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Calculates the length of the polyline. |
97
|
|
|
* |
98
|
|
|
* @param DistanceInterface $calculator instance of distance calculation class |
99
|
|
|
* |
100
|
|
|
* @return float |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function getLength(DistanceInterface $calculator): float |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$distance = 0.0; |
105
|
|
|
|
106
|
|
|
if (count($this->points) <= 1) { |
107
|
|
|
return $distance; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
foreach ($this->getSegments() as $segment) { |
111
|
|
|
$distance += $segment->getLength($calculator); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $distance; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Create a new polyline with reversed order of points, i. e. reversed |
119
|
|
|
* polyline direction. |
120
|
|
|
* |
121
|
|
|
* @return Polyline |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function getReverse(): Polyline |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$reversed = new static(); |
126
|
|
|
|
127
|
|
|
foreach (array_reverse($this->points) as $point) { |
128
|
|
|
$reversed->addPoint($point); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $reversed; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Coordinate|null |
136
|
|
|
*/ |
137
|
|
|
public function getMiddlePoint() |
138
|
|
|
{ |
139
|
|
|
$lat = 0.0; |
140
|
|
|
$lng = 0.0; |
141
|
|
|
$numberOfPoints = count($this->points); |
142
|
|
|
|
143
|
|
|
if($numberOfPoints < 1) { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
foreach($this->points as $point){ |
148
|
|
|
/* @var $point Coordinate */ |
149
|
|
|
$lat += $point->getLat(); |
150
|
|
|
$lng += $point->getLng(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$lat /= $numberOfPoints; |
154
|
|
|
$lng /= $numberOfPoints; |
155
|
|
|
|
156
|
|
|
return new Coordinate($lat, $lng); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.