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

brick / geo / 13715715561

06 Mar 2025 10:47PM UTC coverage: 44.086% (-40.4%) from 84.507%
13715715561

push

github

BenMorel
Remove Psalm-specific annotations

1543 of 3500 relevant lines covered (44.09%)

270.91 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);
548✔
26
        $geometry = $this->readGeometry($buffer, $srid);
548✔
27

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

32
        return $geometry;
548✔
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();
548✔
73

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

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

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

84
        return new WKBGeometryHeader($geometryType, $hasZ, $hasM);
548✔
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