-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathGeometry.php
118 lines (97 loc) · 3.21 KB
/
Geometry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Grimzy\LaravelMysqlSpatial\Types;
use GeoIO\WKB\Parser\Parser;
use GeoJson\GeoJson;
use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
use Illuminate\Contracts\Support\Jsonable;
abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializable
{
protected static $wkb_types = [
1 => Point::class,
2 => LineString::class,
3 => Polygon::class,
4 => MultiPoint::class,
5 => MultiLineString::class,
6 => MultiPolygon::class,
7 => GeometryCollection::class,
];
protected $srid = 0;
public static function getWKTArgument($value)
{
$left = strpos($value, '(');
$right = strrpos($value, ')');
return substr($value, $left + 1, $right - $left - 1);
}
public static function getWKTClass($value)
{
$left = strpos($value, '(');
$type = trim(substr($value, 0, $left));
switch (strtoupper($type)) {
case 'POINT':
return Point::class;
case 'LINESTRING':
return LineString::class;
case 'POLYGON':
return Polygon::class;
case 'MULTIPOINT':
return MultiPoint::class;
case 'MULTILINESTRING':
return MultiLineString::class;
case 'MULTIPOLYGON':
return MultiPolygon::class;
case 'GEOMETRYCOLLECTION':
return GeometryCollection::class;
default:
throw new UnknownWKTTypeException('Type was '.$type);
}
}
public static function fromWKB($wkb)
{
// mysql adds 4 bytes at the start of the binary as SRID
// $srid = @unpack('V', substr($wkb, 0, 4))[1];
// $wkb = substr($wkb, 4);
$bom = unpack('C', substr($wkb, 4, 5));
$formatter = $bom ? 'V' : 'N';
$srid = unpack($formatter, substr($wkb, 0, 4))[1];
$type = unpack($formatter, substr($wkb, 5, 9))[1];
$wkb = substr($wkb, 9);
if ($srid)
{
$type |= Parser::MASK_SRID;
$wkb = pack('C', $bom) . pack($formatter, $type) . pack($formatter, $srid) . $wkb;
}
else
{
$wkb = pack('C', $bom) . pack($formatter, $type) . $wkb;
}
$parser = new Parser(new Factory());
return $parser->parse($wkb);
}
public static function fromWKT($wkt)
{
$wktArgument = static::getWKTArgument($wkt);
return static::fromString($wktArgument);
}
public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
if ($geoJson->getType() === 'FeatureCollection') {
return GeometryCollection::fromJson($geoJson);
}
if ($geoJson->getType() === 'Feature') {
$geoJson = $geoJson->getGeometry();
}
$type = '\Grimzy\LaravelMysqlSpatial\Types\\'.$geoJson->getType();
return $type::fromJson($geoJson);
}
public function toJson($options = 0)
{
return json_encode($this, $options);
}
public function getSRID()
{
return $this->srid;
}
}