|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GeoJSON class : a geojson reader/writer. |
|
4
|
|
|
* |
|
5
|
|
|
* Note that it will always return a GeoJSON geometry. This |
|
6
|
|
|
* means that if you pass it a feature, it will return the |
|
7
|
|
|
* geometry of that feature strip everything else. |
|
8
|
|
|
*/ |
|
9
|
|
|
class GeoJSON extends GeoAdapter |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Given an object or a string, return a Geometry |
|
13
|
|
|
* |
|
14
|
|
|
* @param mixed $input The GeoJSON string or object |
|
15
|
|
|
* |
|
16
|
|
|
* @return object Geometry |
|
17
|
|
|
*/ |
|
18
|
|
|
public function read($input) { |
|
19
|
|
|
if (is_string($input)) { |
|
20
|
|
|
$input = json_decode($input); |
|
21
|
|
|
} |
|
22
|
|
|
if (!is_object($input)) { |
|
23
|
|
|
throw new Exception('Invalid JSON'); |
|
24
|
|
|
} |
|
25
|
|
|
if (!is_string($input->type)) { |
|
26
|
|
|
throw new Exception('Invalid JSON'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Check to see if it's a FeatureCollection |
|
30
|
|
|
if ($input->type == 'FeatureCollection') { |
|
31
|
|
|
$geoms = array(); |
|
32
|
|
|
foreach ($input->features as $feature) { |
|
33
|
|
|
$geoms[] = $this->read($feature); |
|
34
|
|
|
} |
|
35
|
|
|
return geoPHP::geometryReduce($geoms); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Check to see if it's a Feature |
|
39
|
|
|
if ($input->type == 'Feature') { |
|
40
|
|
|
return $this->read($input->geometry); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// It's a geometry - process it |
|
44
|
|
|
return $this->objToGeom($input); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function objToGeom($obj) { |
|
48
|
|
|
$type = $obj->type; |
|
49
|
|
|
|
|
50
|
|
|
if ($type == 'GeometryCollection') { |
|
51
|
|
|
return $this->objToGeometryCollection($obj); |
|
52
|
|
|
} |
|
53
|
|
|
$method = 'arrayTo' . $type; |
|
54
|
|
|
return $this->$method($obj->coordinates); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function arrayToPoint($array) { |
|
58
|
|
|
return new Point($array[0], $array[1]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function arrayToLineString($array) { |
|
62
|
|
|
$points = array(); |
|
63
|
|
|
foreach ($array as $comp_array) { |
|
64
|
|
|
$points[] = $this->arrayToPoint($comp_array); |
|
65
|
|
|
} |
|
66
|
|
|
return new LineString($points); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function arrayToPolygon($array) { |
|
70
|
|
|
$lines = array(); |
|
71
|
|
|
foreach ($array as $comp_array) { |
|
72
|
|
|
$lines[] = $this->arrayToLineString($comp_array); |
|
73
|
|
|
} |
|
74
|
|
|
return new Polygon($lines); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function arrayToMultiPoint($array) { |
|
78
|
|
|
$points = array(); |
|
79
|
|
|
foreach ($array as $comp_array) { |
|
80
|
|
|
$points[] = $this->arrayToPoint($comp_array); |
|
81
|
|
|
} |
|
82
|
|
|
return new MultiPoint($points); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function arrayToMultiLineString($array) { |
|
86
|
|
|
$lines = array(); |
|
87
|
|
|
foreach ($array as $comp_array) { |
|
88
|
|
|
$lines[] = $this->arrayToLineString($comp_array); |
|
89
|
|
|
} |
|
90
|
|
|
return new MultiLineString($lines); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function arrayToMultiPolygon($array) { |
|
94
|
|
|
$polys = array(); |
|
95
|
|
|
foreach ($array as $comp_array) { |
|
96
|
|
|
$polys[] = $this->arrayToPolygon($comp_array); |
|
97
|
|
|
} |
|
98
|
|
|
return new MultiPolygon($polys); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function objToGeometryCollection($obj) { |
|
102
|
|
|
$geoms = array(); |
|
103
|
|
|
if (empty($obj->geometries)) { |
|
104
|
|
|
throw new Exception('Invalid GeoJSON: GeometryCollection with no component geometries'); |
|
105
|
|
|
} |
|
106
|
|
|
foreach ($obj->geometries as $comp_object) { |
|
107
|
|
|
$geoms[] = $this->objToGeom($comp_object); |
|
108
|
|
|
} |
|
109
|
|
|
return new GeometryCollection($geoms); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Serializes an object into a geojson string |
|
114
|
|
|
* |
|
115
|
|
|
* |
|
116
|
|
|
* @param Geometry $obj The object to serialize |
|
|
|
|
|
|
117
|
|
|
* |
|
118
|
|
|
* @return string The GeoJSON string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function write(Geometry $geometry, $return_array = FALSE) { |
|
121
|
|
|
if ($return_array) { |
|
122
|
|
|
return $this->getArray($geometry); |
|
123
|
|
|
} |
|
124
|
|
|
else { |
|
125
|
|
|
return json_encode($this->getArray($geometry)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getArray($geometry) { |
|
130
|
|
|
if ($geometry->getGeomType() == 'GeometryCollection') { |
|
131
|
|
|
$component_array = array(); |
|
132
|
|
|
foreach ($geometry->components as $component) { |
|
133
|
|
|
$component_array[] = array( |
|
134
|
|
|
'type' => $component->geometryType(), |
|
135
|
|
|
'coordinates' => $component->asArray(), |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
return array( |
|
139
|
|
|
'type'=> 'GeometryCollection', |
|
140
|
|
|
'geometries'=> $component_array, |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
else return array( |
|
144
|
|
|
'type'=> $geometry->getGeomType(), |
|
145
|
|
|
'coordinates'=> $geometry->asArray(), |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.