-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
CurvePolygon.php
190 lines (163 loc) · 4.95 KB
/
CurvePolygon.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
<?php
declare(strict_types=1);
namespace Brick\Geo;
use ArrayIterator;
use Brick\Geo\Attribute\NoProxy;
use Brick\Geo\Exception\CoordinateSystemException;
use Brick\Geo\Exception\EmptyGeometryException;
use Brick\Geo\Exception\NoSuchGeometryException;
use Brick\Geo\Projector\Projector;
/**
* A CurvePolygon is a planar Surface defined by 1 exterior boundary and 0 or more interior boundaries.
*
* A CurvePolygon instance differs from a Polygon instance in that a CurvePolygon instance may contain
* the following circular arc segments: CircularString and CompoundCurve in addition to LineString.
*/
class CurvePolygon extends Surface
{
/**
* The rings that compose this CurvePolygon.
*
* The first one represents the exterior ring, and the
* (optional) other ones represent the interior rings of the CurvePolygon.
*
* An empty CurvePolygon contains no rings.
*
* @psalm-var list<Curve>
*
* @var Curve[]
*/
protected array $rings = [];
/**
* The coordinate system of each of the rings must match the one of the CurvePolygon.
*
* @param CoordinateSystem $cs The coordinate system of the CurvePolygon.
* @param Curve ...$rings The rings that compose the CurvePolygon.
*
* @throws CoordinateSystemException If different coordinate systems are used.
*/
public function __construct(CoordinateSystem $cs, Curve ...$rings)
{
parent::__construct($cs, ! $rings);
if (! $rings) {
return;
}
CoordinateSystem::check($this, ...$rings);
$this->rings = array_values($rings);
}
/**
* Creates a non-empty CurvePolygon composed of the given rings.
*
* @psalm-suppress UnsafeInstantiation
*
* @param Curve $exteriorRing The exterior ring.
* @param Curve ...$interiorRings The interior rings, if any.
*
* @throws CoordinateSystemException If the rings use different coordinate systems.
*/
public static function of(Curve $exteriorRing, Curve ...$interiorRings) : CurvePolygon
{
return new static($exteriorRing->coordinateSystem(), $exteriorRing, ...$interiorRings);
}
/**
* Returns the exterior ring of this CurvePolygon.
*
* @throws EmptyGeometryException
*/
public function exteriorRing() : Curve
{
if ($this->isEmpty) {
throw new EmptyGeometryException('An empty CurvePolygon has no exterior ring.');
}
return $this->rings[0];
}
/**
* Returns the number of interior rings in this CurvePolygon.
*/
public function numInteriorRings() : int
{
if ($this->isEmpty) {
return 0;
}
return count($this->rings) - 1;
}
/**
* Returns the specified interior ring N in this CurvePolygon.
*
* @param int $n The ring number, 1-based.
*
* @throws NoSuchGeometryException If there is no interior ring at this index.
*/
public function interiorRingN(int $n) : Curve
{
if ($n === 0 || ! isset($this->rings[$n])) {
throw new NoSuchGeometryException('There is no interior ring in this CurvePolygon at index ' . $n);
}
return $this->rings[$n];
}
/**
* Returns the interior rings in this CurvePolygon.
*
* @return Curve[]
*/
public function interiorRings() : array
{
return array_slice($this->rings, 1);
}
#[NoProxy]
public function geometryType() : string
{
return 'CurvePolygon';
}
#[NoProxy]
public function geometryTypeBinary() : int
{
return Geometry::CURVEPOLYGON;
}
public function getBoundingBox() : BoundingBox
{
$boundingBox = new BoundingBox();
foreach ($this->rings as $ring) {
$boundingBox = $boundingBox->extendedWithBoundingBox($ring->getBoundingBox());
}
return $boundingBox;
}
public function toArray() : array
{
$result = [];
foreach ($this->rings as $ring) {
$result[] = $ring->toArray();
}
return $result;
}
public function project(Projector $projector): CurvePolygon
{
return new CurvePolygon(
$projector->getTargetCoordinateSystem($this->coordinateSystem),
...array_map(
fn (Curve $ring) => $ring->project($projector),
$this->rings,
),
);
}
/**
* Returns the number of rings (exterior + interior) in this CurvePolygon.
*
* Required by interface Countable.
*/
public function count() : int
{
return count($this->rings);
}
/**
* Returns an iterator for the rings (exterior + interior) in this CurvePolygon.
*
* Required by interface IteratorAggregate.
*
* @psalm-return ArrayIterator<int, Curve>
*/
public function getIterator() : ArrayIterator
{
return new ArrayIterator($this->rings);
}
}