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

brick / geo / 13376323048

17 Feb 2025 06:09PM UTC coverage: 83.297% (-0.08%) from 83.38%
13376323048

push

github

BenMorel
Add #[Override] attributes

1511 of 1814 relevant lines covered (83.3%)

1861.35 hits per line

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

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

3
declare(strict_types=1);
4

5
namespace Brick\Geo\IO;
6

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

25
/**
26
 * Builds geometries out of Well-Known Binary strings.
27
 */
28
final class WKBReader extends AbstractWKBReader
29
{
30
    /**
31
     * @param string $wkb  The WKB to read.
32
     * @param int    $srid The optional SRID of the geometry.
33
     *
34
     * @throws GeometryIOException
35
     */
36
    public function read(string $wkb, int $srid = 0) : Geometry
37
    {
38
        $buffer = new WKBBuffer($wkb);
3,462✔
39
        $geometry = $this->readGeometry($buffer, $srid);
3,462✔
40

41
        if (! $buffer->isEndOfStream()) {
3,462✔
42
            throw GeometryIOException::invalidWKB('unexpected data at end of stream');
×
43
        }
44

45
        return $geometry;
3,462✔
46
    }
47

48
    /**
49
     * Introspects the given WKB and returns a proxy of the corresponding class.
50
     *
51
     * This prevents having to fully hydrate the underlying geometry object graph, while still returning an instance
52
     * of the correct geometry class.
53
     *
54
     * @throws GeometryIOException
55
     */
56
    public function readAsProxy(string $wkb, int $srid = 0) : Geometry
57
    {
58
        $buffer = new WKBBuffer($wkb);
1,568✔
59
        $buffer->readByteOrder();
1,568✔
60
        $geometryHeader = $this->readGeometryHeader($buffer);
1,568✔
61

62
        $geometryClass = $this->getGeometryClass($geometryHeader->geometryType);
1,568✔
63

64
        return ProxyFactory::createWkbProxy($geometryClass, $wkb, $srid);
1,568✔
65
    }
66

67
    /**
68
     * @return class-string<Geometry>
69
     *
70
     * @throws GeometryIOException
71
     */
72
    private function getGeometryClass(int $geometryType) : string
73
    {
74
        return match ($geometryType) {
1,568✔
75
            Geometry::POINT => Point::class,
56✔
76
            Geometry::LINESTRING => LineString::class,
112✔
77
            Geometry::CIRCULARSTRING => CircularString::class,
112✔
78
            Geometry::COMPOUNDCURVE => CompoundCurve::class,
112✔
79
            Geometry::POLYGON => Polygon::class,
168✔
80
            Geometry::CURVEPOLYGON => CurvePolygon::class,
112✔
81
            Geometry::MULTIPOINT => MultiPoint::class,
112✔
82
            Geometry::MULTILINESTRING => MultiLineString::class,
168✔
83
            Geometry::MULTIPOLYGON => MultiPolygon::class,
168✔
84
            Geometry::GEOMETRYCOLLECTION => GeometryCollection::class,
112✔
85
            Geometry::POLYHEDRALSURFACE => PolyhedralSurface::class,
112✔
86
            Geometry::TIN => TIN::class,
112✔
87
            Geometry::TRIANGLE => Triangle::class,
112✔
88
            default => throw GeometryIOException::unsupportedWKBType($geometryType),
1,568✔
89
        };
1,568✔
90
    }
91

92
    #[Override]
93
    protected function readGeometryHeader(WKBBuffer $buffer) : WKBGeometryHeader
94
    {
95
        $wkbType = $buffer->readUnsignedLong();
3,462✔
96

97
        if ($wkbType < 0 || $wkbType >= 4000) {
3,462✔
98
            throw GeometryIOException::unsupportedWKBType($wkbType);
×
99
        }
100

101
        $geometryType = $wkbType % 1000;
3,462✔
102
        $dimension = ($wkbType - $geometryType) / 1000;
3,462✔
103

104
        $hasZ = ($dimension === 1 || $dimension === 3);
3,462✔
105
        $hasM = ($dimension === 2 || $dimension === 3);
3,462✔
106

107
        return new WKBGeometryHeader($geometryType, $hasZ, $hasM);
3,462✔
108
    }
109
}
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