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

brick / geo / 13715715561

06 Mar 2025 10:47PM UTC coverage: 44.086% (-40.4%) from 84.507%
13715715561

push

github

BenMorel
Remove Psalm-specific annotations

1543 of 3500 relevant lines covered (44.09%)

270.91 hits per line

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

92.86
/src/LineString.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\EmptyGeometryException;
11
use Brick\Geo\Exception\InvalidGeometryException;
12
use Brick\Geo\Exception\NoSuchGeometryException;
13
use Brick\Geo\Projector\Projector;
14
use Override;
15

16
/**
17
 * A LineString is a Curve with linear interpolation between Points.
18
 *
19
 * Each consecutive pair of Points defines a line segment.
20
 *
21
 * @template-implements \IteratorAggregate<Point>
22
 */
23
class LineString extends Curve implements \Countable, \IteratorAggregate
24
{
25
    /**
26
     * The Points that compose this LineString.
27
     *
28
     * An empty LineString contains no points.
29
     * A non-empty LineString contains a minimum of 2 points.
30
     *
31
     * @var list<Point>
32
     */
33
    protected array $points = [];
34

35
    /**
36
     * A LineString must be composed of 2 points or more, or 0 points for an empty LineString.
37
     * A LineString with exactly 1 point is not allowed.
38
     *
39
     * The coordinate system of each of the points must match the one of the LineString.
40
     *
41
     * @param CoordinateSystem $cs        The coordinate system of the LineString.
42
     * @param Point            ...$points The points that compose the LineString.
43
     *
44
     * @throws InvalidGeometryException  If only one point was given.
45
     * @throws CoordinateSystemException If different coordinate systems are used.
46
     */
47
    public function __construct(CoordinateSystem $cs, Point ...$points)
48
    {
49
        parent::__construct($cs, ! $points);
3,516✔
50

51
        if (! $points) {
3,516✔
52
            return;
266✔
53
        }
54

55
        CoordinateSystem::check($this, ...$points);
3,254✔
56

57
        if (count($points) < 2) {
3,254✔
58
            throw new InvalidGeometryException('A LineString must be composed of at least 2 points.');
2✔
59
        }
60

61
        $this->points = array_values($points);
3,252✔
62
    }
63

64
    /**
65
     * Creates a non-empty LineString composed of the given points.
66
     *
67
     * @param Point    $point1 The first point.
68
     * @param Point ...$pointN The subsequent points.
69
     *
70
     * @throws InvalidGeometryException  If only one point was given.
71
     * @throws CoordinateSystemException If the points use different coordinate systems.
72
     */
73
    public static function of(Point $point1, Point ...$pointN) : LineString
74
    {
75
        return new LineString($point1->coordinateSystem(), $point1, ...$pointN);
×
76
    }
77

78
    /**
79
     * Creates a rectangle out of two 2D corner points.
80
     *
81
     * The result is a linear ring (closed and simple).
82
     *
83
     * @psalm-suppress PossiblyNullArgument
84
     *
85
     * @throws EmptyGeometryException    If any of the points is empty.
86
     * @throws CoordinateSystemException If the points use different coordinate systems, or are not 2D.
87
     */
88
    public static function rectangle(Point $a, Point $b) : LineString
89
    {
90
        $cs = $a->coordinateSystem();
14✔
91

92
        if (! $cs->isEqualTo($b->coordinateSystem())) {
14✔
93
            throw CoordinateSystemException::dimensionalityMix($cs, $b->coordinateSystem());
8✔
94
        }
95

96
        if ($cs->coordinateDimension() !== 2) {
6✔
97
            throw new CoordinateSystemException(__METHOD__ . ' expects 2D points.');
×
98
        }
99

100
        if ($a->isEmpty() || $b->isEmpty()) {
6✔
101
            throw new EmptyGeometryException('Points cannot be empty.');
×
102
        }
103

104
        $x1 = min($a->x(), $b->x());
6✔
105
        $x2 = max($a->x(), $b->x());
6✔
106

107
        $y1 = min($a->y(), $b->y());
6✔
108
        $y2 = max($a->y(), $b->y());
6✔
109

110
        $p1 = new Point($cs, $x1, $y1);
6✔
111
        $p2 = new Point($cs, $x2, $y1);
6✔
112
        $p3 = new Point($cs, $x2, $y2);
6✔
113
        $p4 = new Point($cs, $x1, $y2);
6✔
114

115
        return new LineString($cs, $p1, $p2, $p3, $p4, $p1);
6✔
116
    }
117

118
    #[Override]
119
    public function startPoint() : Point
120
    {
121
        if (count($this->points) === 0) {
156✔
122
            throw new EmptyGeometryException('The LineString is empty and has no start point.');
8✔
123
        }
124

125
        return $this->points[0];
148✔
126
    }
127

128
    #[Override]
129
    public function endPoint() : Point
130
    {
131
        $count = count($this->points);
248✔
132

133
        if ($count === 0) {
248✔
134
            throw new EmptyGeometryException('The LineString is empty and has no end point.');
8✔
135
        }
136

137
        return $this->points[$count - 1];
240✔
138
    }
139

140
    /**
141
     * Returns the number of Points in this LineString.
142
     */
143
    public function numPoints() : int
144
    {
145
        return count($this->points);
366✔
146
    }
147

148
    /**
149
     * Returns the specified Point N in this LineString.
150
     *
151
     * @param int $n The point number, 1-based.
152
     *
153
     * @throws NoSuchGeometryException If there is no Point at this index.
154
     */
155
    public function pointN(int $n) : Point
156
    {
157
        if (! isset($this->points[$n - 1])) {
44✔
158
            throw new NoSuchGeometryException('There is no Point in this LineString at index ' . $n);
16✔
159
        }
160

161
        return $this->points[$n - 1];
28✔
162
    }
163

164
    /**
165
     * Returns the points that compose this LineString.
166
     *
167
     * @return list<Point>
168
     */
169
    public function points() : array
170
    {
171
        return $this->points;
×
172
    }
173

174
    #[NoProxy, Override]
175
    public function geometryType() : string
176
    {
177
        return 'LineString';
474✔
178
    }
179

180
    #[NoProxy, Override]
181
    public function geometryTypeBinary() : int
182
    {
183
        return Geometry::LINESTRING;
520✔
184
    }
185

186
    #[Override]
187
    public function getBoundingBox() : BoundingBox
188
    {
189
        $boundingBox = BoundingBox::new();
2✔
190

191
        foreach ($this->points as $point) {
2✔
192
            $boundingBox = $boundingBox->extendedWithPoint($point);
2✔
193
        }
194

195
        return $boundingBox;
2✔
196
    }
197

198
    /**
199
     * @return list<list<float>>
200
     */
201
    #[Override]
202
    public function toArray() : array
203
    {
204
        return array_map(
628✔
205
            fn (Point $point) => $point->toArray(),
628✔
206
            $this->points,
628✔
207
        );
628✔
208
    }
209

210
    #[Override]
211
    public function project(Projector $projector): LineString
212
    {
213
        return new LineString(
40✔
214
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
40✔
215
            ...array_map(
40✔
216
                fn (Point $point) => $point->project($projector),
40✔
217
                $this->points,
40✔
218
            ),
40✔
219
        );
40✔
220
    }
221

222
    /**
223
     * Returns the number of points in this LineString.
224
     */
225
    #[Override]
226
    public function count() : int
227
    {
228
        return count($this->points);
978✔
229
    }
230

231
    /**
232
     * Returns an iterator for the points in this LineString.
233
     *
234
     * @return ArrayIterator<int<0, max>, Point>
235
     */
236
    #[Override]
237
    public function getIterator() : ArrayIterator
238
    {
239
        return new ArrayIterator($this->points);
2,370✔
240
    }
241

242
    /**
243
     * Returns a copy of this LineString, with the given points added.
244
     */
245
    public function withAddedPoints(Point ...$points): LineString
246
    {
247
        return new LineString($this->coordinateSystem, ...$this->points, ...$points);
10✔
248
    }
249
}
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