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

brick / geo / 13633473181

03 Mar 2025 02:25PM UTC coverage: 83.61% (+0.2%) from 83.425%
13633473181

push

github

BenMorel
BoundingBox: readonly + promoted properties

21 of 28 new or added lines in 10 files covered. (75.0%)

41 existing lines in 5 files now uncovered.

1561 of 1867 relevant lines covered (83.61%)

1882.91 hits per line

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

98.44
/src/Point.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use ArrayIterator;
8
use Brick\Geo\Exception\InvalidGeometryException;
9
use Brick\Geo\Projector\Projector;
10
use Override;
11

12
/**
13
 * A Point is a 0-dimensional geometric object and represents a single location in coordinate space.
14
 *
15
 * A Point has an x-coordinate value, a y-coordinate value.
16
 * If called for by the associated Spatial Reference System, it may also have coordinate values for z and m.
17
 *
18
 * The boundary of a Point is the empty set.
19
 */
20
final class Point extends Geometry
21
{
22
    /**
23
     * The x-coordinate value for this Point, or NULL if the point is empty.
24
     */
25
    private ?float $x = null;
26

27
    /**
28
     * The y-coordinate value for this Point, or NULL if the point is empty.
29
     */
30
    private ?float $y = null;
31

32
    /**
33
     * The z-coordinate value for this Point, or NULL if it does not have one.
34
     */
35
    private ?float $z = null;
36

37
    /**
38
     * The m-coordinate value for this Point, or NULL if it does not have one.
39
     */
40
    private ?float $m = null;
41

42
    /**
43
     * @param CoordinateSystem $cs        The coordinate system.
44
     * @param float            ...$coords The point coordinates; can be empty for an empty point.
45
     *
46
     * @throws InvalidGeometryException If the number of coordinates does not match the coordinate system.
47
     *
48
     * @psalm-suppress PossiblyUndefinedArrayOffset
49
     */
50
    public function __construct(CoordinateSystem $cs, float ...$coords)
51
    {
52
        $isEmpty = count($coords) === 0;
16,283✔
53

54
        parent::__construct($cs, $isEmpty);
16,283✔
55

56
        if ($isEmpty) {
16,283✔
57
            return;
576✔
58
        }
59

60
        if (count($coords) !== $cs->coordinateDimension()) {
15,731✔
61
            throw new InvalidGeometryException(sprintf(
84✔
62
                'Expected %d coordinates for Point %s, got %d.',
84✔
63
                $cs->coordinateDimension(),
84✔
64
                $cs->coordinateName(),
84✔
65
                count($coords)
84✔
66
            ));
84✔
67
        }
68

69
        $coords = array_values($coords);
15,647✔
70

71
        foreach ($coords as $i => $coord) {
15,647✔
72
            if (! is_finite($coord)) {
15,647✔
73
                $coordinateName = match ($i) {
126✔
74
                    0 => 'X',
21✔
75
                    1 => 'Y',
21✔
76
                    2 => $cs->hasZ() ? 'Z' : 'M',
63✔
77
                    3 => 'M',
21✔
78
                };
126✔
79
                throw new InvalidGeometryException(sprintf(
126✔
80
                    'Coordinate #%d (%s) for Point %s is %s, this is not allowed.',
126✔
81
                    $i + 1,
126✔
82
                    $coordinateName,
126✔
83
                    $cs->coordinateName(),
126✔
84
                    is_infinite($coord) ? ($coord > 0 ? '+' : '-') . 'INF' : 'NaN',
126✔
85
                ));
126✔
86
            }
87
        }
88

89
        $this->x = $coords[0];
15,521✔
90
        $this->y = $coords[1];
15,521✔
91

92
        $hasZ = $cs->hasZ();
15,521✔
93
        $hasM = $cs->hasM();
15,521✔
94

95
        if ($hasZ) {
15,521✔
96
            $this->z = $coords[2];
6,840✔
97
        }
98

99
        if ($hasM) {
15,521✔
100
            $this->m = $coords[$hasZ ? 3 : 2];
6,204✔
101
        }
102
    }
103

104
    /**
105
     * Creates a point with X and Y coordinates.
106
     */
107
    public static function xy(float $x, float $y, int $srid = 0) : Point
108
    {
109
        return new Point(CoordinateSystem::xy($srid), $x, $y);
77✔
110
    }
111

112
    /**
113
     * Creates a point with X, Y and Z coordinates.
114
     */
115
    public static function xyz(float $x, float $y, float $z, int $srid = 0) : Point
116
    {
117
        return new Point(CoordinateSystem::xyz($srid), $x, $y, $z);
35✔
118
    }
119

120
    /**
121
     * Creates a point with X, Y and M coordinates.
122
     */
123
    public static function xym(float $x, float $y, float $m, int $srid = 0) : Point
124
    {
125
        return new Point(CoordinateSystem::xym($srid), $x, $y, $m);
21✔
126
    }
127

128
    /**
129
     * Creates a point with X, Y, Z and M coordinates.
130
     */
131
    public static function xyzm(float $x, float $y, float $z, float $m, int $srid = 0) : Point
132
    {
133
        return new Point(CoordinateSystem::xyzm($srid), $x, $y, $z, $m);
14✔
134
    }
135

136
    /**
137
     * Creates an empty Point with XY dimensionality.
138
     */
139
    public static function xyEmpty(int $srid = 0) : Point
140
    {
141
        return new Point(CoordinateSystem::xy($srid));
14✔
142
    }
143

144
    /**
145
     * Creates an empty Point with XYZ dimensionality.
146
     */
147
    public static function xyzEmpty(int $srid = 0) : Point
148
    {
149
        return new Point(CoordinateSystem::xyz($srid));
14✔
150
    }
151

152
    /**
153
     * Creates an empty Point with XYM dimensionality.
154
     */
155
    public static function xymEmpty(int $srid = 0) : Point
156
    {
157
        return new Point(CoordinateSystem::xym($srid));
14✔
158
    }
159

160
    /**
161
     * Creates an empty Point with XYZM dimensionality.
162
     */
163
    public static function xyzmEmpty(int $srid = 0) : Point
164
    {
165
        return new Point(CoordinateSystem::xyzm($srid));
14✔
166
    }
167

168
    /**
169
     * Returns the x-coordinate value for this Point.
170
     *
171
     * Returns NULL if the Point is empty.
172
     */
173
    public function x() : ?float
174
    {
175
        return $this->x;
11,474✔
176
    }
177

178
    /**
179
     * Returns the y-coordinate value for this Point.
180
     *
181
     * Returns NULL if the Point is empty.
182
     */
183
    public function y() : ?float
184
    {
185
        return $this->y;
11,474✔
186
    }
187

188
    /**
189
     * Returns the z-coordinate value for this Point.
190
     *
191
     * Returns NULL if the Point is empty, or does not have a Z coordinate.
192
     */
193
    public function z() : ?float
194
    {
195
        return $this->z;
11,467✔
196
    }
197

198
    /**
199
     * Returns the m-coordinate value for this Point.
200
     *
201
     * Returns NULL if the Point is empty, or does not have a M coordinate.
202
     */
203
    public function m() : ?float
204
    {
205
        return $this->m;
11,432✔
206
    }
207

208
    #[Override]
209
    public function geometryType() : string
210
    {
211
        return 'Point';
1,519✔
212
    }
213

214
    #[Override]
215
    public function geometryTypeBinary() : int
216
    {
217
        return Geometry::POINT;
1,000✔
218
    }
219

220
    #[Override]
221
    public function dimension() : int
222
    {
223
        return 0;
77✔
224
    }
225

226
    #[Override]
227
    public function getBoundingBox() : BoundingBox
228
    {
NEW
229
        return BoundingBox::new()->extendedWithPoint($this);
×
230
    }
231

232
    /**
233
     * @psalm-return list<float>
234
     *
235
     * @return float[]
236
     */
237
    #[Override]
238
    public function toArray() : array
239
    {
240
        if ($this->isEmpty) {
3,126✔
241
            return [];
210✔
242
        }
243

244
        /** @var list<float> $result */
245
        $result = [$this->x, $this->y];
2,916✔
246

247
        if ($this->z !== null) {
2,916✔
248
            $result[] = $this->z;
1,407✔
249
        }
250

251
        if ($this->m !== null) {
2,916✔
252
            $result[] = $this->m;
1,036✔
253
        }
254

255
        return $result;
2,916✔
256
    }
257

258
    #[Override]
259
    public function project(Projector $projector) : Point
260
    {
261
        return $projector->project($this);
203✔
262
    }
263

264
    /**
265
     * Returns the number of coordinates in this Point.
266
     *
267
     * Required by interface Countable.
268
     */
269
    #[Override]
270
    public function count() : int
271
    {
272
        if ($this->isEmpty) {
56✔
273
            return 0;
28✔
274
        }
275

276
        return $this->coordinateSystem->coordinateDimension();
28✔
277
    }
278

279
    /**
280
     * Returns an iterator for the coordinates in this Point.
281
     *
282
     * Required by interface IteratorAggregate.
283
     *
284
     * @psalm-return ArrayIterator<int, float>
285
     */
286
    #[Override]
287
    public function getIterator() : ArrayIterator
288
    {
289
        return new ArrayIterator($this->toArray());
252✔
290
    }
291
}
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