• 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

98.33
/src/Point.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\InvalidGeometryException;
10
use Brick\Geo\Projector\Projector;
11
use Override;
12

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

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

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

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

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

55
        parent::__construct($cs, $isEmpty);
4,416✔
56

57
        if ($isEmpty) {
4,416✔
58
            return;
166✔
59
        }
60

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

70
        $coords = array_values($coords);
4,232✔
71

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

90
        $this->x = $coords[0];
4,196✔
91
        $this->y = $coords[1];
4,196✔
92

93
        $hasZ = $cs->hasZ();
4,196✔
94
        $hasM = $cs->hasM();
4,196✔
95

96
        if ($hasZ) {
4,196✔
97
            $this->z = $coords[2];
1,828✔
98
        }
99

100
        if ($hasM) {
4,196✔
101
            $this->m = $coords[$hasZ ? 3 : 2];
1,648✔
102
        }
103
    }
104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

221
    #[NoProxy, Override]
222
    public function dimension() : int
223
    {
224
        return 0;
30✔
225
    }
226

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

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

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

246
        if ($this->z !== null) {
840✔
247
            $result[] = $this->z;
402✔
248
        }
249

250
        if ($this->m !== null) {
840✔
251
            $result[] = $this->m;
296✔
252
        }
253

254
        return $result;
840✔
255
    }
256

257
    #[Override]
258
    public function project(Projector $projector) : Point
259
    {
260
        return $projector->project($this);
58✔
261
    }
262
}
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