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

brick / geo / 13872493694

15 Mar 2025 11:39AM UTC coverage: 51.165%. Remained the same
13872493694

push

github

BenMorel
Properly capitalize symbols: IO

7 of 61 new or added lines in 13 files covered. (11.48%)

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.4
/src/Io/Internal/AbstractWkbReader.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Io\Internal;
6

7
use Brick\Geo\CircularString;
8
use Brick\Geo\CompoundCurve;
9
use Brick\Geo\CoordinateSystem;
10
use Brick\Geo\Curve;
11
use Brick\Geo\CurvePolygon;
12
use Brick\Geo\Exception\GeometryIoException;
13
use Brick\Geo\Geometry;
14
use Brick\Geo\GeometryCollection;
15
use Brick\Geo\LineString;
16
use Brick\Geo\MultiLineString;
17
use Brick\Geo\MultiPoint;
18
use Brick\Geo\MultiPolygon;
19
use Brick\Geo\Point;
20
use Brick\Geo\Polygon;
21
use Brick\Geo\PolyhedralSurface;
22
use Brick\Geo\TIN;
23
use Brick\Geo\Triangle;
24

25
/**
26
 * Base class for WkbReader and EwkbReader.
27
 *
28
 * @internal
29
 */
30
abstract class AbstractWkbReader
31
{
32
    /**
33
     * @throws GeometryIoException
34
     */
35
    abstract protected function readGeometryHeader(WkbBuffer $buffer) : WkbGeometryHeader;
36

37
    /**
38
     * @throws GeometryIoException
39
     */
40
    protected function readGeometry(WkbBuffer $buffer, int $srid) : Geometry
41
    {
42
        $buffer->readByteOrder();
7,753✔
43

44
        $geometryHeader = $this->readGeometryHeader($buffer);
7,753✔
45

46
        $cs = new CoordinateSystem(
7,753✔
47
            $geometryHeader->hasZ,
7,753✔
48
            $geometryHeader->hasM,
7,753✔
49
            $geometryHeader->srid ?? $srid
7,753✔
50
        );
7,753✔
51

52
        return match ($geometryHeader->geometryType) {
7,753✔
53
            Geometry::POINT => $this->readPoint($buffer, $cs),
1,945✔
54
            Geometry::LINESTRING => $this->readLineString($buffer, $cs),
2,497✔
55
            Geometry::CIRCULARSTRING => $this->readCircularString($buffer, $cs),
1,724✔
56
            Geometry::COMPOUNDCURVE => $this->readCompoundCurve($buffer, $cs),
1,324✔
57
            Geometry::POLYGON => $this->readPolygon($buffer, $cs),
2,161✔
58
            Geometry::CURVEPOLYGON => $this->readCurvePolygon($buffer, $cs),
1,037✔
59
            Geometry::MULTIPOINT => $this->readMultiPoint($buffer, $cs),
1,067✔
60
            Geometry::MULTILINESTRING => $this->readMultiLineString($buffer, $cs),
1,139✔
61
            Geometry::MULTIPOLYGON => $this->readMultiPolygon($buffer, $cs),
1,067✔
62
            Geometry::GEOMETRYCOLLECTION => $this->readGeometryCollection($buffer, $cs),
729✔
63
            Geometry::POLYHEDRALSURFACE => $this->readPolyhedralSurface($buffer, $cs),
664✔
64
            Geometry::TIN => $this->readTIN($buffer, $cs),
576✔
65
            Geometry::TRIANGLE => $this->readTriangle($buffer, $cs),
768✔
66
            default => throw GeometryIoException::unsupportedWkbType($geometryHeader->geometryType),
7,753✔
67
        };
7,753✔
68
    }
69

70
    private function readPoint(WkbBuffer $buffer, CoordinateSystem $cs) : Point
71
    {
72
        $coords = $buffer->readDoubles($cs->coordinateDimension());
4,575✔
73

74
        return new Point($cs, ...$coords);
4,575✔
75
    }
76

77
    private function readLineString(WkbBuffer $buffer, CoordinateSystem $cs) : LineString
78
    {
79
        $numPoints = $buffer->readUnsignedLong();
3,739✔
80

81
        $points = [];
3,739✔
82

83
        for ($i = 0; $i < $numPoints; $i++) {
3,739✔
84
            $points[] = $this->readPoint($buffer, $cs);
3,459✔
85
        }
86

87
        return new LineString($cs, ...$points);
3,739✔
88
    }
89

90
    private function readCircularString(WkbBuffer $buffer, CoordinateSystem $cs) : CircularString
91
    {
92
        $numPoints = $buffer->readUnsignedLong();
1,024✔
93

94
        $points = [];
1,024✔
95

96
        for ($i = 0; $i < $numPoints; $i++) {
1,024✔
97
            $points[] = $this->readPoint($buffer, $cs);
768✔
98
        }
99

100
        return new CircularString($cs, ...$points);
1,024✔
101
    }
102

103
    /**
104
     * @throws GeometryIoException
105
     */
106
    private function readCompoundCurve(WkbBuffer $buffer, CoordinateSystem $cs) : CompoundCurve
107
    {
108
        $numCurves = $buffer->readUnsignedLong();
640✔
109
        $curves = [];
640✔
110

111
        for ($i = 0; $i < $numCurves; $i++) {
640✔
112
            $curve = $this->readGeometry($buffer, $cs->SRID());
384✔
113

114
            if (! $curve instanceof LineString && ! $curve instanceof CircularString) {
384✔
NEW
115
                throw new GeometryIoException('Expected LineString|CircularString, got ' . $curve->geometryType());
×
116
            }
117

118
            $curves[] = $curve;
384✔
119
        }
120

121
        return new CompoundCurve($cs, ...$curves);
640✔
122
    }
123

124
    private function readPolygon(WkbBuffer $buffer, CoordinateSystem $cs) : Polygon
125
    {
126
        $numRings = $buffer->readUnsignedLong();
1,670✔
127

128
        $rings = [];
1,670✔
129

130
        for ($i = 0; $i < $numRings; $i++) {
1,670✔
131
            $rings[] = $this->readLineString($buffer, $cs);
1,398✔
132
        }
133

134
        return new Polygon($cs, ...$rings);
1,670✔
135
    }
136

137
    /**
138
     * @throws GeometryIoException
139
     */
140
    private function readCurvePolygon(WkbBuffer $buffer, CoordinateSystem $cs) : CurvePolygon
141
    {
142
        $numRings = $buffer->readUnsignedLong();
512✔
143

144
        $rings = [];
512✔
145

146
        for ($i = 0; $i < $numRings; $i++) {
512✔
147
            $ring = $this->readGeometry($buffer, $cs->SRID());
256✔
148

149
            if (! $ring instanceof Curve) {
256✔
NEW
150
                throw new GeometryIoException('Expected Curve, got ' . $ring->geometryType());
×
151
            }
152

153
            $rings[] = $ring;
256✔
154
        }
155

156
        return new CurvePolygon($cs, ...$rings);
512✔
157
    }
158

159
    /**
160
     * @throws GeometryIoException
161
     */
162
    private function readMultiPoint(WkbBuffer $buffer, CoordinateSystem $cs) : MultiPoint
163
    {
164
        $numPoints = $buffer->readUnsignedLong();
609✔
165
        $points = [];
609✔
166

167
        for ($i = 0; $i < $numPoints; $i++) {
609✔
168
            $point = $this->readGeometry($buffer, $cs->SRID());
353✔
169

170
            if (! $point instanceof Point) {
353✔
NEW
171
                throw new GeometryIoException('Expected Point, got ' . $point->geometryType());
×
172
            }
173

174
            $points[] = $point;
353✔
175
        }
176

177
        return new MultiPoint($cs, ...$points);
609✔
178
    }
179

180
    /**
181
     * @throws GeometryIoException
182
     */
183
    private function readMultiLineString(WkbBuffer $buffer, CoordinateSystem $cs) : MultiLineString
184
    {
185
        $numLineStrings = $buffer->readUnsignedLong();
778✔
186
        $lineStrings = [];
778✔
187

188
        for ($i = 0; $i < $numLineStrings; $i++) {
778✔
189
            $lineString = $this->readGeometry($buffer, $cs->SRID());
514✔
190

191
            if (! $lineString instanceof LineString) {
514✔
NEW
192
                throw new GeometryIoException('Expected LineString, got ' . $lineString->geometryType());
×
193
            }
194

195
            $lineStrings[] = $lineString;
514✔
196
        }
197

198
        return new MultiLineString($cs, ...$lineStrings);
778✔
199
    }
200

201
    /**
202
     * @throws GeometryIoException
203
     */
204
    private function readMultiPolygon(WkbBuffer $buffer, CoordinateSystem $cs) : MultiPolygon
205
    {
206
        $numPolygons = $buffer->readUnsignedLong();
806✔
207
        $polygons = [];
806✔
208

209
        for ($i = 0; $i < $numPolygons; $i++) {
806✔
210
            $polygon = $this->readGeometry($buffer, $cs->SRID());
534✔
211

212
            if (! $polygon instanceof Polygon) {
534✔
NEW
213
                throw new GeometryIoException('Expected Polygon, got ' . $polygon->geometryType());
×
214
            }
215

216
            $polygons[] = $polygon;
534✔
217
        }
218

219
        return new MultiPolygon($cs, ...$polygons);
806✔
220
    }
221

222
    private function readGeometryCollection(WkbBuffer $buffer, CoordinateSystem $cs) : GeometryCollection
223
    {
224
        $numGeometries = $buffer->readUnsignedLong();
534✔
225
        $geometries = [];
534✔
226

227
        for ($i = 0; $i < $numGeometries; $i++) {
534✔
228
            $geometries[] = $this->readGeometry($buffer, $cs->SRID());
260✔
229
        }
230

231
        return new GeometryCollection($cs, ...$geometries);
534✔
232
    }
233

234
    /**
235
     * @throws GeometryIoException
236
     */
237
    private function readPolyhedralSurface(WkbBuffer $buffer, CoordinateSystem $cs) : PolyhedralSurface
238
    {
239
        $numPatches = $buffer->readUnsignedLong();
536✔
240
        $patches = [];
536✔
241

242
        for ($i = 0; $i < $numPatches; $i++) {
536✔
243
            $patch = $this->readGeometry($buffer, $cs->SRID());
256✔
244

245
            if (! $patch instanceof Polygon) {
256✔
NEW
246
                throw new GeometryIoException('Expected Polygon, got ' . $patch->geometryType());
×
247
            }
248

249
            $patches[] = $patch;
256✔
250
        }
251

252
        return new PolyhedralSurface($cs, ...$patches);
536✔
253
    }
254

255
    /**
256
     * @throws GeometryIoException
257
     */
258
    private function readTIN(WkbBuffer $buffer, CoordinateSystem $cs) : TIN
259
    {
260
        $numPatches = $buffer->readUnsignedLong();
512✔
261
        $patches = [];
512✔
262

263
        for ($i = 0; $i < $numPatches; $i++) {
512✔
264
            $patch = $this->readGeometry($buffer, $cs->SRID());
256✔
265

266
            if (! $patch instanceof Triangle) {
256✔
NEW
267
                throw new GeometryIoException('Expected Triangle, got ' . $patch->geometryType());
×
268
            }
269

270
            $patches[] = $patch;
256✔
271
        }
272

273
        return new TIN($cs, ...$patches);
512✔
274
    }
275

276
    private function readTriangle(WkbBuffer $buffer, CoordinateSystem $cs) : Triangle
277
    {
278
        $numRings = $buffer->readUnsignedLong();
768✔
279

280
        $rings = [];
768✔
281

282
        for ($i = 0; $i < $numRings; $i++) {
768✔
283
            $rings[] = $this->readLineString($buffer, $cs);
512✔
284
        }
285

286
        return new Triangle($cs, ...$rings);
768✔
287
    }
288
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc