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

brick / geo / 13700983528

06 Mar 2025 02:28PM UTC coverage: 48.121% (+0.6%) from 47.546%
13700983528

Pull #55

github

web-flow
Merge 41526e1ed into 723c55ddf
Pull Request #55: Add LineInterpolatePoint for Postgis

15 of 21 new or added lines in 3 files covered. (71.43%)

54 existing lines in 10 files now uncovered.

1652 of 3433 relevant lines covered (48.12%)

967.63 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,471✔
52

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

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

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

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

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

70
        foreach ($geometries as $geometry) {
4,687✔
71
            if (! $geometry instanceof $containedGeometryType) {
4,687✔
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,687✔
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);
205✔
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,257✔
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
    {
UNCOV
169
        $boundingBox = new BoundingBox();
×
170

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

UNCOV
175
        return $boundingBox;
×
176
    }
177

178
    public function toArray() : array
179
    {
180
        return array_map(
1,624✔
181
            fn (Geometry $geometry) => $geometry->toArray(),
1,624✔
182
            $this->geometries,
1,624✔
183
        );
1,624✔
184
    }
185

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

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

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

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

229
    /**
230
     * Returns a copy of this GeometryCollection, with the given geometries added.
231
     *
232
     * @psalm-suppress UnsafeInstantiation
233
     *
234
     * @param T ...$geometries
235
     *
236
     * @return GeometryCollection<T>
237
     */
238
    public function withAddedGeometries(Geometry ...$geometries): GeometryCollection
239
    {
240
        return new static($this->coordinateSystem, ...$this->geometries, ...$geometries);
147✔
241
    }
242
}
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