Skip to content

Commit b1db057

Browse files
authored
Merge pull request #67 from jl9404/fix/srid-prefix
Fix Geometry::fromWKB function to allow various SRID
2 parents 34c10e4 + a198278 commit b1db057

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

src/Types/Geometry.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ public static function getWKTClass($value)
5555
public static function fromWKB($wkb)
5656
{
5757
// mysql adds 4 NUL bytes at the start of the binary
58-
$prefix = "\0\0\0\0";
59-
if (substr($wkb, 0, strlen($prefix)) == $prefix) {
60-
$wkb = substr($wkb, strlen($prefix));
61-
}
62-
58+
$wkb = substr($wkb, 4);
6359
$parser = new Parser(new Factory());
6460

6561
return $parser->parse($wkb);

tests/Unit/Eloquent/SpatialTraitTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function testInsertUpdateGeometryCollectionHasCorrectSql()
208208

209209
public function testSettingRawAttributes()
210210
{
211-
$attributes['point'] = '0101000000000000000000f03f0000000000000040';
211+
$attributes['point'] = "\0\0\0\0".'0101000000000000000000f03f0000000000000040';
212212

213213
$this->model->setRawAttributes($attributes);
214214
$this->assertInstanceOf(Point::class, ($this->model->point));

tests/Unit/Types/GeometryTest.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ public function testGetWKTClass()
3838

3939
public function testGetWKBClass()
4040
{
41-
$this->assertInstanceOf(Point::class, Geometry::fromWKB('0101000000000000000000f03f0000000000000040'));
41+
$prefix = "\0\0\0\0";
42+
43+
$this->assertInstanceOf(Point::class, Geometry::fromWKB($prefix.'0101000000000000000000f03f0000000000000040'));
4244

43-
$this->assertInstanceOf(LineString::class, Geometry::fromWKB('010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
44-
$this->assertInstanceOf(Polygon::class, Geometry::fromWKB('01030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f0000000000000040'));
45-
$this->assertInstanceOf(MultiPoint::class, Geometry::fromWKB('0104000000020000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040'));
46-
$this->assertInstanceOf(MultiLineString::class, Geometry::fromWKB('010500000001000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
47-
$this->assertInstanceOf(MultiLineString::class, Geometry::fromWKB('010500000002000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040010200000002000000000000000000144000000000000018400000000000001c400000000000002040'));
48-
$this->assertInstanceOf(MultiPolygon::class, Geometry::fromWKB('01060000000200000001030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004001030000000300000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004004000000000000000000264000000000000028400000000000002a400000000000002c400000000000002e4000000000000030400000000000002640000000000000284004000000000000000000354000000000000036400000000000003740000000000000384000000000000039400000000000003a4000000000000035400000000000003640'));
49-
$this->assertInstanceOf(GeometryCollection::class, Geometry::fromWKB('0107000000010000000101000000000000000000f03f0000000000000040'));
50-
$this->assertInstanceOf(GeometryCollection::class, Geometry::fromWKB('0107000000020000000101000000000000000000f03f0000000000000040010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
45+
$this->assertInstanceOf(LineString::class, Geometry::fromWKB($prefix.'010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
46+
$this->assertInstanceOf(Polygon::class, Geometry::fromWKB($prefix.'01030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f0000000000000040'));
47+
$this->assertInstanceOf(MultiPoint::class, Geometry::fromWKB($prefix.'0104000000020000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040'));
48+
$this->assertInstanceOf(MultiLineString::class, Geometry::fromWKB($prefix.'010500000001000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
49+
$this->assertInstanceOf(MultiLineString::class, Geometry::fromWKB($prefix.'010500000002000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040010200000002000000000000000000144000000000000018400000000000001c400000000000002040'));
50+
$this->assertInstanceOf(MultiPolygon::class, Geometry::fromWKB($prefix.'01060000000200000001030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004001030000000300000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004004000000000000000000264000000000000028400000000000002a400000000000002c400000000000002e4000000000000030400000000000002640000000000000284004000000000000000000354000000000000036400000000000003740000000000000384000000000000039400000000000003a4000000000000035400000000000003640'));
51+
$this->assertInstanceOf(GeometryCollection::class, Geometry::fromWKB($prefix.'0107000000010000000101000000000000000000f03f0000000000000040'));
52+
$this->assertInstanceOf(GeometryCollection::class, Geometry::fromWKB($prefix.'0107000000020000000101000000000000000000f03f0000000000000040010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
5153

52-
$prefix = "\0\0\0\0";
5354
$this->assertInstanceOf(Point::class, Geometry::fromWKB($prefix.'0101000000000000000000f03f0000000000000040'));
5455
}
5556

0 commit comments

Comments
 (0)