• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 13632627167

03 Mar 2025 01:58PM UTC coverage: 47.859% (+0.3%) from 47.546%
13632627167

push

github

BenMorel
Final methods in abstract test classes

1632 of 3410 relevant lines covered (47.86%)

965.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/src/GeometryCollection.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use ArrayIterator;
8
use Brick\Geo\Attribute\NoProxy;
9
use Brick\Geo\Exception\CoordinateSystemException;
10
use Brick\Geo\Exception\NoSuchGeometryException;
11
use Brick\Geo\Exception\UnexpectedGeometryException;
12
use Brick\Geo\Projector\Projector;
13

14
/**
15
 * A GeometryCollection is a geometric object that is a collection of some number of geometric objects.
16
 *
17
 * All the elements in a GeometryCollection shall be in the same Spatial Reference System. This is also the Spatial
18
 * Reference System for the GeometryCollection.
19
 *
20
 * GeometryCollection places no other constraints on its elements. Subclasses of GeometryCollection may restrict
21
 * membership based on dimension and may also place other constraints on the degree of spatial overlap between
22
 * elements.
23
 *
24
 * By the nature of digital representations, collections are inherently ordered by the underlying storage mechanism.
25
 * Two collections whose difference is only this order are spatially equal and will return equivalent results in any
26
 * geometric-defined operations.
27
 *
28
 * @template T of Geometry
29
 */
