forked from mjaschen/phpgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polyline.php
197 lines (167 loc) · 4.42 KB
/
Polyline.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
<?php
declare(strict_types=1);
namespace Location;
use Location\Distance\DistanceInterface;
use Location\Exception\InvalidGeometryException;
use Location\Formatter\Polyline\FormatterInterface;
/**
* Polyline Implementation
*
* @author Marcus Jaschen <[email protected]>
*/
class Polyline implements GeometryInterface
{
use GetBoundsTrait;
/**
* @var array<Coordinate>
*/
protected $points = [];
/**
* @param Coordinate $point
*
* @return void
*/
public function addPoint(Coordinate $point): void
{
$this->points[] = $point;
}
/**
* @param array<Coordinate> $points
*/
public function addPoints(array $points): void
{
foreach ($points as $point) {
$this->addPoint($point);
}
}
/**
* Adds an unique point to the polyline. A maximum allowed distance for
* same point comparison can be provided. Default allowed distance
* deviation is 0.001 meters (1 millimeter).
*
* @param Coordinate $point
* @param float $allowedDistance
*
* @return void
*/
public function addUniquePoint(Coordinate $point, float $allowedDistance = .001): void
{
if ($this->containsPoint($point, $allowedDistance)) {
return;
}
$this->addPoint($point);
}
/**
* @return array<Coordinate>
*/
public function getPoints(): array
{
return $this->points;
}
/**
* @return int
*/
public function getNumberOfPoints(): int
{
return count($this->points);
}
/**
* @param Coordinate $point
* @param float $allowedDistance
*
* @return bool
*/
public function containsPoint(Coordinate $point, float $allowedDistance = .001): bool
{
foreach ($this->points as $existingPoint) {
if ($existingPoint->hasSameLocation($point, $allowedDistance)) {
return true;
}
}
return false;
}
/**
* @param FormatterInterface $formatter
*
* @return string
*/
public function format(FormatterInterface $formatter): string
{
return $formatter->format($this);
}
/**
* @return array<Line>
*/
public function getSegments(): array
{
$length = count($this->points);
$segments = [];
if ($length <= 1) {
return $segments;
}
for ($i = 1; $i < $length; $i++) {
$segments[] = new Line($this->points[$i - 1], $this->points[$i]);
}
return $segments;
}
/**
* Calculates the length of the polyline.
*
* @param DistanceInterface $calculator instance of distance calculation class
*
* @return float
*/
public function getLength(DistanceInterface $calculator): float
{
$distance = 0.0;
if (count($this->points) <= 1) {
return $distance;
}
foreach ($this->getSegments() as $segment) {
$distance += $segment->getLength($calculator);
}
return $distance;
}
/**
* Create a new polyline with reversed order of points, i. e. reversed
* polyline direction.
*
* @return Polyline
*/
public function getReverse(): Polyline
{
$reversed = new self();
foreach (array_reverse($this->points) as $point) {
$reversed->addPoint($point);
}
return $reversed;
}
/**
* Returns the point which is defined by the avarages of all
* latitude/longitude values.
*
* This currently only works for polylines which don't cross the dateline at
* 180/-180 degrees longitude.
*
* @return Coordinate
*
* @throws InvalidGeometryException when the polyline doesn't contain any points.
*/
public function getAveragePoint(): Coordinate
{
$latitude = 0.0;
$longitude = 0.0;
$numberOfPoints = count($this->points);
if ($this->getNumberOfPoints() === 0) {
throw new InvalidGeometryException('Polyline doesn\'t contain points', 9464188927);
}
foreach ($this->points as $point) {
// @var Coordinate $point
$latitude += $point->getLat();
$longitude += $point->getLng();
}
$latitude /= $numberOfPoints;
$longitude /= $numberOfPoints;
return new Coordinate($latitude, $longitude);
}
}