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

brick / geo / 13632627167

03 Mar 2025 01:58PM UTC coverage: 47.859% (+0.3%) from 47.546%
13632627167

push

github

BenMorel
Final methods in abstract test classes

1632 of 3410 relevant lines covered (47.86%)

965.73 hits per line

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

98.39
/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

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
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
    public function __construct(CoordinateSystem $cs, float ...$coords)
49
    {
50
        parent::__construct($cs, ! $coords);
15,422✔
51

52
        if ($coords) {
15,422✔
53
            if (count($coords) !== $cs->coordinateDimension()) {
14,863✔
54
                throw new InvalidGeometryException(sprintf(
84✔
55
                    'Expected %d coordinates for Point %s, got %d.',
84✔
56
                    $cs->coordinateDimension(),
84✔
57
                    $cs->coordinateName(),
84✔
58
                    count($coords)
84✔
59
                ));
84✔
60
            }
61

62
            $coords = array_values($coords);
14,779✔
63

64
            foreach ($coords as $i => $coord) {
14,779✔
65
                if (! is_finite($coord)) {
14,779✔
66
                    $coordinateName = match ($i) {
126✔
67
                        0 => 'X',
21✔
68
                        1 => 'Y',
21✔
69
                        2 => $cs->hasZ() ? 'Z' : 'M',
63✔
70
                        3 => 'M',
21✔
71
                    };
126✔
72
                    throw new InvalidGeometryException(sprintf(
126✔
73
                        'Coordinate #%d (%s) for Point %s is %s, this is not allowed.',
126✔
74
                        $i + 1,
126✔
75
                        $coordinateName,
126✔
76
                        $cs->coordinateName(),
126✔
77
                        is_infinite($coord) ? ($coord > 0 ? '+' : '-') . 'INF' : 'NaN',
126✔
78
                    ));
126✔
79
                }
80
            }
81

82
            $this->x = $coords[0];
14,653✔
83
            $this->y = $coords[1];
14,653✔
84

85
            $hasZ = $cs->hasZ();
14,653✔
86
            $hasM = $cs->hasM();
14,653✔
87

88
            if ($hasZ) {
14,653✔
89
                $this->z = $coords[2];
6,392✔
90
            }
91

92
            if ($hasM) {
14,653✔
93
                $this->m = $coords[$hasZ ? 3 : 2];
5,756✔
94
            }
95
        }
96
    }
97

98
    /**
99
     * Creates a point with X and Y coordinates.
100
     */
101
    public static function xy(float $x, float $y, int $srid = 0) : Point
102
    {
103
        return new Point(CoordinateSystem::xy($srid), $x, $y);
77✔
104
    }
105

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

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

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

130
    /**
131
     * Creates an empty Point with XY dimensionality.
132
     */
133
    public static function xyEmpty(int $srid = 0) : Point
134
    {
135
        return new Point(CoordinateSystem::xy($srid));
14✔
136
    }
137

138
    /**
139
     * Creates an empty Point with XYZ dimensionality.
140
     */
141
    public static function xyzEmpty(int $srid = 0) : Point
142
    {
143
        return new Point(CoordinateSystem::xyz($srid));
14✔
144
    }
145

146
    /**
147
     * Creates an empty Point with XYM dimensionality.
148
     */
149
    public static function xymEmpty(int $srid = 0) : Point
150
    {
151
        return new Point(CoordinateSystem::xym($srid));
14✔
152
    }
153

154
    /**
155
     * Creates an empty Point with XYZM dimensionality.
156
     */
157
    public static function xyzmEmpty(int $srid = 0) : Point
158
    {
159
        return new Point(CoordinateSystem::xyzm($srid));
14✔
160
    }
161

162
    /**
163
     * Returns the x-coordinate value for this Point.
164
     *
165
     * Returns NULL if the Point is empty.
166
     */
167
    public function x() : ?float
168
    {
169
        return $this->x;
10,606✔
170
    }
171

172
    /**
173
     * Returns the y-coordinate value for this Point.
174
     *
175
     * Returns NULL if the Point is empty.
176
     */
177
    public function y() : ?float
178
    {
179
        return $this->y;
10,606✔
180
    }
181

182
    /**
183
     * Returns the z-coordinate value for this Point.
184
     *
185
     * Returns NULL if the Point is empty, or does not have a Z coordinate.
186
     */
187
    public function z() : ?float
188
    {
189
        return $this->z;
10,599✔
190
    }
191

192
    /**
193
     * Returns the m-coordinate value for this Point.
194
     *
195
     * Returns NULL if the Point is empty, or does not have a M coordinate.
196
     */
197
    public function m() : ?float
198
    {
199
        return $this->m;
10,564✔
200
    }
201

202
    #[NoProxy]
203
    public function geometryType() : string
204
    {
205
        return 'Point';
1,431✔
206
    }
207

208
    #[NoProxy]
209
    public function geometryTypeBinary() : int
210
    {
211
        return Geometry::POINT;
979✔
212
    }
213

214
    #[NoProxy]
215
    public function dimension() : int
216
    {
217
        return 0;
105✔
218
    }
219

220
    public function getBoundingBox() : BoundingBox
221
    {
222
        return (new BoundingBox())->extendedWithPoint($this);
×
223
    }
224

225
    /**
226
     * @psalm-return list<float>
227
     *
228
     * @return float[]
229
     */
230
    public function toArray() : array
231
    {
232
        if ($this->isEmpty) {
3,161✔
233
            return [];
217✔
234
        }
235

236
        /** @var list<float> $result */
237
        $result = [$this->x, $this->y];
2,944✔
238

239
        if ($this->z !== null) {
2,944✔
240
            $result[] = $this->z;
1,407✔
241
        }
242

243
        if ($this->m !== null) {
2,944✔
244
            $result[] = $this->m;
1,036✔
245
        }
246

247
        return $result;
2,944✔
248
    }
249

250
    public function project(Projector $projector) : Point
251
    {
252
        return $projector->project($this);
203✔
253
    }
254

255
    /**
256
     * Returns the number of coordinates in this Point.
257
     *
258
     * Required by interface Countable.
259
     */
260
    public function count() : int
261
    {
262
        if ($this->isEmpty) {
56✔
263
            return 0;
28✔
264
        }
265

266
        return $this->coordinateSystem->coordinateDimension();
28✔
267
    }
268

269
    /**
270
     * Returns an iterator for the coordinates in this Point.
271
     *
272
     * Required by interface IteratorAggregate.
273
     *
274
     * @psalm-return ArrayIterator<int, float>
275
     */
276
    public function getIterator() : ArrayIterator
277
    {
278
        return new ArrayIterator($this->toArray());
252✔
279
    }
280
}
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