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

brick / geo / 13753277563

09 Mar 2025 10:43PM UTC coverage: 49.787% (+2.5%) from 47.295%
13753277563

push

github

BenMorel
Prepare for release

1749 of 3513 relevant lines covered (49.79%)

975.53 hits per line

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

34.38
/src/IO/WKBReader.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\IO;
6

7
use Brick\Geo\Geometry;
8
use Brick\Geo\Exception\GeometryIOException;
9
use Brick\Geo\Proxy;
10
use Override;
11

12
/**
13
 * Builds geometries out of Well-Known Binary strings.
14
 */
15
final class WKBReader extends AbstractWKBReader
16
{
17
    /**
18
     * @param string $wkb  The WKB to read.
19
     * @param int    $srid The optional SRID of the geometry.
20
     *
21
     * @throws GeometryIOException
22
     */
23
    public function read(string $wkb, int $srid = 0) : Geometry
24
    {
25
        $buffer = new WKBBuffer($wkb);
2,068✔
26
        $geometry = $this->readGeometry($buffer, $srid);
2,068✔
27

28
        if (! $buffer->isEndOfStream()) {
2,068✔
29
            throw GeometryIOException::invalidWKB('unexpected data at end of stream');
×
30
        }
31

32
        return $geometry;
2,068✔
33
    }
34

35
    /**
36
     * Introspects the given WKB and returns a proxy of the corresponding class.
37
     *
38
     * This prevents having to fully hydrate the underlying geometry object graph, while still returning an instance
39
     * of the correct geometry class.
40
     *
41
     * @return Geometry&Proxy\ProxyInterface
42
     *
43
     * @throws GeometryIOException
44
     */
45
    public function readAsProxy(string $wkb, int $srid = 0) : Geometry
46
    {
47
        $buffer = new WKBBuffer($wkb);
×
48
        $buffer->readByteOrder();
×
49
        $geometryHeader = $this->readGeometryHeader($buffer);
×
50

51
        return match ($geometryHeader->geometryType) {
×
52
            Geometry::POINT => new Proxy\PointProxy($wkb, true, $srid),
×
53
            Geometry::LINESTRING => new Proxy\LineStringProxy($wkb, true, $srid),
×
54
            Geometry::CIRCULARSTRING => new Proxy\CircularStringProxy($wkb, true, $srid),
×
55
            Geometry::COMPOUNDCURVE => new Proxy\CompoundCurveProxy($wkb, true, $srid),
×
56
            Geometry::POLYGON => new Proxy\PolygonProxy($wkb, true, $srid),
×
57
            Geometry::CURVEPOLYGON => new Proxy\CurvePolygonProxy($wkb, true, $srid),
×
58
            Geometry::MULTIPOINT => new Proxy\MultiPointProxy($wkb, true, $srid),
×
59
            Geometry::MULTILINESTRING => new Proxy\MultiLineStringProxy($wkb, true, $srid),
×
60
            Geometry::MULTIPOLYGON => new Proxy\MultiPolygonProxy($wkb, true, $srid),
×
61
            Geometry::GEOMETRYCOLLECTION => new Proxy\GeometryCollectionProxy($wkb, true, $srid),
×
62
            Geometry::POLYHEDRALSURFACE => new Proxy\PolyhedralSurfaceProxy($wkb, true, $srid),
×
63
            Geometry::TIN => new Proxy\TINProxy($wkb, true, $srid),
×
64
            Geometry::TRIANGLE => new Proxy\TriangleProxy($wkb, true, $srid),
×
65
            default => throw GeometryIOException::unsupportedWKBType($geometryHeader->geometryType),
×
66
        };
×
67
    }
68

69
    #[Override]
70
    protected function readGeometryHeader(WKBBuffer $buffer) : WKBGeometryHeader
71
    {
72
        $wkbType = $buffer->readUnsignedLong();
2,068✔
73

74
        if ($wkbType < 0 || $wkbType >= 4000) {
2,068✔
75
            throw GeometryIOException::unsupportedWKBType($wkbType);
×
76
        }
77

78
        $geometryType = $wkbType % 1000;
2,068✔
79
        $dimension = ($wkbType - $geometryType) / 1000;
2,068✔
80

81
        $hasZ = ($dimension === 1 || $dimension === 3);
2,068✔
82
        $hasM = ($dimension === 2 || $dimension === 3);
2,068✔
83

84
        return new WKBGeometryHeader($geometryType, $hasZ, $hasM);
2,068✔
85
    }
86
}
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