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

brick / geo / 17456208570

04 Sep 2025 07:10AM UTC coverage: 50.432%. Remained the same
17456208570

push

github

BenMorel
Use @extends and @implements instead of @template-* variants

For consistency with the rest of the project.

1867 of 3702 relevant lines covered (50.43%)

1140.21 hits per line

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

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

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

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

12
use function array_values;
13
use function count;
14
use function is_finite;
15
use function is_infinite;
16
use function sprintf;
17

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

35
    /**
36
     * The y-coordinate value for this Point, or NULL if the point is empty.
37
     */
38
    private ?float $y = null;
39

40
    /**
41
     * The z-coordinate value for this Point, or NULL if it does not have one.
42
     */
43
    private ?float $z = null;
44

45
    /**
46
     * The m-coordinate value for this Point, or NULL if it does not have one.
47
     */
48
    private ?float $m = null;
49

50
    /**
51
     * @param CoordinateSystem $cs        The coordinate system.
52
     * @param float            ...$coords The point coordinates; can be empty for an empty point.
53
     *
54
     * @throws InvalidGeometryException If the number of coordinates does not match the coordinate system.
55
     *
56
     * @psalm-suppress PossiblyUndefinedArrayOffset
57
     */
58
    public function __construct(CoordinateSystem $cs, float ...$coords)
59
    {
60
        $isEmpty = count($coords) === 0;
18,644✔
61

62
        parent::__construct($cs, $isEmpty);
18,644✔
63

64
        if ($isEmpty) {
18,644✔
65
            return;
663✔
66
        }
67

68
        if (count($coords) !== $cs->coordinateDimension()) {
18,004✔
69
            throw new InvalidGeometryException(sprintf(
96✔
70
                'Expected %d coordinates for Point %s, got %d.',
96✔
71
                $cs->coordinateDimension(),
96✔
72
                $cs->coordinateName(),
96✔
73
                count($coords),
96✔
74
            ));
96✔
75
        }
76

77
        $coords = array_values($coords);
17,908✔
78

79
        foreach ($coords as $i => $coord) {
17,908✔
80
            if (! is_finite($coord)) {
17,908✔
81
                $coordinateName = match ($i) {
144✔
82
                    0 => 'X',
24✔
83
                    1 => 'Y',
24✔
84
                    2 => $cs->hasZ() ? 'Z' : 'M',
72✔
85
                    3 => 'M',
24✔
86
                };
144✔
87

88
                throw new InvalidGeometryException(sprintf(
144✔
89
                    'Coordinate #%d (%s) for Point %s is %s, this is not allowed.',
144✔
90
                    $i + 1,
144✔
91
                    $coordinateName,
144✔
92
                    $cs->coordinateName(),
144✔
93
                    is_infinite($coord) ? ($coord > 0 ? '+' : '-') . 'INF' : 'NaN',
144✔
94
                ));
144✔
95
            }
96
        }
97

98
        $this->x = $coords[0];
17,764✔
99
        $this->y = $coords[1];
17,764✔
100

101
        $hasZ = $cs->hasZ();
17,764✔
102
        $hasM = $cs->hasM();
17,764✔
103

104
        if ($hasZ) {
17,764✔
105
            $this->z = $coords[2];
7,450✔
106
        }
107

108
        if ($hasM) {
17,764✔
109
            $this->m = $coords[$hasZ ? 3 : 2];
6,724✔
110
        }
111
    }
112

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

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

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

137
    /**
138
     * Creates a point with X, Y, Z and M coordinates.
139
     */
140
    public static function xyzm(float $x, float $y, float $z, float $m, int $srid = 0): Point
141
    {
142
        return new Point(CoordinateSystem::xyzm($srid), $x, $y, $z, $m);
16✔
143
    }
144

145
    /**
146
     * Creates an empty Point with XY dimensionality.
147
     */
148
    public static function xyEmpty(int $srid = 0): Point
149
    {
150
        return new Point(CoordinateSystem::xy($srid));
16✔
151
    }
152

153
    /**
154
     * Creates an empty Point with XYZ dimensionality.
155
     */
156
    public static function xyzEmpty(int $srid = 0): Point
157
    {
158
        return new Point(CoordinateSystem::xyz($srid));
16✔
159
    }
160

161
    /**
162
     * Creates an empty Point with XYM dimensionality.
163
     */
164
    public static function xymEmpty(int $srid = 0): Point
165
    {
166
        return new Point(CoordinateSystem::xym($srid));
16✔
167
    }
168

169
    /**
170
     * Creates an empty Point with XYZM dimensionality.
171
     */
172
    public static function xyzmEmpty(int $srid = 0): Point
173
    {
174
        return new Point(CoordinateSystem::xyzm($srid));
16✔
175
    }
176

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

187
    /**
188
     * Returns the y-coordinate value for this Point.
189
     *
190
     * Returns NULL if the Point is empty.
191
     */
192
    public function y(): ?float
193
    {
194
        return $this->y;
12,952✔
195
    }
196

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

207
    /**
208
     * Returns the m-coordinate value for this Point.
209
     *
210
     * Returns NULL if the Point is empty, or does not have a M coordinate.
211
     */
212
    public function m(): ?float
213
    {
214
        return $this->m;
12,904✔
215
    }
216

217
    #[NoProxy, Override]
218
    public function geometryType(): string
219
    {
220
        return 'Point';
1,854✔
221
    }
222

223
    #[NoProxy, Override]
224
    public function geometryTypeBinary(): int
225
    {
226
        return Geometry::POINT;
1,210✔
227
    }
228

229
    #[NoProxy, Override]
230
    public function dimension(): int
231
    {
232
        return 0;
120✔
233
    }
234

235
    #[Override]
236
    public function getBoundingBox(): BoundingBox
237
    {
238
        return BoundingBox::new()->extendedWithPoint($this);
×
239
    }
240

241
    /**
242
     * @return list<float>
243
     */
244
    #[Override]
245
    public function toArray(): array
246
    {
247
        if ($this->isEmpty) {
3,708✔
248
            return [];
248✔
249
        }
250

251
        /** @var list<float> $result */
252
        $result = [$this->x, $this->y];
3,460✔
253

254
        if ($this->z !== null) {
3,460✔
255
            $result[] = $this->z;
1,608✔
256
        }
257

258
        if ($this->m !== null) {
3,460✔
259
            $result[] = $this->m;
1,184✔
260
        }
261

262
        return $result;
3,460✔
263
    }
264

265
    #[Override]
266
    public function project(Projector $projector): Point
267
    {
268
        return $projector->project($this);
318✔
269
    }
270
}
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