-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathGeometryCollection.php
executable file
·146 lines (120 loc) · 3.65 KB
/
GeometryCollection.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Grimzy\LaravelMysqlSpatial\Types;
use ArrayAccess;
use ArrayIterator;
use Countable;
use GeoJson\Feature\FeatureCollection;
use GeoJson\GeoJson;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
use Illuminate\Contracts\Support\Arrayable;
use InvalidArgumentException;
use IteratorAggregate;
class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable
{
/**
* The items contained in the spatial collection.
*
* @var GeometryInterface[]
*/
protected $items = [];
/**
* @param GeometryInterface[] $geometries
*
* @throws InvalidArgumentException
*/
public function __construct(array $geometries, $srid = null)
{
$validated = array_filter($geometries, function ($value) {
return $value instanceof GeometryInterface;
});
if (count($geometries) !== count($validated)) {
throw new InvalidArgumentException('$geometries must be an array of Geometry objects');
}
$this->items = $geometries;
$this->srid = $srid;
}
public function getGeometries()
{
return $this->items;
}
public function toWKT()
{
return sprintf('GEOMETRYCOLLECTION(%s)', (string) $this);
}
public function __toString()
{
return implode(',', array_map(function (GeometryInterface $geometry) {
return $geometry->toWKT();
}, $this->items));
}
public static function fromString($wktArgument)
{
$geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument);
return new static(array_map(function ($geometry_string) {
$klass = Geometry::getWKTClass($geometry_string);
return call_user_func($klass.'::fromWKT', $geometry_string);
}, $geometry_strings));
}
public function toArray()
{
return $this->items;
}
public function getIterator()
{
return new ArrayIterator($this->items);
}
public function offsetExists($offset)
{
return isset($this->items[$offset]);
}
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->items[$offset] : null;
}
public function offsetSet($offset, $value)
{
if (!($value instanceof GeometryInterface)) {
throw new InvalidArgumentException('$value must be an instance of GeometryInterface');
}
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
public function offsetUnset($offset)
{
unset($this->items[$offset]);
}
public function count()
{
return count($this->items);
}
public static function fromJson($geoJson)
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
if (!is_a($geoJson, FeatureCollection::class)) {
throw new InvalidGeoJsonException('Expected '.FeatureCollection::class.', got '.get_class($geoJson));
}
$set = [];
foreach ($geoJson->getFeatures() as $feature) {
$set[] = parent::fromJson($feature);
}
return new self($set);
}
/**
* Convert to GeoJson GeometryCollection that is jsonable to GeoJSON.
*
* @return \GeoJson\Geometry\GeometryCollection
*/
public function jsonSerialize()
{
$geometries = [];
foreach ($this->items as $geometry) {
$geometries[] = $geometry->jsonSerialize();
}
return new \GeoJson\Geometry\GeometryCollection($geometries);
}
}