1 | <?php |
||
2 | |||
3 | namespace geoPHP\Geometry; |
||
4 | |||
5 | use geoPHP\geoPHP; |
||
6 | |||
7 | /** |
||
8 | * MultiGeometry is an abstract collection of geometries |
||
9 | * |
||
10 | * @package GeoPHPGeometry |
||
11 | */ |
||
12 | abstract class MultiGeometry extends Collection |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @param Geometry[] $components |
||
17 | * @param bool $allowEmptyComponents |
||
18 | * @param string $allowedComponentType |
||
19 | */ |
||
20 | public function __construct( |
||
21 | array $components = [], |
||
22 | bool $allowEmptyComponents = true, |
||
23 | string $allowedComponentType = Geometry::class |
||
24 | ) { |
||
25 | parent::__construct($components, $allowEmptyComponents, $allowedComponentType); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @return bool |
||
30 | */ |
||
31 | public function isSimple(): bool |
||
32 | { |
||
33 | $geosObj = $this->getGeos(); |
||
34 | if (is_object($geosObj)) { |
||
35 | // @codeCoverageIgnoreStart |
||
36 | /** @noinspection PhpUndefinedMethodInspection */ |
||
37 | return $geosObj->isSimple(); |
||
38 | // @codeCoverageIgnoreEnd |
||
39 | } |
||
40 | |||
41 | // A collection is simple if all it's components are simple |
||
42 | foreach ($this->components as $component) { |
||
43 | if (!$component->isSimple()) { |
||
44 | return false; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | return true; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function isValid(): bool |
||
55 | { |
||
56 | if ($this->getGeos()) { |
||
57 | return parent::isValid(); |
||
58 | } |
||
59 | |||
60 | // A collection is valid if all it's components are valid |
||
61 | foreach ($this->components as $component) { |
||
62 | if (!$component->isValid()) { |
||
63 | return false; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return true; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns the boundary, or an empty geometry of appropriate dimension if this Geometry is empty. |
||
72 | * By default, the boundary of a collection is the boundary of it's components. |
||
73 | * In the case of zero-dimensional geometries, an empty GeometryCollection is returned. |
||
74 | * |
||
75 | * @return Geometry|GeometryCollection |
||
76 | */ |
||
77 | public function boundary(): Geometry |
||
78 | { |
||
79 | if ($this->isEmpty()) { |
||
80 | return new GeometryCollection(); |
||
81 | } |
||
82 | |||
83 | $geosObj = $this->getGeos(); |
||
84 | if (is_object($geosObj)) { |
||
85 | // @codeCoverageIgnoreStart |
||
86 | /** @noinspection PhpUndefinedMethodInspection */ |
||
87 | /** @phpstan-ignore-next-line */ |
||
88 | return $geosObj->boundary(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
89 | // @codeCoverageIgnoreEnd |
||
90 | } |
||
91 | |||
92 | $componentsBoundaries = []; |
||
93 | foreach ($this->components as $component) { |
||
94 | $componentsBoundaries[] = $component->boundary(); |
||
95 | } |
||
96 | return geoPHP::buildGeometry($componentsBoundaries); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Returns the total area of this collection. |
||
101 | * |
||
102 | * @return float |
||
103 | */ |
||
104 | public function getArea(): float |
||
105 | { |
||
106 | $geosObj = $this->getGeos(); |
||
107 | if (is_object($geosObj)) { |
||
108 | // @codeCoverageIgnoreStart |
||
109 | /** @noinspection PhpUndefinedMethodInspection */ |
||
110 | return (float) $geosObj->area(); |
||
111 | // @codeCoverageIgnoreEnd |
||
112 | } |
||
113 | |||
114 | $area = 0.0; |
||
115 | foreach ($this->components as $component) { |
||
116 | $area += $component->getArea(); |
||
117 | } |
||
118 | return (float) $area; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns the length of this Collection in its associated spatial reference. |
||
123 | * Eg. if Geometry is in geographical coordinate system it returns the length in degrees |
||
124 | * |
||
125 | * @return float |
||
126 | */ |
||
127 | public function getLength(): float |
||
128 | { |
||
129 | $length = 0.0; |
||
130 | foreach ($this->components as $component) { |
||
131 | $length += $component->getLength(); |
||
132 | } |
||
133 | return $length; |
||
134 | } |
||
135 | |||
136 | public function length3D(): float |
||
137 | { |
||
138 | $length = 0.0; |
||
139 | foreach ($this->components as $component) { |
||
140 | $length += $component->length3D(); |
||
141 | } |
||
142 | return $length; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns the degree based Geometry' length in meters |
||
147 | * |
||
148 | * @param float|int $radius Default is the semi-major axis of WGS84. |
||
149 | * @return float the length in meters |
||
150 | */ |
||
151 | public function greatCircleLength($radius = geoPHP::EARTH_WGS84_SEMI_MAJOR_AXIS): float |
||
152 | { |
||
153 | $length = 0.0; |
||
154 | foreach ($this->components as $component) { |
||
155 | $length += $component->greatCircleLength($radius); |
||
156 | } |
||
157 | return $length; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return float sum haversine length of all components |
||
162 | */ |
||
163 | public function haversineLength(): float |
||
164 | { |
||
165 | $length = 0.0; |
||
166 | foreach ($this->components as $component) { |
||
167 | $length += $component->haversineLength(); |
||
168 | } |
||
169 | return $length; |
||
170 | } |
||
171 | |||
172 | public function minimumZ() |
||
173 | { |
||
174 | $min = PHP_INT_MAX; |
||
175 | foreach ($this->components as $component) { |
||
176 | $componentMin = $component->minimumZ(); |
||
177 | if (null !== $componentMin && $componentMin < $min) { |
||
178 | $min = $componentMin; |
||
179 | } |
||
180 | } |
||
181 | return $min !== PHP_INT_MAX ? $min : null; |
||
182 | } |
||
183 | |||
184 | public function maximumZ() |
||
185 | { |
||
186 | $max = PHP_INT_MIN; |
||
187 | foreach ($this->components as $component) { |
||
188 | $componentMax = $component->maximumZ(); |
||
189 | if ($componentMax > $max) { |
||
190 | $max = $componentMax; |
||
191 | } |
||
192 | } |
||
193 | return $max !== PHP_INT_MIN ? $max : null; |
||
194 | } |
||
195 | |||
196 | public function zDifference() |
||
197 | { |
||
198 | $startPoint = $this->startPoint(); |
||
0 ignored issues
–
show
Are you sure the assignment to
$startPoint is correct as $this->startPoint() targeting geoPHP\Geometry\Geometry::startPoint() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
199 | $endPoint = $this->endPoint(); |
||
0 ignored issues
–
show
Are you sure the assignment to
$endPoint is correct as $this->endPoint() targeting geoPHP\Geometry\Geometry::endPoint() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
200 | if ($startPoint && $endPoint && $startPoint->hasZ() && $endPoint->hasZ()) { |
||
0 ignored issues
–
show
|
|||
201 | return abs($startPoint->getZ() - $endPoint->getZ()); |
||
202 | } |
||
203 | |||
204 | return null; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * |
||
209 | * @param int|float $verticalTolerance |
||
210 | * @return int|float|null |
||
211 | */ |
||
212 | public function elevationGain($verticalTolerance = 0) |
||
213 | { |
||
214 | $gain = null; |
||
215 | foreach ($this->components as $component) { |
||
216 | $gain += $component->elevationGain($verticalTolerance); |
||
217 | } |
||
218 | return $gain; |
||
219 | } |
||
220 | |||
221 | public function elevationLoss($verticalTolerance = 0) |
||
222 | { |
||
223 | $loss = null; |
||
224 | foreach ($this->components as $component) { |
||
225 | $loss += $component->elevationLoss($verticalTolerance); |
||
226 | } |
||
227 | return $loss; |
||
228 | } |
||
229 | |||
230 | public function minimumM() |
||
231 | { |
||
232 | $min = PHP_INT_MAX; |
||
233 | foreach ($this->components as $component) { |
||
234 | $componentMin = $component->minimumM(); |
||
235 | if ($componentMin < $min) { |
||
236 | $min = $componentMin; |
||
237 | } |
||
238 | } |
||
239 | return $min !== PHP_INT_MAX ? $min : null; |
||
240 | } |
||
241 | |||
242 | public function maximumM() |
||
243 | { |
||
244 | $max = PHP_INT_MIN; |
||
245 | foreach ($this->components as $component) { |
||
246 | $componentMax = $component->maximumM(); |
||
247 | if ($componentMax > $max) { |
||
248 | $max = $componentMax; |
||
249 | } |
||
250 | } |
||
251 | return $max !== PHP_INT_MIN ? $max : null; |
||
252 | } |
||
253 | |||
254 | public function isClosed(): bool |
||
255 | { |
||
256 | return true; |
||
257 | } |
||
258 | } |
||
259 |