|
1
|
|
|
<?php
|
|
2
|
|
|
/*
|
|
3
|
|
|
* Copyright (c) Patrick Hayes
|
|
4
|
|
|
*
|
|
5
|
|
|
* This code is open-source and licenced under the Modified BSD License.
|
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
7
|
|
|
* file that was distributed with this source code.
|
|
8
|
|
|
*/
|
|
9
|
|
|
|
|
10
|
|
|
/**
|
|
11
|
|
|
* PHP Geometry/GeoRSS encoder/decoder
|
|
12
|
|
|
*/
|
|
13
|
|
|
class GeoRSS extends GeoAdapter
|
|
14
|
|
|
{
|
|
15
|
|
|
private $namespace = FALSE;
|
|
16
|
|
|
private $nss = ''; // Name-space string. eg 'georss:'
|
|
17
|
|
|
|
|
18
|
|
|
/**
|
|
19
|
|
|
* Read GeoRSS string into geometry objects
|
|
20
|
|
|
*
|
|
21
|
|
|
* @param string $georss - an XML feed containing geoRSS
|
|
|
|
|
|
|
22
|
|
|
*
|
|
23
|
|
|
* @return Geometry|GeometryCollection
|
|
24
|
|
|
*/
|
|
25
|
|
|
public function read($gpx) {
|
|
26
|
|
|
return $this->geomFromText($gpx);
|
|
27
|
|
|
}
|
|
28
|
|
|
|
|
29
|
|
|
/**
|
|
30
|
|
|
* Serialize geometries into a GeoRSS string.
|
|
31
|
|
|
*
|
|
32
|
|
|
* @param Geometry $geometry
|
|
33
|
|
|
*
|
|
34
|
|
|
* @return string The georss string representation of the input geometries
|
|
35
|
|
|
*/
|
|
36
|
|
|
public function write(Geometry $geometry, $namespace = FALSE) {
|
|
37
|
|
|
if ($namespace) {
|
|
38
|
|
|
$this->namespace = $namespace;
|
|
39
|
|
|
$this->nss = $namespace.':';
|
|
40
|
|
|
}
|
|
41
|
|
|
return $this->geometryToGeoRSS($geometry);
|
|
42
|
|
|
}
|
|
43
|
|
|
|
|
44
|
|
|
public function geomFromText($text) {
|
|
45
|
|
|
// Change to lower-case, strip all CDATA, and de-namespace
|
|
46
|
|
|
$text = strtolower($text);
|
|
47
|
|
|
$text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
|
|
48
|
|
|
|
|
49
|
|
|
// Load into DOMDOcument
|
|
50
|
|
|
$xmlobj = new DOMDocument();
|
|
51
|
|
|
@$xmlobj->loadXML($text);
|
|
|
|
|
|
|
52
|
|
|
if ($xmlobj === false) {
|
|
53
|
|
|
throw new Exception("Invalid GeoRSS: ". $text);
|
|
54
|
|
|
}
|
|
55
|
|
|
|
|
56
|
|
|
$this->xmlobj = $xmlobj;
|
|
|
|
|
|
|
57
|
|
|
try {
|
|
58
|
|
|
$geom = $this->geomFromXML();
|
|
59
|
|
|
} catch(InvalidText $e) {
|
|
|
|
|
|
|
60
|
|
|
throw new Exception("Cannot Read Geometry From GeoRSS: ". $text);
|
|
61
|
|
|
} catch(Exception $e) {
|
|
62
|
|
|
throw $e;
|
|
63
|
|
|
}
|
|
64
|
|
|
|
|
65
|
|
|
return $geom;
|
|
66
|
|
|
}
|
|
67
|
|
|
|
|
68
|
|
|
protected function geomFromXML() {
|
|
69
|
|
|
$geometries = array();
|
|
70
|
|
|
$geometries = array_merge($geometries, $this->parsePoints());
|
|
71
|
|
|
$geometries = array_merge($geometries, $this->parseLines());
|
|
72
|
|
|
$geometries = array_merge($geometries, $this->parsePolygons());
|
|
73
|
|
|
$geometries = array_merge($geometries, $this->parseBoxes());
|
|
74
|
|
|
$geometries = array_merge($geometries, $this->parseCircles());
|
|
75
|
|
|
|
|
76
|
|
|
if (empty($geometries)) {
|
|
77
|
|
|
throw new Exception("Invalid / Empty GeoRSS");
|
|
78
|
|
|
}
|
|
79
|
|
|
|
|
80
|
|
|
return geoPHP::geometryReduce($geometries);
|
|
81
|
|
|
}
|
|
82
|
|
|
|
|
83
|
|
|
protected function getPointsFromCoords($string) {
|
|
84
|
|
|
$coords = array();
|
|
85
|
|
|
$latlon = explode(' ',$string);
|
|
86
|
|
|
foreach ($latlon as $key => $item) {
|
|
87
|
|
|
if (!($key % 2)) {
|
|
88
|
|
|
// It's a latitude
|
|
89
|
|
|
$lat = $item;
|
|
90
|
|
|
}
|
|
91
|
|
|
else {
|
|
92
|
|
|
// It's a longitude
|
|
93
|
|
|
$lon = $item;
|
|
94
|
|
|
$coords[] = new Point($lon, $lat);
|
|
|
|
|
|
|
95
|
|
|
}
|
|
96
|
|
|
}
|
|
97
|
|
|
return $coords;
|
|
98
|
|
|
}
|
|
99
|
|
|
|
|
100
|
|
|
protected function parsePoints() {
|
|
101
|
|
|
$points = array();
|
|
102
|
|
|
$pt_elements = $this->xmlobj->getElementsByTagName('point');
|
|
103
|
|
|
foreach ($pt_elements as $pt) {
|
|
104
|
|
|
$point_array = $this->getPointsFromCoords(trim($pt->firstChild->nodeValue));
|
|
105
|
|
|
$points[] = $point_array[0];
|
|
106
|
|
|
}
|
|
107
|
|
|
return $points;
|
|
108
|
|
|
}
|
|
109
|
|
|
|
|
110
|
|
|
protected function parseLines() {
|
|
111
|
|
|
$lines = array();
|
|
112
|
|
|
$line_elements = $this->xmlobj->getElementsByTagName('line');
|
|
113
|
|
|
foreach ($line_elements as $line) {
|
|
114
|
|
|
$components = $this->getPointsFromCoords(trim($line->firstChild->nodeValue));
|
|
115
|
|
|
$lines[] = new LineString($components);
|
|
116
|
|
|
}
|
|
117
|
|
|
return $lines;
|
|
118
|
|
|
}
|
|
119
|
|
|
|
|
120
|
|
|
protected function parsePolygons() {
|
|
121
|
|
|
$polygons = array();
|
|
122
|
|
|
$poly_elements = $this->xmlobj->getElementsByTagName('polygon');
|
|
123
|
|
|
foreach ($poly_elements as $poly) {
|
|
124
|
|
|
if ($poly->hasChildNodes()) {
|
|
125
|
|
|
$points = $this->getPointsFromCoords(trim($poly->firstChild->nodeValue));
|
|
126
|
|
|
$exterior_ring = new LineString($points);
|
|
127
|
|
|
$polygons[] = new Polygon(array($exterior_ring));
|
|
128
|
|
|
}
|
|
129
|
|
|
else {
|
|
130
|
|
|
// It's an EMPTY polygon
|
|
131
|
|
|
$polygons[] = new Polygon();
|
|
132
|
|
|
}
|
|
133
|
|
|
}
|
|
134
|
|
|
return $polygons;
|
|
135
|
|
|
}
|
|
136
|
|
|
|
|
137
|
|
|
// Boxes are rendered into polygons
|
|
138
|
|
|
protected function parseBoxes() {
|
|
139
|
|
|
$polygons = array();
|
|
140
|
|
|
$box_elements = $this->xmlobj->getElementsByTagName('box');
|
|
141
|
|
|
foreach ($box_elements as $box) {
|
|
142
|
|
|
$parts = explode(' ',trim($box->firstChild->nodeValue));
|
|
143
|
|
|
$components = array(
|
|
144
|
|
|
new Point($parts[3], $parts[2]),
|
|
145
|
|
|
new Point($parts[3], $parts[0]),
|
|
146
|
|
|
new Point($parts[1], $parts[0]),
|
|
147
|
|
|
new Point($parts[1], $parts[2]),
|
|
148
|
|
|
new Point($parts[3], $parts[2]),
|
|
149
|
|
|
);
|
|
150
|
|
|
$exterior_ring = new LineString($components);
|
|
151
|
|
|
$polygons[] = new Polygon(array($exterior_ring));
|
|
152
|
|
|
}
|
|
153
|
|
|
return $polygons;
|
|
154
|
|
|
}
|
|
155
|
|
|
|
|
156
|
|
|
// Circles are rendered into points
|
|
157
|
|
|
// @@TODO: Add good support once we have circular-string geometry support
|
|
158
|
|
|
protected function parseCircles() {
|
|
159
|
|
|
$points = array();
|
|
160
|
|
|
$circle_elements = $this->xmlobj->getElementsByTagName('circle');
|
|
161
|
|
|
foreach ($circle_elements as $circle) {
|
|
162
|
|
|
$parts = explode(' ',trim($circle->firstChild->nodeValue));
|
|
163
|
|
|
$points[] = new Point($parts[1], $parts[0]);
|
|
164
|
|
|
}
|
|
165
|
|
|
return $points;
|
|
166
|
|
|
}
|
|
167
|
|
|
|
|
168
|
|
|
protected function geometryToGeoRSS($geom) {
|
|
169
|
|
|
$type = strtolower($geom->getGeomType());
|
|
170
|
|
|
switch ($type) {
|
|
171
|
|
|
case 'point':
|
|
172
|
|
|
return $this->pointToGeoRSS($geom);
|
|
173
|
|
|
break;
|
|
|
|
|
|
|
174
|
|
|
case 'linestring':
|
|
175
|
|
|
return $this->linestringToGeoRSS($geom);
|
|
176
|
|
|
break;
|
|
|
|
|
|
|
177
|
|
|
case 'polygon':
|
|
178
|
|
|
return $this->PolygonToGeoRSS($geom);
|
|
179
|
|
|
break;
|
|
|
|
|
|
|
180
|
|
|
case 'multipoint':
|
|
181
|
|
|
case 'multilinestring':
|
|
182
|
|
|
case 'multipolygon':
|
|
183
|
|
|
case 'geometrycollection':
|
|
184
|
|
|
return $this->collectionToGeoRSS($geom);
|
|
185
|
|
|
break;
|
|
|
|
|
|
|
186
|
|
|
}
|
|
187
|
|
|
return $output;
|
|
|
|
|
|
|
188
|
|
|
}
|
|
189
|
|
|
|
|
190
|
|
|
private function pointToGeoRSS($geom) {
|
|
191
|
|
|
return '<'.$this->nss.'point>'.$geom->getY().' '.$geom->getX().'</'.$this->nss.'point>';
|
|
192
|
|
|
}
|
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
private function linestringToGeoRSS($geom) {
|
|
196
|
|
|
$output = '<'.$this->nss.'line>';
|
|
197
|
|
|
foreach ($geom->getComponents() as $k => $point) {
|
|
198
|
|
|
$output .= $point->getY().' '.$point->getX();
|
|
199
|
|
|
if ($k < ($geom->numGeometries() -1)) $output .= ' ';
|
|
200
|
|
|
}
|
|
201
|
|
|
$output .= '</'.$this->nss.'line>';
|
|
202
|
|
|
return $output;
|
|
203
|
|
|
}
|
|
204
|
|
|
|
|
205
|
|
|
private function polygonToGeoRSS($geom) {
|
|
206
|
|
|
$output = '<'.$this->nss.'polygon>';
|
|
207
|
|
|
$exterior_ring = $geom->exteriorRing();
|
|
208
|
|
|
foreach ($exterior_ring->getComponents() as $k => $point) {
|
|
209
|
|
|
$output .= $point->getY().' '.$point->getX();
|
|
210
|
|
|
if ($k < ($exterior_ring->numGeometries() -1)) $output .= ' ';
|
|
211
|
|
|
}
|
|
212
|
|
|
$output .= '</'.$this->nss.'polygon>';
|
|
213
|
|
|
return $output;
|
|
214
|
|
|
}
|
|
215
|
|
|
|
|
216
|
|
|
public function collectionToGeoRSS($geom) {
|
|
217
|
|
|
$georss = '<'.$this->nss.'where>';
|
|
218
|
|
|
$components = $geom->getComponents();
|
|
|
|
|
|
|
219
|
|
|
foreach ($geom->getComponents() as $comp) {
|
|
220
|
|
|
$georss .= $this->geometryToGeoRSS($comp);
|
|
221
|
|
|
}
|
|
222
|
|
|
|
|
223
|
|
|
$georss .= '</'.$this->nss.'where>';
|
|
224
|
|
|
|
|
225
|
|
|
return $georss;
|
|
226
|
|
|
}
|
|
227
|
|
|
|
|
228
|
|
|
}
|
|
229
|
|
|
|
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.