30
class GeometryCollection extends Geometry
31
{
32
    /**
33
     * The geometries that compose this GeometryCollection.
34
     *
35
     * This array can be empty.
36
     *
37
     * @psalm-var list<T>
38
     *
39
     * @var Geometry[]
40
     */
41
    protected array $geometries = [];
42

43
    /**
44
     * @psalm-param T ...$geometries
45
     *
46
     * @throws CoordinateSystemException   If different coordinate systems are used.
47
     * @throws UnexpectedGeometryException If a geometry is not a valid type for a subclass of GeometryCollection.
48
     */
49
    public function __construct(CoordinateSystem $cs, Geometry ...$geometries)
50
    {
51
        $isEmpty = true;
7,458✔
52

53
        foreach ($geometries as $geometry) {
7,458✔
54
            if (! $geometry->isEmpty()) {
4,675✔
55
                $isEmpty = false;
4,598✔
56
                break;
4,598✔
57
            }
58
        }
59

60
        parent::__construct($cs, $isEmpty);
7,458✔
61

62
        if (! $geometries) {
7,458✔
63
            return;
2,832✔
64
        }
65

66
        CoordinateSystem::check($this, ...$geometries);
4,675✔
67

68
        $containedGeometryType = $this->containedGeometryType();
4,675✔
69

70
        foreach ($geometries as $geometry) {
4,675✔
71
            if (! $geometry instanceof $containedGeometryType) {
4,675✔
72
                throw new UnexpectedGeometryException(sprintf(
×
73
                    '%s expects instance of %s, instance of %s given.',
×
74
                    static::class,
×
75
                    $containedGeometryType,
×
76
                    $geometry::class
×
77
                ));
×
78
            }
79
        }
80

81
        $this->geometries = array_values($geometries);
4,675✔
82
    }
83

84
    /**
85
     * Creates a non-empty GeometryCollection composed of the given geometries.
86
     *
87
     * @psalm-suppress UnsafeInstantiation
88
     *
89
     * @param Geometry    $geometry1 The first geometry.
90
     * @param Geometry ...$geometryN The subsequent geometries, if any.
91
     *
92
     * @return static
93
     *
94
     * @throws CoordinateSystemException   If the geometries use different coordinate systems.
95
     * @throws UnexpectedGeometryException If a geometry is not a valid type for a subclass of GeometryCollection.
96
     */
97
    public static function of(Geometry $geometry1, Geometry ...$geometryN) : GeometryCollection
98
    {
99
        return new static($geometry1->coordinateSystem(), $geometry1, ...$geometryN);
203✔
100
    }
101

102
    /**
103
     * Returns the number of geometries in this GeometryCollection.
104
     */
105
    public function numGeometries() : int
106
    {
107
        return count($this->geometries);
3,244✔
108
    }
109

110
    /**
111
     * Returns the specified geometry N in this GeometryCollection.
112
     *
113
     * @param int $n The geometry number, 1-based.
114
     *
115
     * @return T
116
     *
117
     * @throws NoSuchGeometryException If there is no Geometry at this index.
118
     */
119
    public function geometryN(int $n) : Geometry
120
    {
121
        if (! isset($this->geometries[$n - 1])) {
126✔
122
            throw new NoSuchGeometryException('There is no Geometry in this GeometryCollection at index ' . $n);
84✔
123
        }
124

125
        return $this->geometries[$n - 1];
42✔
126
    }
127

128
    /**
129
     * Returns the geometries that compose this GeometryCollection.
130
     *
131
     * @psalm-return list<T>
132
     *
133
     * @return Geometry[]
134
     */
135
    public function geometries() : array
136
    {
137
        return $this->geometries;
84✔
138
    }
139

140
    #[NoProxy]
141
    public function geometryType() : string
142
    {
143
        return 'GeometryCollection';
841✔
144
    }
145

146
    #[NoProxy]
147
    public function geometryTypeBinary() : int
148
    {
149
        return Geometry::GEOMETRYCOLLECTION;
336✔
150
    }
151

152
    public function dimension() : int
153
    {
154
        $dimension = 0;
56✔
155

156
        foreach ($this->geometries as $geometry) {
56✔
157
            $dim = $geometry->dimension();
28✔
158

159
            if ($dim > $dimension) {
28✔
160
                $dimension = $dim;
21✔
161
            }
162
        }
163

164
        return $dimension;
56✔
165
    }
166

167
    public function getBoundingBox() : BoundingBox
168
    {
169
        $boundingBox = new BoundingBox();
×
170

171
        foreach ($this->geometries as $geometry) {
×
172
            $boundingBox = $boundingBox->extendedWithBoundingBox($geometry->getBoundingBox());
×
173
        }
174

175
        return $boundingBox;
×
176
    }
177

178
    public function toArray() : array
179
    {
180
        $result = [];
1,624✔
181

182
        foreach ($this->geometries as $geometry) {
1,624✔
183
            $result[] = $geometry->toArray();
1,022✔
184
        }
185

186
        return $result;
1,624✔
187
    }
188

189
    public function project(Projector $projector): GeometryCollection
190
    {
191
        return new GeometryCollection(
28✔
192
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
28✔
193
            ...array_map(
28✔
194
                fn (Geometry $geometry) => $geometry->project($projector),
28✔
195
                $this->geometries,
28✔
196
            ),
28✔
197
        );
28✔
198
    }
199

200
    /**
201
     * Returns the number of geometries in this GeometryCollection.
202
     *
203
     * Required by interface Countable.
204
     */
205
    public function count() : int
206
    {
207
        return count($this->geometries);
2,059✔
208
    }
209

210
    /**
211
     * Returns an iterator for the geometries in this GeometryCollection.
212
     *
213
     * Required by interface IteratorAggregate.
214
     *
215
     * @psalm-return ArrayIterator<int, T>
216
     */
217
    public function getIterator() : ArrayIterator
218
    {
219
        return new ArrayIterator($this->geometries);
4,083✔
220
    }
221

222
    /**
223
     * Returns the FQCN of the contained Geometry type.
224
     *
225
     * @psalm-return class-string<T>
226
     */
227
    protected function containedGeometryType() : string
228
    {
229
        return Geometry::class;
1,047✔
230
    }
231

232
    /**
233
     * Returns a copy of this GeometryCollection, with the given geometries added.
234
     *
235
     * @psalm-suppress UnsafeInstantiation
236
     *
237
     * @param T ...$geometries
238
     *
239
     * @return GeometryCollection<T>
240
     */
241
    public function withAddedGeometries(Geometry ...$geometries): GeometryCollection
242
    {
243
        return new static($this->coordinateSystem, ...$this->geometries, ...$geometries);
147✔
244
    }
245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc