Skip to content

Commit 81b8305

Browse files
committed
Merge branch 'srid-fix'
2 parents aa2a28a + 7d0578b commit 81b8305

29 files changed

+727
-424
lines changed

src/Eloquent/BaseBuilder.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ class BaseBuilder extends QueryBuilder
88
{
99
protected function cleanBindings(array $bindings)
1010
{
11-
$bindings = array_map(function ($binding) {
12-
return $binding instanceof SpatialExpression ? $binding->getSpatialValue() : $binding;
13-
}, $bindings);
11+
$spatialBindings = [];
12+
foreach ($bindings as &$binding) {
13+
if ($binding instanceof SpatialExpression) {
14+
$spatialBindings[] = $binding->getSpatialValue();
15+
$spatialBindings[] = $binding->getSrid();
16+
} else {
17+
$spatialBindings[] = $binding;
18+
}
19+
}
1420

15-
return parent::cleanBindings($bindings);
21+
return parent::cleanBindings($spatialBindings);
1622
}
1723
}

src/Eloquent/SpatialExpression.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ class SpatialExpression extends Expression
88
{
99
public function getValue()
1010
{
11-
return 'ST_GeomFromText(?)';
11+
return 'ST_GeomFromText(?, ?)';
1212
}
1313

1414
public function getSpatialValue()
1515
{
1616
return $this->value->toWkt();
1717
}
18+
19+
public function getSrid()
20+
{
21+
return $this->value->getSrid();
22+
}
1823
}

src/Schema/Blueprint.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ class Blueprint extends IlluminateBlueprint
99
/**
1010
* Add a geometry column on the table.
1111
*
12-
* @param string $column
12+
* @param string $column
13+
* @param null|int $srid
1314
*
1415
* @return \Illuminate\Support\Fluent
1516
*/
16-
public function geometry($column)
17+
public function geometry($column, $srid = null)
1718
{
18-
return $this->addColumn('geometry', $column);
19+
return $this->addColumn('geometry', $column, compact('srid'));
1920
}
2021

2122
/**
@@ -34,73 +35,79 @@ public function point($column, $srid = null)
3435
/**
3536
* Add a linestring column on the table.
3637
*
37-
* @param $column
38+
* @param string $column
39+
* @param null|int $srid
3840
*
3941
* @return \Illuminate\Support\Fluent
4042
*/
41-
public function lineString($column)
43+
public function lineString($column, $srid = null)
4244
{
43-
return $this->addColumn('linestring', $column);
45+
return $this->addColumn('linestring', $column, compact('srid'));
4446
}
4547

4648
/**
4749
* Add a polygon column on the table.
4850
*
49-
* @param $column
51+
* @param string $column
52+
* @param null|int $srid
5053
*
5154
* @return \Illuminate\Support\Fluent
5255
*/
53-
public function polygon($column)
56+
public function polygon($column, $srid = null)
5457
{
55-
return $this->addColumn('polygon', $column);
58+
return $this->addColumn('polygon', $column, compact('srid'));
5659
}
5760

5861
/**
5962
* Add a multipoint column on the table.
6063
*
61-
* @param $column
64+
* @param string $column
65+
* @param null|int $srid
6266
*
6367
* @return \Illuminate\Support\Fluent
6468
*/
65-
public function multiPoint($column)
69+
public function multiPoint($column, $srid = null)
6670
{
67-
return $this->addColumn('multipoint', $column);
71+
return $this->addColumn('multipoint', $column, compact('srid'));
6872
}
6973

7074
/**
7175
* Add a multilinestring column on the table.
7276
*
73-
* @param $column
77+
* @param string $column
78+
* @param null|int $srid
7479
*
7580
* @return \Illuminate\Support\Fluent
7681
*/
77-
public function multiLineString($column)
82+
public function multiLineString($column, $srid = null)
7883
{
79-
return $this->addColumn('multilinestring', $column);
84+
return $this->addColumn('multilinestring', $column, compact('srid'));
8085
}
8186

8287
/**
8388
* Add a multipolygon column on the table.
8489
*
85-
* @param $column
90+
* @param string $column
91+
* @param null|int $srid
8692
*
8793
* @return \Illuminate\Support\Fluent
8894
*/
89-
public function multiPolygon($column)
95+
public function multiPolygon($column, $srid = null)
9096
{
91-
return $this->addColumn('multipolygon', $column);
97+
return $this->addColumn('multipolygon', $column, compact('srid'));
9298
}
9399

94100
/**
95101
* Add a geometrycollection column on the table.
96102
*
97-
* @param $column
103+
* @param string $column
104+
* @param null|int $srid
98105
*
99106
* @return \Illuminate\Support\Fluent
100107
*/
101-
public function geometryCollection($column)
108+
public function geometryCollection($column, $srid = null)
102109
{
103-
return $this->addColumn('geometrycollection', $column);
110+
return $this->addColumn('geometrycollection', $column, compact('srid'));
104111
}
105112

106113
/**

src/Types/Factory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,41 @@ class Factory implements \GeoIO\Factory
66
{
77
public function createPoint($dimension, array $coordinates, $srid = null)
88
{
9-
return new Point($coordinates['y'], $coordinates['x']);
9+
return new Point($coordinates['y'], $coordinates['x'], $srid);
1010
}
1111

1212
public function createLineString($dimension, array $points, $srid = null)
1313
{
14-
return new LineString($points);
14+
return new LineString($points, $srid);
1515
}
1616

1717
public function createLinearRing($dimension, array $points, $srid = null)
1818
{
19-
return new LineString($points);
19+
return new LineString($points, $srid);
2020
}
2121

2222
public function createPolygon($dimension, array $lineStrings, $srid = null)
2323
{
24-
return new Polygon($lineStrings);
24+
return new Polygon($lineStrings, $srid);
2525
}
2626

2727
public function createMultiPoint($dimension, array $points, $srid = null)
2828
{
29-
return new MultiPoint($points);
29+
return new MultiPoint($points, $srid);
3030
}
3131

3232
public function createMultiLineString($dimension, array $lineStrings, $srid = null)
3333
{
34-
return new MultiLineString($lineStrings);
34+
return new MultiLineString($lineStrings, $srid);
3535
}
3636

3737
public function createMultiPolygon($dimension, array $polygons, $srid = null)
3838
{
39-
return new MultiPolygon($polygons);
39+
return new MultiPolygon($polygons, $srid);
4040
}
4141

4242
public function createGeometryCollection($dimension, array $geometries, $srid = null)
4343
{
44-
return new GeometryCollection($geometries);
44+
return new GeometryCollection($geometries, $srid);
4545
}
4646
}

src/Types/Geometry.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializabl
1919
7 => GeometryCollection::class,
2020
];
2121

22+
protected $srid;
23+
24+
public function __construct($srid = 0)
25+
{
26+
$this->srid = (int) $srid;
27+
}
28+
29+
public function getSrid() {
30+
return $this->srid;
31+
}
32+
33+
public function setSrid($srid)
34+
{
35+
$this->srid = (int) $srid;
36+
}
37+
2238
public static function getWKTArgument($value)
2339
{
2440
$left = strpos($value, '(');
@@ -54,18 +70,27 @@ public static function getWKTClass($value)
5470

5571
public static function fromWKB($wkb)
5672
{
57-
// mysql adds 4 NUL bytes at the start of the binary
73+
$srid = substr($wkb, 0, 4);
74+
$srid = unpack('L', $srid)[1];
75+
5876
$wkb = substr($wkb, 4);
5977
$parser = new Parser(new Factory());
6078

61-
return $parser->parse($wkb);
79+
/** @var Geometry $parsed */
80+
$parsed = $parser->parse($wkb);
81+
82+
if ($srid >= 0 && $srid < 4000) {
83+
$parsed->setSrid($srid);
84+
}
85+
86+
return $parsed;
6287
}
6388

64-
public static function fromWKT($wkt)
89+
public static function fromWKT($wkt, $srid = null)
6590
{
6691
$wktArgument = static::getWKTArgument($wkt);
6792

68-
return static::fromString($wktArgument);
93+
return static::fromString($wktArgument, $srid);
6994
}
7095

7196
public static function fromJson($geoJson)

src/Types/GeometryCollection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc
2323

2424
/**
2525
* @param GeometryInterface[] $geometries
26+
* @param int $srid
2627
*
2728
* @throws InvalidArgumentException
2829
*/
29-
public function __construct(array $geometries)
30+
public function __construct(array $geometries, $srid = 0)
3031
{
32+
parent::__construct($srid);
33+
3134
$validated = array_filter($geometries, function ($value) {
3235
return $value instanceof GeometryInterface;
3336
});
@@ -56,15 +59,15 @@ public function __toString()
5659
}, $this->items));
5760
}
5861

59-
public static function fromString($wktArgument)
62+
public static function fromString($wktArgument, $srid = 0)
6063
{
6164
$geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument);
6265

6366
return new static(array_map(function ($geometry_string) {
6467
$klass = Geometry::getWKTClass($geometry_string);
6568

6669
return call_user_func($klass.'::fromWKT', $geometry_string);
67-
}, $geometry_strings));
70+
}, $geometry_strings), $srid);
6871
}
6972

7073
public function toArray()

src/Types/GeometryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ interface GeometryInterface
66
{
77
public function toWKT();
88

9-
public static function fromWKT($wkt);
9+
public static function fromWKT($wkt, $srid = 0);
1010

1111
public function __toString();
1212

13-
public static function fromString($wktArgument);
13+
public static function fromString($wktArgument, $srid = 0);
1414

1515
public static function fromJson($geoJson);
1616
}

src/Types/LineString.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ public function toWKT()
1313
return sprintf('LINESTRING(%s)', $this->toPairList());
1414
}
1515

16-
public static function fromWkt($wkt)
16+
public static function fromWkt($wkt, $srid = 0)
1717
{
1818
$wktArgument = Geometry::getWKTArgument($wkt);
1919

20-
return static::fromString($wktArgument);
20+
return static::fromString($wktArgument, $srid);
2121
}
2222

23-
public static function fromString($wktArgument)
23+
public static function fromString($wktArgument, $srid = 0)
2424
{
2525
$pairs = explode(',', trim($wktArgument));
2626
$points = array_map(function ($pair) {
2727
return Point::fromPair($pair);
2828
}, $pairs);
2929

30-
return new static($points);
30+
return new static($points, $srid);
3131
}
3232

3333
public function __toString()

src/Types/MultiLineString.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class MultiLineString extends GeometryCollection
1111
{
1212
/**
1313
* @param LineString[] $lineStrings
14+
* @param int $srid
1415
*/
15-
public function __construct(array $lineStrings)
16+
public function __construct(array $lineStrings, $srid = 0)
1617
{
1718
if (count($lineStrings) < 1) {
1819
throw new InvalidArgumentException('$lineStrings must contain at least one entry');
@@ -26,7 +27,7 @@ public function __construct(array $lineStrings)
2627
throw new InvalidArgumentException('$lineStrings must be an array of LineString');
2728
}
2829

29-
parent::__construct($lineStrings);
30+
parent::__construct($lineStrings, $srid);
3031
}
3132

3233
public function getLineStrings()
@@ -39,14 +40,14 @@ public function toWKT()
3940
return sprintf('MULTILINESTRING(%s)', (string) $this);
4041
}
4142

42-
public static function fromString($wktArgument)
43+
public static function fromString($wktArgument, $srid = 0)
4344
{
4445
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
4546
$lineStrings = array_map(function ($data) {
4647
return LineString::fromString($data);
4748
}, $str);
4849

49-
return new static($lineStrings);
50+
return new static($lineStrings, $srid);
5051
}
5152

5253
public function __toString()

src/Types/MultiPoint.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public function toWKT()
1313
return sprintf('MULTIPOINT(%s)', (string) $this);
1414
}
1515

16-
public static function fromWkt($wkt)
16+
public static function fromWkt($wkt, $srid = 0)
1717
{
1818
$wktArgument = Geometry::getWKTArgument($wkt);
1919

20-
return static::fromString($wktArgument);
20+
return static::fromString($wktArgument, $srid);
2121
}
2222

23-
public static function fromString($wktArgument)
23+
public static function fromString($wktArgument, $srid = 0)
2424
{
2525
$matches = [];
2626
preg_match_all('/\(\s*(\d+\s+\d+)\s*\)/', trim($wktArgument), $matches);
@@ -29,7 +29,7 @@ public static function fromString($wktArgument)
2929
return Point::fromPair($pair);
3030
}, $matches[1]);
3131

32-
return new static($points);
32+
return new static($points, $srid);
3333
}
3434

3535
public function __toString()

0 commit comments

Comments
 (0)