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

brick / geo / 13700983528

06 Mar 2025 02:28PM UTC coverage: 48.121% (+0.6%) from 47.546%
13700983528

Pull #55

github

web-flow
Merge 41526e1ed into 723c55ddf
Pull Request #55: Add LineInterpolatePoint for Postgis

15 of 21 new or added lines in 3 files covered. (71.43%)

54 existing lines in 10 files now uncovered.

1652 of 3433 relevant lines covered (48.12%)

967.63 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\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
     * @psalm-suppress PossiblyUndefinedArrayOffset
49
     */
50
    public function __construct(CoordinateSystem $cs, float ...$coords)
51
    {
52
        $isEmpty = count($coords) === 0;
15,455✔
53

54
        parent::__construct($cs, $isEmpty);
15,455✔
55

56
        if ($isEmpty) {
15,455✔
57
            return;
580✔
58
        }
59

60
        if (count($coords) !== $cs->coordinateDimension()) {
14,895✔
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);
14,811✔
70

71
        foreach ($coords as $i => $coord) {
14,811✔
72
            if (! is_finite($coord)) {
14,811✔
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];
14,685✔
90
        $this->y = $coords[1];
14,685✔
91

92
        $hasZ = $cs->hasZ();
14,685✔
93
        $hasM = $cs->hasM();
14,685✔
94

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

99
        if ($hasM) {
14,685✔
100
            $this->m = $coords[$hasZ ? 3 : 2];
5,756✔
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;
10,638✔
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;
10,638✔
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;
10,631✔
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;
10,596✔
206
    }
207

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

214
    #[NoProxy]
215
    public function geometryTypeBinary() : int
216
    {
217
        return Geometry::POINT;
979✔
218
    }
219

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

226
    public function getBoundingBox() : BoundingBox
227
    {
UNCOV
228
        return (new BoundingBox())->extendedWithPoint($this);
×
229
    }
230

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

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

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

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

253
        return $result;
2,944✔
254
    }
255

256
    public function project(Projector $projector) : Point
257
    {
258
        return $projector->project($this);
203✔
259
    }
260

261
    /**
262
     * Returns the number of coordinates in this Point.
263
     *
264
     * Required by interface Countable.
265
     */
266
    public function count() : int
267
    {
268
        if ($this->isEmpty) {
56✔
269
            return 0;
28✔
270
        }
271

272
        return $this->coordinateSystem->coordinateDimension();
28✔
273
    }
274

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