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

brick / geo / 13872073621

15 Mar 2025 10:37AM UTC coverage: 51.165% (-36.2%) from 87.322%
13872073621

push

github

BenMorel
Add support for GeosOp in requireEngine()

1866 of 3647 relevant lines covered (51.17%)

1154.34 hits per line

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

93.02
/src/CoordinateSystem.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use Brick\Geo\Exception\CoordinateSystemException;
8

9
/**
10
 * Represents the dimensionality and spatial reference system of a geometry.
11
 *
12
 * This class is immutable.
13
 */
14
final class CoordinateSystem
15
{
16
    /**
17
     * Whether this coordinate system has Z-coordinates.
18
     */
19
    private readonly bool $hasZ;
20

21
    /**
22
     * Whether this coordinate system has M-coordinates.
23
     */
24
    private readonly bool $hasM;
25

26
    /**
27
     * The Spatial Reference System Identifier of this coordinate system.
28
     */
29
    private readonly int $srid;
30

31
    /**
32
     * @param bool $hasZ Whether the coordinate system has Z-coordinates.
33
     * @param bool $hasM Whether the coordinate system has M-coordinates.
34
     * @param int  $srid The optional Spatial Reference ID of the coordinate system.
35
     */
36
    public function __construct(bool $hasZ, bool $hasM, int $srid = 0)
37
    {
38
        $this->hasZ = $hasZ;
28,591✔
39
        $this->hasM = $hasM;
28,591✔
40
        $this->srid = $srid;
28,591✔
41
    }
42

43
    /**
44
     * Returns a CoordinateSystem with X and Y coordinates, and an optional SRID.
45
     */
46
    public static function xy(int $srid = 0) : CoordinateSystem
47
    {
48
        return new self(false, false, $srid);
160✔
49
    }
50

51
    /**
52
     * Returns a CoordinateSystem with X, Y and Z coordinates, and an optional SRID.
53
     */
54
    public static function xyz(int $srid = 0) : CoordinateSystem
55
    {
56
        return new self(true, false, $srid);
56✔
57
    }
58

59
    /**
60
     * Returns a CoordinateSystem with X, Y and M coordinates, and an optional SRID.
61
     */
62
    public static function xym(int $srid = 0) : CoordinateSystem
63
    {
64
        return new self(false, true, $srid);
40✔
65
    }
66

67
    /**
68
     * Returns a CoordinateSystem with X, Y, Z and M coordinates, and an optional SRID.
69
     */
70
    public static function xyzm(int $srid = 0) : CoordinateSystem
71
    {
72
        return new self(true, true, $srid);
48✔
73
    }
74

75
    /**
76
     * Returns whether this coordinate system has Z-coordinates.
77
     */
78
    public function hasZ() : bool
79
    {
80
        return $this->hasZ;
26,479✔
81
    }
82

83
    /**
84
     * Returns whether this coordinate system has M-coordinates.
85
     */
86
    public function hasM() : bool
87
    {
88
        return $this->hasM;
26,511✔
89
    }
90

91
    /**
92
     * Returns the Spatial Reference System Identifier of this coordinate system.
93
     */
94
    public function SRID() : int
95
    {
96
        return $this->srid;
21,809✔
97
    }
98

99
    /**
100
     * Returns a name for the coordinates in this system, such as XY or XYZ.
101
     *
102
     * @return 'XY'|'XYZ'|'XYM'|'XYZM'
103
     */
104
    public function coordinateName() : string
105
    {
106
        $name = 'XY';
432✔
107

108
        if ($this->hasZ) {
432✔
109
            $name .= 'Z';
264✔
110
        }
111

112
        if ($this->hasM) {
432✔
113
            $name .= 'M';
256✔
114
        }
115

116
        return $name;
432✔
117
    }
118

119
    /**
120
     * Returns the coordinate dimension of this coordinate system.
121
     *
122
     * The coordinate dimension is the total number of coordinates in the coordinate system.
123
     *
124
     * Returns:
125
     *
126
     * - 2 for (X,Y)
127
     * - 3 for (X,Y,Z) and (X,Y,M)
128
     * - 4 for (X,Y,Z,M)
129
     *
130
     * @return int<2, 4>
131
     */
132
    public function coordinateDimension() : int
133
    {
134
        $coordinateDimension = 2;
17,940✔
135

136
        if ($this->hasZ) {
17,940✔
137
            $coordinateDimension++;
7,578✔
138
        }
139

140
        if ($this->hasM) {
17,940✔
141
            $coordinateDimension++;
6,852✔
142
        }
143

144
        return $coordinateDimension;
17,940✔
145
    }
146

147
    /**
148
     * Returns the spatial dimension of this coordinate system.
149
     *
150
     * The spatial dimension is 3 if the coordinate system has a Z coordinate, 2 otherwise.
151
     *
152
     * @return int<2, 3>
153
     */
154
    public function spatialDimension() : int
155
    {
156
        return $this->hasZ ? 3 : 2;
96✔
157
    }
158

159
    /**
160
     * Returns a copy of this CoordinateSystem with the $hasZ altered.
161
     */
162
    public function withZ(bool $hasZ) : CoordinateSystem
163
    {
164
        if ($hasZ === $this->hasZ) {
8✔
165
            return $this;
8✔
166
        }
167

168
        return new CoordinateSystem($hasZ, $this->hasM, $this->srid);
×
169
    }
170

171
    /**
172
     * Returns a copy of this CoordinateSystem with the $hasM altered.
173
     */
174
    public function withM(bool $hasM) : CoordinateSystem
175
    {
176
        if ($hasM === $this->hasM) {
8✔
177
            return $this;
×
178
        }
179

180
        return new CoordinateSystem($this->hasZ, $hasM, $this->srid);
8✔
181
    }
182

183
    /**
184
     * Returns a copy of this CoordinateSystem with the SRID altered.
185
     */
186
    public function withSRID(int $srid) : CoordinateSystem
187
    {
188
        if ($srid === $this->srid) {
224✔
189
            return $this;
×
190
        }
191

192
        return new CoordinateSystem($this->hasZ, $this->hasM, $srid);
224✔
193
    }
194

195
    public function isEqualTo(CoordinateSystem $that) : bool
196
    {
197
        return $this->hasZ === $that->hasZ
16,220✔
198
            && $this->hasM === $that->hasM
16,220✔
199
            && $this->srid === $that->srid;
16,220✔
200
    }
201

202
    /**
203
     * @param Geometry    $reference  The geometry holding the reference coordinate system.
204
     * @param Geometry ...$geometries The geometries to check against this coordinate system.
205
     *
206
     * @throws CoordinateSystemException If the coordinate systems differ.
207
     */
208
    public static function check(Geometry $reference, Geometry ...$geometries) : void
209
    {
210
        $referenceCS = $reference->coordinateSystem();
16,156✔
211

212
        foreach ($geometries as $geometry) {
16,156✔
213
            $geometryCS = $geometry->coordinateSystem();
16,156✔
214

215
            if ($geometryCS->isEqualTo($referenceCS)) {
16,156✔
216
                continue;
16,156✔
217
            }
218

219
            if ($geometryCS->srid !== $referenceCS->srid) {
192✔
220
                throw CoordinateSystemException::sridCompositionMix($reference, $geometry);
48✔
221
            }
222

223
            throw CoordinateSystemException::dimensionalityCompositionMix($reference, $geometry);
144✔
224
        }
225
    }
226
}
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