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; |
13
|
|
|
|
14
|
|
|
use League\Geotools\BoundingBox\BoundingBox; |
15
|
|
|
use League\Geotools\BoundingBox\BoundingBoxInterface; |
16
|
|
|
use League\Geotools\Coordinate\Coordinate; |
17
|
|
|
use League\Geotools\Coordinate\CoordinateCollection; |
18
|
|
|
use League\Geotools\Coordinate\Ellipsoid; |
19
|
|
|
use League\Geotools\Exception\InvalidArgumentException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Rémi San <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class GeometryCollection extends ArrayCollection implements GeometryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Ellipsoid |
28
|
|
|
*/ |
29
|
|
|
private $ellipsoid; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
private $precision; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* CoordinateCollection constructor. |
38
|
|
|
* |
39
|
|
|
* @param GeometryInterface[] $geometries |
40
|
|
|
* @param Ellipsoid $ellipsoid |
41
|
|
|
*/ |
42
|
20 |
|
public function __construct(array $geometries = array(), Ellipsoid $ellipsoid = null) |
43
|
|
|
{ |
44
|
20 |
|
$this->precision = -1; |
45
|
|
|
|
46
|
20 |
|
$this->ellipsoid = $ellipsoid ? : null; |
47
|
|
|
|
48
|
20 |
|
$this->checkGeometriesArray($geometries); |
49
|
|
|
|
50
|
19 |
|
parent::__construct($geometries); |
51
|
19 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
abstract public function getGeometryType(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return integer |
60
|
|
|
*/ |
61
|
1 |
|
public function getPrecision() |
62
|
|
|
{ |
63
|
1 |
|
return $this->precision; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Coordinate |
68
|
|
|
*/ |
69
|
2 |
|
public function getCoordinate() |
70
|
|
|
{ |
71
|
2 |
|
if ($this->isEmpty()) { |
72
|
1 |
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $this->offsetGet(0)->getCoordinate(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return CoordinateCollection |
80
|
|
|
*/ |
81
|
1 |
|
public function getCoordinates() |
82
|
|
|
{ |
83
|
1 |
|
$coordinates = new CoordinateCollection(array(), $this->ellipsoid); |
84
|
|
|
|
85
|
|
|
/** @var GeometryInterface $element */ |
86
|
1 |
|
foreach ($this->elements as $element) { |
87
|
1 |
|
$coordinates = $coordinates->merge($element->getCoordinates()); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
return $coordinates; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
2 |
|
public function isEmpty() |
97
|
|
|
{ |
98
|
2 |
|
return count($this->elements) === 0 ; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return BoundingBoxInterface |
103
|
|
|
*/ |
104
|
1 |
|
public function getBoundingBox() |
105
|
|
|
{ |
106
|
1 |
|
$boundingBox = new BoundingBox(); |
107
|
|
|
|
108
|
|
|
/** @var GeometryInterface $element */ |
109
|
1 |
|
foreach ($this->elements as $element) { |
110
|
1 |
|
$boundingBox = $boundingBox->merge($element->getBoundingBox()); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
return $boundingBox; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $key |
118
|
|
|
* |
119
|
|
|
* @param GeometryInterface $value |
120
|
|
|
*/ |
121
|
3 |
|
public function set($key, $value) |
122
|
|
|
{ |
123
|
3 |
|
$this->checkEllipsoid($value); |
124
|
3 |
|
$this->elements[$key] = $value; |
125
|
3 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param GeometryInterface $value |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
3 |
|
public function add($value) |
133
|
|
|
{ |
134
|
3 |
|
$this->checkEllipsoid($value); |
135
|
2 |
|
$this->elements[] = $value; |
136
|
|
|
|
137
|
2 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return Ellipsoid |
142
|
|
|
*/ |
143
|
1 |
|
public function getEllipsoid() |
144
|
|
|
{ |
145
|
1 |
|
return $this->ellipsoid; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array GeometryInterface[] $geometries |
150
|
|
|
*/ |
151
|
20 |
|
private function checkGeometriesArray(array $geometries) |
152
|
|
|
{ |
153
|
20 |
|
foreach ($geometries as $geometry) { |
154
|
18 |
|
if (!$geometry instanceof GeometryInterface) { |
155
|
1 |
|
throw new InvalidArgumentException("You didn't provide a geometry!"); |
156
|
|
|
} |
157
|
17 |
|
$this->checkEllipsoid($geometry); |
158
|
19 |
|
} |
159
|
19 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param GeometryInterface $geometry |
163
|
|
|
* |
164
|
|
|
* @throws InvalidArgumentException |
165
|
|
|
*/ |
166
|
17 |
|
private function checkEllipsoid(GeometryInterface $geometry) |
167
|
|
|
{ |
168
|
17 |
|
if (bccomp($geometry->getPrecision(), $this->precision) === 1) { |
169
|
17 |
|
$this->precision = $geometry->getPrecision(); |
170
|
17 |
|
} |
171
|
|
|
|
172
|
17 |
|
if ($this->ellipsoid === null) { |
173
|
17 |
|
$this->ellipsoid = $geometry->getEllipsoid(); |
174
|
17 |
|
} elseif ($geometry->isEmpty() || $geometry->getEllipsoid() != $this->ellipsoid) { |
175
|
1 |
|
throw new InvalidArgumentException("Geometry is invalid"); |
176
|
|
|
} |
177
|
17 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param ArrayCollection $collection |
181
|
|
|
* |
182
|
|
|
* @return ArrayCollection |
183
|
|
|
*/ |
184
|
2 |
|
public function merge(ArrayCollection $collection) |
185
|
|
|
{ |
186
|
2 |
|
if (!$collection instanceof GeometryCollection || $collection->getGeometryType() !== $this->getGeometryType()) { |
187
|
1 |
|
throw new InvalidArgumentException("Collections types don't match, you can't merge."); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
return parent::merge($collection); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|