@@ -13,15 +13,15 @@ |
||
13 | 13 | * @return string The Extended-WKT string representation of the input geometries |
14 | 14 | */ |
15 | 15 | public function write(Geometry $geometry) { |
16 | - $srid = $geometry->SRID(); |
|
17 | - $wkt = ''; |
|
18 | - if ($srid) { |
|
19 | - $wkt = 'SRID=' . $srid . ';'; |
|
20 | - $wkt .= $geometry->out('wkt'); |
|
21 | - return $wkt; |
|
22 | - } |
|
23 | - else { |
|
24 | - return $geometry->out('wkt'); |
|
25 | - } |
|
16 | + $srid = $geometry->SRID(); |
|
17 | + $wkt = ''; |
|
18 | + if ($srid) { |
|
19 | + $wkt = 'SRID=' . $srid . ';'; |
|
20 | + $wkt .= $geometry->out('wkt'); |
|
21 | + return $wkt; |
|
22 | + } |
|
23 | + else { |
|
24 | + return $geometry->out('wkt'); |
|
25 | + } |
|
26 | 26 | } |
27 | 27 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * @return Geometry|GeometryCollection |
24 | 24 | */ |
25 | 25 | public function read($gpx) { |
26 | - return $this->geomFromText($gpx); |
|
26 | + return $this->geomFromText($gpx); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
@@ -34,195 +34,195 @@ discard block |
||
34 | 34 | * @return string The georss string representation of the input geometries |
35 | 35 | */ |
36 | 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); |
|
37 | + if ($namespace) { |
|
38 | + $this->namespace = $namespace; |
|
39 | + $this->nss = $namespace.':'; |
|
40 | + } |
|
41 | + return $this->geometryToGeoRSS($geometry); |
|
42 | 42 | } |
43 | 43 | |
44 | 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); |
|
45 | + // Change to lower-case, strip all CDATA, and de-namespace |
|
46 | + $text = strtolower($text); |
|
47 | + $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text); |
|
48 | 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 | - } |
|
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 | 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 | - } |
|
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 | 64 | |
65 | - return $geom; |
|
65 | + return $geom; |
|
66 | 66 | } |
67 | 67 | |
68 | 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()); |
|
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 | 75 | |
76 | - if (empty($geometries)) { |
|
77 | - throw new Exception("Invalid / Empty GeoRSS"); |
|
78 | - } |
|
76 | + if (empty($geometries)) { |
|
77 | + throw new Exception("Invalid / Empty GeoRSS"); |
|
78 | + } |
|
79 | 79 | |
80 | - return geoPHP::geometryReduce($geometries); |
|
80 | + return geoPHP::geometryReduce($geometries); |
|
81 | 81 | } |
82 | 82 | |
83 | 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; |
|
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 | 98 | } |
99 | 99 | |
100 | 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; |
|
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 | 108 | } |
109 | 109 | |
110 | 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; |
|
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 | 118 | } |
119 | 119 | |
120 | 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; |
|
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 | 135 | } |
136 | 136 | |
137 | 137 | // Boxes are rendered into polygons |
138 | 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; |
|
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 | 154 | } |
155 | 155 | |
156 | 156 | // Circles are rendered into points |
157 | 157 | // @@TODO: Add good support once we have circular-string geometry support |
158 | 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; |
|
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 | 166 | } |
167 | 167 | |
168 | 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; |
|
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 | 188 | } |
189 | 189 | |
190 | 190 | private function pointToGeoRSS($geom) { |
191 | - return '<'.$this->nss.'point>'.$geom->getY().' '.$geom->getX().'</'.$this->nss.'point>'; |
|
191 | + return '<'.$this->nss.'point>'.$geom->getY().' '.$geom->getX().'</'.$this->nss.'point>'; |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | |
195 | 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; |
|
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 | 203 | } |
204 | 204 | |
205 | 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; |
|
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 | 214 | } |
215 | 215 | |
216 | 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 | - } |
|
217 | + $georss = '<'.$this->nss.'where>'; |
|
218 | + $components = $geom->getComponents(); |
|
219 | + foreach ($geom->getComponents() as $comp) { |
|
220 | + $georss .= $this->geometryToGeoRSS($comp); |
|
221 | + } |
|
222 | 222 | |
223 | - $georss .= '</'.$this->nss.'where>'; |
|
223 | + $georss .= '</'.$this->nss.'where>'; |
|
224 | 224 | |
225 | - return $georss; |
|
225 | + return $georss; |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | } |
@@ -13,185 +13,185 @@ discard block |
||
13 | 13 | * @return Geometry |
14 | 14 | */ |
15 | 15 | public function read($wkt) { |
16 | - $wkt = trim($wkt); |
|
16 | + $wkt = trim($wkt); |
|
17 | 17 | |
18 | - // If it contains a ';', then it contains additional SRID data |
|
19 | - if (strpos($wkt,';')) { |
|
20 | - $parts = explode(';', $wkt); |
|
21 | - $wkt = $parts[1]; |
|
22 | - $eparts = explode('=',$parts[0]); |
|
23 | - $srid = $eparts[1]; |
|
24 | - } |
|
25 | - else { |
|
26 | - $srid = NULL; |
|
27 | - } |
|
18 | + // If it contains a ';', then it contains additional SRID data |
|
19 | + if (strpos($wkt,';')) { |
|
20 | + $parts = explode(';', $wkt); |
|
21 | + $wkt = $parts[1]; |
|
22 | + $eparts = explode('=',$parts[0]); |
|
23 | + $srid = $eparts[1]; |
|
24 | + } |
|
25 | + else { |
|
26 | + $srid = NULL; |
|
27 | + } |
|
28 | 28 | |
29 | - // If geos is installed, then we take a shortcut and let it parse the WKT |
|
30 | - if (geoPHP::geosInstalled()) { |
|
31 | - $reader = new GEOSWKTReader(); |
|
32 | - if ($srid) { |
|
33 | - $geom = geoPHP::geosToGeometry($reader->read($wkt)); |
|
34 | - $geom->setSRID($srid); |
|
35 | - return $geom; |
|
36 | - } |
|
37 | - else { |
|
38 | - return geoPHP::geosToGeometry($reader->read($wkt)); |
|
39 | - } |
|
40 | - } |
|
41 | - $wkt = str_replace(', ', ',', $wkt); |
|
29 | + // If geos is installed, then we take a shortcut and let it parse the WKT |
|
30 | + if (geoPHP::geosInstalled()) { |
|
31 | + $reader = new GEOSWKTReader(); |
|
32 | + if ($srid) { |
|
33 | + $geom = geoPHP::geosToGeometry($reader->read($wkt)); |
|
34 | + $geom->setSRID($srid); |
|
35 | + return $geom; |
|
36 | + } |
|
37 | + else { |
|
38 | + return geoPHP::geosToGeometry($reader->read($wkt)); |
|
39 | + } |
|
40 | + } |
|
41 | + $wkt = str_replace(', ', ',', $wkt); |
|
42 | 42 | |
43 | - // For each geometry type, check to see if we have a match at the |
|
44 | - // beggining of the string. If we do, then parse using that type |
|
45 | - foreach (geoPHP::geometryList() as $geom_type) { |
|
46 | - $wkt_geom = strtoupper($geom_type); |
|
47 | - if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) { |
|
48 | - $data_string = $this->getDataString($wkt); |
|
49 | - $method = 'parse'.$geom_type; |
|
43 | + // For each geometry type, check to see if we have a match at the |
|
44 | + // beggining of the string. If we do, then parse using that type |
|
45 | + foreach (geoPHP::geometryList() as $geom_type) { |
|
46 | + $wkt_geom = strtoupper($geom_type); |
|
47 | + if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) { |
|
48 | + $data_string = $this->getDataString($wkt); |
|
49 | + $method = 'parse'.$geom_type; |
|
50 | 50 | |
51 | - if ($srid) { |
|
52 | - $geom = $this->$method($data_string); |
|
53 | - $geom->setSRID($srid); |
|
54 | - return $geom; |
|
55 | - } |
|
56 | - else { |
|
57 | - return $this->$method($data_string); |
|
58 | - } |
|
51 | + if ($srid) { |
|
52 | + $geom = $this->$method($data_string); |
|
53 | + $geom->setSRID($srid); |
|
54 | + return $geom; |
|
55 | + } |
|
56 | + else { |
|
57 | + return $this->$method($data_string); |
|
58 | + } |
|
59 | 59 | |
60 | - } |
|
61 | - } |
|
60 | + } |
|
61 | + } |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | private function parsePoint($data_string) { |
65 | - $data_string = $this->trimParens($data_string); |
|
66 | - $parts = explode(' ',$data_string); |
|
67 | - return new Point($parts[0], $parts[1]); |
|
65 | + $data_string = $this->trimParens($data_string); |
|
66 | + $parts = explode(' ',$data_string); |
|
67 | + return new Point($parts[0], $parts[1]); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | private function parseLineString($data_string) { |
71 | - $data_string = $this->trimParens($data_string); |
|
71 | + $data_string = $this->trimParens($data_string); |
|
72 | 72 | |
73 | - // If it's marked as empty, then return an empty line |
|
74 | - if ($data_string == 'EMPTY') return new LineString(); |
|
73 | + // If it's marked as empty, then return an empty line |
|
74 | + if ($data_string == 'EMPTY') return new LineString(); |
|
75 | 75 | |
76 | - $parts = explode(',',$data_string); |
|
77 | - $points = array(); |
|
78 | - foreach ($parts as $part) { |
|
79 | - $points[] = $this->parsePoint($part); |
|
80 | - } |
|
81 | - return new LineString($points); |
|
76 | + $parts = explode(',',$data_string); |
|
77 | + $points = array(); |
|
78 | + foreach ($parts as $part) { |
|
79 | + $points[] = $this->parsePoint($part); |
|
80 | + } |
|
81 | + return new LineString($points); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | private function parsePolygon($data_string) { |
85 | - $data_string = $this->trimParens($data_string); |
|
85 | + $data_string = $this->trimParens($data_string); |
|
86 | 86 | |
87 | - // If it's marked as empty, then return an empty polygon |
|
88 | - if ($data_string == 'EMPTY') return new Polygon(); |
|
87 | + // If it's marked as empty, then return an empty polygon |
|
88 | + if ($data_string == 'EMPTY') return new Polygon(); |
|
89 | 89 | |
90 | - $parts = explode('),(',$data_string); |
|
91 | - $lines = array(); |
|
92 | - foreach ($parts as $part) { |
|
93 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
94 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
95 | - $lines[] = $this->parseLineString($part); |
|
96 | - } |
|
97 | - return new Polygon($lines); |
|
90 | + $parts = explode('),(',$data_string); |
|
91 | + $lines = array(); |
|
92 | + foreach ($parts as $part) { |
|
93 | + if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
94 | + if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
95 | + $lines[] = $this->parseLineString($part); |
|
96 | + } |
|
97 | + return new Polygon($lines); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | private function parseMultiPoint($data_string) { |
101 | - $data_string = $this->trimParens($data_string); |
|
101 | + $data_string = $this->trimParens($data_string); |
|
102 | 102 | |
103 | - // If it's marked as empty, then return an empty MutiPoint |
|
104 | - if ($data_string == 'EMPTY') return new MultiPoint(); |
|
103 | + // If it's marked as empty, then return an empty MutiPoint |
|
104 | + if ($data_string == 'EMPTY') return new MultiPoint(); |
|
105 | 105 | |
106 | - $parts = explode(',',$data_string); |
|
107 | - $points = array(); |
|
108 | - foreach ($parts as $part) { |
|
109 | - $points[] = $this->parsePoint($part); |
|
110 | - } |
|
111 | - return new MultiPoint($points); |
|
106 | + $parts = explode(',',$data_string); |
|
107 | + $points = array(); |
|
108 | + foreach ($parts as $part) { |
|
109 | + $points[] = $this->parsePoint($part); |
|
110 | + } |
|
111 | + return new MultiPoint($points); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | private function parseMultiLineString($data_string) { |
115 | - $data_string = $this->trimParens($data_string); |
|
115 | + $data_string = $this->trimParens($data_string); |
|
116 | 116 | |
117 | - // If it's marked as empty, then return an empty multi-linestring |
|
118 | - if ($data_string == 'EMPTY') return new MultiLineString(); |
|
117 | + // If it's marked as empty, then return an empty multi-linestring |
|
118 | + if ($data_string == 'EMPTY') return new MultiLineString(); |
|
119 | 119 | |
120 | - $parts = explode('),(',$data_string); |
|
121 | - $lines = array(); |
|
122 | - foreach ($parts as $part) { |
|
123 | - // Repair the string if the explode broke it |
|
124 | - if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
125 | - if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
126 | - $lines[] = $this->parseLineString($part); |
|
127 | - } |
|
128 | - return new MultiLineString($lines); |
|
120 | + $parts = explode('),(',$data_string); |
|
121 | + $lines = array(); |
|
122 | + foreach ($parts as $part) { |
|
123 | + // Repair the string if the explode broke it |
|
124 | + if (!$this->beginsWith($part,'(')) $part = '(' . $part; |
|
125 | + if (!$this->endsWith($part,')')) $part = $part . ')'; |
|
126 | + $lines[] = $this->parseLineString($part); |
|
127 | + } |
|
128 | + return new MultiLineString($lines); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | private function parseMultiPolygon($data_string) { |
132 | - $data_string = $this->trimParens($data_string); |
|
132 | + $data_string = $this->trimParens($data_string); |
|
133 | 133 | |
134 | - // If it's marked as empty, then return an empty multi-polygon |
|
135 | - if ($data_string == 'EMPTY') return new MultiPolygon(); |
|
134 | + // If it's marked as empty, then return an empty multi-polygon |
|
135 | + if ($data_string == 'EMPTY') return new MultiPolygon(); |
|
136 | 136 | |
137 | - $parts = explode(')),((',$data_string); |
|
138 | - $polys = array(); |
|
139 | - foreach ($parts as $part) { |
|
140 | - // Repair the string if the explode broke it |
|
141 | - if (!$this->beginsWith($part,'((')) $part = '((' . $part; |
|
142 | - if (!$this->endsWith($part,'))')) $part = $part . '))'; |
|
143 | - $polys[] = $this->parsePolygon($part); |
|
144 | - } |
|
145 | - return new MultiPolygon($polys); |
|
137 | + $parts = explode(')),((',$data_string); |
|
138 | + $polys = array(); |
|
139 | + foreach ($parts as $part) { |
|
140 | + // Repair the string if the explode broke it |
|
141 | + if (!$this->beginsWith($part,'((')) $part = '((' . $part; |
|
142 | + if (!$this->endsWith($part,'))')) $part = $part . '))'; |
|
143 | + $polys[] = $this->parsePolygon($part); |
|
144 | + } |
|
145 | + return new MultiPolygon($polys); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | private function parseGeometryCollection($data_string) { |
149 | - $data_string = $this->trimParens($data_string); |
|
149 | + $data_string = $this->trimParens($data_string); |
|
150 | 150 | |
151 | - // If it's marked as empty, then return an empty geom-collection |
|
152 | - if ($data_string == 'EMPTY') return new GeometryCollection(); |
|
151 | + // If it's marked as empty, then return an empty geom-collection |
|
152 | + if ($data_string == 'EMPTY') return new GeometryCollection(); |
|
153 | 153 | |
154 | - $geometries = array(); |
|
155 | - $matches = array(); |
|
156 | - $str = preg_replace('/,\s*([A-Za-z])/', '|$1', $data_string); |
|
157 | - $components = explode('|', trim($str)); |
|
154 | + $geometries = array(); |
|
155 | + $matches = array(); |
|
156 | + $str = preg_replace('/,\s*([A-Za-z])/', '|$1', $data_string); |
|
157 | + $components = explode('|', trim($str)); |
|
158 | 158 | |
159 | - foreach ($components as $component) { |
|
160 | - $geometries[] = $this->read($component); |
|
161 | - } |
|
162 | - return new GeometryCollection($geometries); |
|
159 | + foreach ($components as $component) { |
|
160 | + $geometries[] = $this->read($component); |
|
161 | + } |
|
162 | + return new GeometryCollection($geometries); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | protected function getDataString($wkt) { |
166 | - $first_paren = strpos($wkt, '('); |
|
166 | + $first_paren = strpos($wkt, '('); |
|
167 | 167 | |
168 | - if ($first_paren !== FALSE) { |
|
169 | - return substr($wkt, $first_paren); |
|
170 | - } |
|
171 | - return FALSE; |
|
168 | + if ($first_paren !== FALSE) { |
|
169 | + return substr($wkt, $first_paren); |
|
170 | + } |
|
171 | + return FALSE; |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
175 | 175 | * Trim the parenthesis and spaces |
176 | 176 | */ |
177 | 177 | protected function trimParens($str) { |
178 | - $str = trim($str); |
|
178 | + $str = trim($str); |
|
179 | 179 | |
180 | - // We want to only strip off one set of parenthesis |
|
181 | - if ($this->beginsWith($str, '(')) { |
|
182 | - return substr($str,1,-1); |
|
183 | - } |
|
184 | - else return $str; |
|
180 | + // We want to only strip off one set of parenthesis |
|
181 | + if ($this->beginsWith($str, '(')) { |
|
182 | + return substr($str,1,-1); |
|
183 | + } |
|
184 | + else return $str; |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | protected function beginsWith($str, $char) { |
188 | - if (substr($str,0,strlen($char)) == $char) return TRUE; |
|
189 | - else return FALSE; |
|
188 | + if (substr($str,0,strlen($char)) == $char) return TRUE; |
|
189 | + else return FALSE; |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | protected function endsWith($str, $char) { |
193 | - if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
|
194 | - else return FALSE; |
|
193 | + if (substr($str,(0 - strlen($char))) == $char) return TRUE; |
|
194 | + else return FALSE; |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -202,19 +202,19 @@ discard block |
||
202 | 202 | * @return string The WKT string representation of the input geometries |
203 | 203 | */ |
204 | 204 | public function write(Geometry $geometry) { |
205 | - // If geos is installed, then we take a shortcut and let it write the WKT |
|
206 | - if (geoPHP::geosInstalled()) { |
|
207 | - $writer = new GEOSWKTWriter(); |
|
208 | - $writer->setTrim(TRUE); |
|
209 | - return $writer->write($geometry->geos()); |
|
210 | - } |
|
205 | + // If geos is installed, then we take a shortcut and let it write the WKT |
|
206 | + if (geoPHP::geosInstalled()) { |
|
207 | + $writer = new GEOSWKTWriter(); |
|
208 | + $writer->setTrim(TRUE); |
|
209 | + return $writer->write($geometry->geos()); |
|
210 | + } |
|
211 | 211 | |
212 | - if ($geometry->isEmpty()) { |
|
213 | - return strtoupper($geometry->geometryType()).' EMPTY'; |
|
214 | - } |
|
215 | - else if ($data = $this->extractData($geometry)) { |
|
216 | - return strtoupper($geometry->geometryType()).' ('.$data.')'; |
|
217 | - } |
|
212 | + if ($geometry->isEmpty()) { |
|
213 | + return strtoupper($geometry->geometryType()).' EMPTY'; |
|
214 | + } |
|
215 | + else if ($data = $this->extractData($geometry)) { |
|
216 | + return strtoupper($geometry->geometryType()).' ('.$data.')'; |
|
217 | + } |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -225,28 +225,28 @@ discard block |
||
225 | 225 | * @return string |
226 | 226 | */ |
227 | 227 | public function extractData($geometry) { |
228 | - $parts = array(); |
|
229 | - switch ($geometry->geometryType()) { |
|
230 | - case 'Point': |
|
231 | - return $geometry->getX().' '.$geometry->getY(); |
|
232 | - case 'LineString': |
|
233 | - foreach ($geometry->getComponents() as $component) { |
|
234 | - $parts[] = $this->extractData($component); |
|
235 | - } |
|
236 | - return implode(', ', $parts); |
|
237 | - case 'Polygon': |
|
238 | - case 'MultiPoint': |
|
239 | - case 'MultiLineString': |
|
240 | - case 'MultiPolygon': |
|
241 | - foreach ($geometry->getComponents() as $component) { |
|
242 | - $parts[] = '('.$this->extractData($component).')'; |
|
243 | - } |
|
244 | - return implode(', ', $parts); |
|
245 | - case 'GeometryCollection': |
|
246 | - foreach ($geometry->getComponents() as $component) { |
|
247 | - $parts[] = strtoupper($component->geometryType()).' ('.$this->extractData($component).')'; |
|
248 | - } |
|
249 | - return implode(', ', $parts); |
|
250 | - } |
|
228 | + $parts = array(); |
|
229 | + switch ($geometry->geometryType()) { |
|
230 | + case 'Point': |
|
231 | + return $geometry->getX().' '.$geometry->getY(); |
|
232 | + case 'LineString': |
|
233 | + foreach ($geometry->getComponents() as $component) { |
|
234 | + $parts[] = $this->extractData($component); |
|
235 | + } |
|
236 | + return implode(', ', $parts); |
|
237 | + case 'Polygon': |
|
238 | + case 'MultiPoint': |
|
239 | + case 'MultiLineString': |
|
240 | + case 'MultiPolygon': |
|
241 | + foreach ($geometry->getComponents() as $component) { |
|
242 | + $parts[] = '('.$this->extractData($component).')'; |
|
243 | + } |
|
244 | + return implode(', ', $parts); |
|
245 | + case 'GeometryCollection': |
|
246 | + foreach ($geometry->getComponents() as $component) { |
|
247 | + $parts[] = strtoupper($component->geometryType()).' ('.$this->extractData($component).')'; |
|
248 | + } |
|
249 | + return implode(', ', $parts); |
|
250 | + } |
|
251 | 251 | } |
252 | 252 | } |
@@ -30,55 +30,55 @@ discard block |
||
30 | 30 | * @return Geometry|GeometryCollection |
31 | 31 | */ |
32 | 32 | public function read($address, $return_type = 'point', $bounds = FALSE, $return_multiple = FALSE) { |
33 | - if (is_array($address)) $address = join(',', $address); |
|
33 | + if (is_array($address)) $address = join(',', $address); |
|
34 | 34 | |
35 | - if (gettype($bounds) == 'object') { |
|
36 | - $bounds = $bounds->getBBox(); |
|
37 | - } |
|
38 | - if (gettype($bounds) == 'array') { |
|
39 | - $bounds_string = '&bounds='.$bounds['miny'].','.$bounds['minx'].'|'.$bounds['maxy'].','.$bounds['maxx']; |
|
40 | - } |
|
41 | - else { |
|
42 | - $bounds_string = ''; |
|
43 | - } |
|
35 | + if (gettype($bounds) == 'object') { |
|
36 | + $bounds = $bounds->getBBox(); |
|
37 | + } |
|
38 | + if (gettype($bounds) == 'array') { |
|
39 | + $bounds_string = '&bounds='.$bounds['miny'].','.$bounds['minx'].'|'.$bounds['maxy'].','.$bounds['maxx']; |
|
40 | + } |
|
41 | + else { |
|
42 | + $bounds_string = ''; |
|
43 | + } |
|
44 | 44 | |
45 | - $url = "http://maps.googleapis.com/maps/api/geocode/json"; |
|
46 | - $url .= '?address='. urlencode($address); |
|
47 | - $url .= $bounds_string; |
|
48 | - $url .= '&sensor=false'; |
|
49 | - $this->result = json_decode(@file_get_contents($url)); |
|
45 | + $url = "http://maps.googleapis.com/maps/api/geocode/json"; |
|
46 | + $url .= '?address='. urlencode($address); |
|
47 | + $url .= $bounds_string; |
|
48 | + $url .= '&sensor=false'; |
|
49 | + $this->result = json_decode(@file_get_contents($url)); |
|
50 | 50 | |
51 | - if ($this->result->status == 'OK') { |
|
52 | - if ($return_multiple == FALSE) { |
|
53 | - if ($return_type == 'point') { |
|
54 | - return $this->getPoint(); |
|
55 | - } |
|
56 | - if ($return_type == 'bounds' || $return_type == 'polygon') { |
|
57 | - return $this->getPolygon(); |
|
58 | - } |
|
59 | - } |
|
60 | - if ($return_multiple == TRUE) { |
|
61 | - if ($return_type == 'point') { |
|
62 | - $points = array(); |
|
63 | - foreach ($this->result->results as $delta => $item) { |
|
64 | - $points[] = $this->getPoint($delta); |
|
65 | - } |
|
66 | - return new MultiPoint($points); |
|
67 | - } |
|
68 | - if ($return_type == 'bounds' || $return_type == 'polygon') { |
|
69 | - $polygons = array(); |
|
70 | - foreach ($this->result->results as $delta => $item) { |
|
71 | - $polygons[] = $this->getPolygon($delta); |
|
72 | - } |
|
73 | - return new MultiPolygon($polygons); |
|
74 | - } |
|
75 | - } |
|
76 | - } |
|
77 | - else { |
|
78 | - if ($this->result->status) throw new Exception('Error in Google Geocoder: '.$this->result->status); |
|
79 | - else throw new Exception('Unknown error in Google Geocoder'); |
|
80 | - return FALSE; |
|
81 | - } |
|
51 | + if ($this->result->status == 'OK') { |
|
52 | + if ($return_multiple == FALSE) { |
|
53 | + if ($return_type == 'point') { |
|
54 | + return $this->getPoint(); |
|
55 | + } |
|
56 | + if ($return_type == 'bounds' || $return_type == 'polygon') { |
|
57 | + return $this->getPolygon(); |
|
58 | + } |
|
59 | + } |
|
60 | + if ($return_multiple == TRUE) { |
|
61 | + if ($return_type == 'point') { |
|
62 | + $points = array(); |
|
63 | + foreach ($this->result->results as $delta => $item) { |
|
64 | + $points[] = $this->getPoint($delta); |
|
65 | + } |
|
66 | + return new MultiPoint($points); |
|
67 | + } |
|
68 | + if ($return_type == 'bounds' || $return_type == 'polygon') { |
|
69 | + $polygons = array(); |
|
70 | + foreach ($this->result->results as $delta => $item) { |
|
71 | + $polygons[] = $this->getPolygon($delta); |
|
72 | + } |
|
73 | + return new MultiPolygon($polygons); |
|
74 | + } |
|
75 | + } |
|
76 | + } |
|
77 | + else { |
|
78 | + if ($this->result->status) throw new Exception('Error in Google Geocoder: '.$this->result->status); |
|
79 | + else throw new Exception('Unknown error in Google Geocoder'); |
|
80 | + return FALSE; |
|
81 | + } |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -90,69 +90,69 @@ discard block |
||
90 | 90 | * @return string Does a reverse geocode of the geometry |
91 | 91 | */ |
92 | 92 | public function write(Geometry $geometry, $return_type = 'string') { |
93 | - $centroid = $geometry->getCentroid(); |
|
94 | - $lat = $centroid->getY(); |
|
95 | - $lon = $centroid->getX(); |
|
93 | + $centroid = $geometry->getCentroid(); |
|
94 | + $lat = $centroid->getY(); |
|
95 | + $lon = $centroid->getX(); |
|
96 | 96 | |
97 | - $url = "http://maps.googleapis.com/maps/api/geocode/json"; |
|
98 | - $url .= '?latlng='.$lat.','.$lon; |
|
99 | - $url .= '&sensor=false'; |
|
100 | - $this->result = json_decode(@file_get_contents($url)); |
|
97 | + $url = "http://maps.googleapis.com/maps/api/geocode/json"; |
|
98 | + $url .= '?latlng='.$lat.','.$lon; |
|
99 | + $url .= '&sensor=false'; |
|
100 | + $this->result = json_decode(@file_get_contents($url)); |
|
101 | 101 | |
102 | - if ($this->result->status == 'OK') { |
|
103 | - if ($return_type == 'string') { |
|
104 | - return $this->result->results[0]->formatted_address; |
|
105 | - } |
|
106 | - if ($return_type == 'array') { |
|
107 | - return $this->result->results[0]->address_components; |
|
108 | - } |
|
109 | - } |
|
110 | - else { |
|
111 | - if ($this->result->status) throw new Exception('Error in Google Reverse Geocoder: '.$this->result->status); |
|
112 | - else throw new Exception('Unknown error in Google Reverse Geocoder'); |
|
113 | - return FALSE; |
|
114 | - } |
|
102 | + if ($this->result->status == 'OK') { |
|
103 | + if ($return_type == 'string') { |
|
104 | + return $this->result->results[0]->formatted_address; |
|
105 | + } |
|
106 | + if ($return_type == 'array') { |
|
107 | + return $this->result->results[0]->address_components; |
|
108 | + } |
|
109 | + } |
|
110 | + else { |
|
111 | + if ($this->result->status) throw new Exception('Error in Google Reverse Geocoder: '.$this->result->status); |
|
112 | + else throw new Exception('Unknown error in Google Reverse Geocoder'); |
|
113 | + return FALSE; |
|
114 | + } |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | private function getPoint($delta = 0) { |
118 | - $lat = $this->result->results[$delta]->geometry->location->lat; |
|
119 | - $lon = $this->result->results[$delta]->geometry->location->lng; |
|
120 | - return new Point($lon, $lat); |
|
118 | + $lat = $this->result->results[$delta]->geometry->location->lat; |
|
119 | + $lon = $this->result->results[$delta]->geometry->location->lng; |
|
120 | + return new Point($lon, $lat); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | private function getPolygon($delta = 0) { |
124 | - $points = array ( |
|
125 | - $this->getTopLeft($delta), |
|
126 | - $this->getTopRight($delta), |
|
127 | - $this->getBottomRight($delta), |
|
128 | - $this->getBottomLeft($delta), |
|
129 | - $this->getTopLeft($delta), |
|
130 | - ); |
|
131 | - $outer_ring = new LineString($points); |
|
132 | - return new Polygon(array($outer_ring)); |
|
124 | + $points = array ( |
|
125 | + $this->getTopLeft($delta), |
|
126 | + $this->getTopRight($delta), |
|
127 | + $this->getBottomRight($delta), |
|
128 | + $this->getBottomLeft($delta), |
|
129 | + $this->getTopLeft($delta), |
|
130 | + ); |
|
131 | + $outer_ring = new LineString($points); |
|
132 | + return new Polygon(array($outer_ring)); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | private function getTopLeft($delta = 0) { |
136 | - $lat = $this->result->results[$delta]->geometry->bounds->northeast->lat; |
|
137 | - $lon = $this->result->results[$delta]->geometry->bounds->southwest->lng; |
|
138 | - return new Point($lon, $lat); |
|
136 | + $lat = $this->result->results[$delta]->geometry->bounds->northeast->lat; |
|
137 | + $lon = $this->result->results[$delta]->geometry->bounds->southwest->lng; |
|
138 | + return new Point($lon, $lat); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | private function getTopRight($delta = 0) { |
142 | - $lat = $this->result->results[$delta]->geometry->bounds->northeast->lat; |
|
143 | - $lon = $this->result->results[$delta]->geometry->bounds->northeast->lng; |
|
144 | - return new Point($lon, $lat); |
|
142 | + $lat = $this->result->results[$delta]->geometry->bounds->northeast->lat; |
|
143 | + $lon = $this->result->results[$delta]->geometry->bounds->northeast->lng; |
|
144 | + return new Point($lon, $lat); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | private function getBottomLeft($delta = 0) { |
148 | - $lat = $this->result->results[$delta]->geometry->bounds->southwest->lat; |
|
149 | - $lon = $this->result->results[$delta]->geometry->bounds->southwest->lng; |
|
150 | - return new Point($lon, $lat); |
|
148 | + $lat = $this->result->results[$delta]->geometry->bounds->southwest->lat; |
|
149 | + $lon = $this->result->results[$delta]->geometry->bounds->southwest->lng; |
|
150 | + return new Point($lon, $lat); |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | private function getBottomRight($delta = 0) { |
154 | - $lat = $this->result->results[$delta]->geometry->bounds->southwest->lat; |
|
155 | - $lon = $this->result->results[$delta]->geometry->bounds->northeast->lng; |
|
156 | - return new Point($lon, $lat); |
|
154 | + $lat = $this->result->results[$delta]->geometry->bounds->southwest->lat; |
|
155 | + $lon = $this->result->results[$delta]->geometry->bounds->northeast->lng; |
|
156 | + return new Point($lon, $lat); |
|
157 | 157 | } |
158 | 158 | } |
@@ -13,33 +13,33 @@ discard block |
||
13 | 13 | * @return Geometry |
14 | 14 | */ |
15 | 15 | public function read($wkb, $is_hex_string = FALSE) { |
16 | - if ($is_hex_string) { |
|
17 | - $wkb = pack('H*',$wkb); |
|
18 | - } |
|
16 | + if ($is_hex_string) { |
|
17 | + $wkb = pack('H*',$wkb); |
|
18 | + } |
|
19 | 19 | |
20 | - // Open the wkb up in memory so we can examine the SRID |
|
21 | - $mem = fopen('php://memory', 'r+'); |
|
22 | - fwrite($mem, $wkb); |
|
23 | - fseek($mem, 0); |
|
24 | - $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
25 | - if ($base_info['s']) { |
|
26 | - $srid = current(unpack("Lsrid", fread($mem, 4))); |
|
27 | - } |
|
28 | - else { |
|
29 | - $srid = NULL; |
|
30 | - } |
|
31 | - fclose($mem); |
|
20 | + // Open the wkb up in memory so we can examine the SRID |
|
21 | + $mem = fopen('php://memory', 'r+'); |
|
22 | + fwrite($mem, $wkb); |
|
23 | + fseek($mem, 0); |
|
24 | + $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
25 | + if ($base_info['s']) { |
|
26 | + $srid = current(unpack("Lsrid", fread($mem, 4))); |
|
27 | + } |
|
28 | + else { |
|
29 | + $srid = NULL; |
|
30 | + } |
|
31 | + fclose($mem); |
|
32 | 32 | |
33 | - // Run the wkb through the normal WKB reader to get the geometry |
|
34 | - $wkb_reader = new WKB(); |
|
35 | - $geom = $wkb_reader->read($wkb); |
|
33 | + // Run the wkb through the normal WKB reader to get the geometry |
|
34 | + $wkb_reader = new WKB(); |
|
35 | + $geom = $wkb_reader->read($wkb); |
|
36 | 36 | |
37 | - // If there is an SRID, add it to the geometry |
|
38 | - if ($srid) { |
|
39 | - $geom->setSRID($srid); |
|
40 | - } |
|
37 | + // If there is an SRID, add it to the geometry |
|
38 | + if ($srid) { |
|
39 | + $geom->setSRID($srid); |
|
40 | + } |
|
41 | 41 | |
42 | - return $geom; |
|
42 | + return $geom; |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -50,47 +50,47 @@ discard block |
||
50 | 50 | * @return string The Extended-WKB binary string representation of the input geometries |
51 | 51 | */ |
52 | 52 | public function write(Geometry $geometry, $write_as_hex = FALSE) { |
53 | - // We always write into NDR (little endian) |
|
54 | - $wkb = pack('c',1); |
|
53 | + // We always write into NDR (little endian) |
|
54 | + $wkb = pack('c',1); |
|
55 | 55 | |
56 | - switch ($geometry->getGeomType()) { |
|
57 | - case 'Point'; |
|
58 | - $wkb .= pack('L',1); |
|
59 | - $wkb .= $this->writePoint($geometry); |
|
60 | - break; |
|
61 | - case 'LineString'; |
|
62 | - $wkb .= pack('L',2); |
|
63 | - $wkb .= $this->writeLineString($geometry); |
|
64 | - break; |
|
65 | - case 'Polygon'; |
|
66 | - $wkb .= pack('L',3); |
|
67 | - $wkb .= $this->writePolygon($geometry); |
|
68 | - break; |
|
69 | - case 'MultiPoint'; |
|
70 | - $wkb .= pack('L',4); |
|
71 | - $wkb .= $this->writeMulti($geometry); |
|
72 | - break; |
|
73 | - case 'MultiLineString'; |
|
74 | - $wkb .= pack('L',5); |
|
75 | - $wkb .= $this->writeMulti($geometry); |
|
76 | - break; |
|
77 | - case 'MultiPolygon'; |
|
78 | - $wkb .= pack('L',6); |
|
79 | - $wkb .= $this->writeMulti($geometry); |
|
80 | - break; |
|
81 | - case 'GeometryCollection'; |
|
82 | - $wkb .= pack('L',7); |
|
83 | - $wkb .= $this->writeMulti($geometry); |
|
84 | - break; |
|
85 | - } |
|
56 | + switch ($geometry->getGeomType()) { |
|
57 | + case 'Point'; |
|
58 | + $wkb .= pack('L',1); |
|
59 | + $wkb .= $this->writePoint($geometry); |
|
60 | + break; |
|
61 | + case 'LineString'; |
|
62 | + $wkb .= pack('L',2); |
|
63 | + $wkb .= $this->writeLineString($geometry); |
|
64 | + break; |
|
65 | + case 'Polygon'; |
|
66 | + $wkb .= pack('L',3); |
|
67 | + $wkb .= $this->writePolygon($geometry); |
|
68 | + break; |
|
69 | + case 'MultiPoint'; |
|
70 | + $wkb .= pack('L',4); |
|
71 | + $wkb .= $this->writeMulti($geometry); |
|
72 | + break; |
|
73 | + case 'MultiLineString'; |
|
74 | + $wkb .= pack('L',5); |
|
75 | + $wkb .= $this->writeMulti($geometry); |
|
76 | + break; |
|
77 | + case 'MultiPolygon'; |
|
78 | + $wkb .= pack('L',6); |
|
79 | + $wkb .= $this->writeMulti($geometry); |
|
80 | + break; |
|
81 | + case 'GeometryCollection'; |
|
82 | + $wkb .= pack('L',7); |
|
83 | + $wkb .= $this->writeMulti($geometry); |
|
84 | + break; |
|
85 | + } |
|
86 | 86 | |
87 | - if ($write_as_hex) { |
|
88 | - $unpacked = unpack('H*',$wkb); |
|
89 | - return $unpacked[1]; |
|
90 | - } |
|
91 | - else { |
|
92 | - return $wkb; |
|
93 | - } |
|
87 | + if ($write_as_hex) { |
|
88 | + $unpacked = unpack('H*',$wkb); |
|
89 | + return $unpacked[1]; |
|
90 | + } |
|
91 | + else { |
|
92 | + return $wkb; |
|
93 | + } |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | } |
@@ -16,21 +16,21 @@ discard block |
||
16 | 16 | * @see GeoAdapter::read() |
17 | 17 | */ |
18 | 18 | public function read($hash, $as_grid = FALSE) { |
19 | - $ll = $this->decode($hash); |
|
20 | - if (!$as_grid) { |
|
21 | - return new Point($ll['medlon'], $ll['medlat']); |
|
22 | - } |
|
23 | - else { |
|
24 | - return new Polygon(array( |
|
25 | - new LineString(array( |
|
26 | - new Point($ll['minlon'], $ll['maxlat']), |
|
27 | - new Point($ll['maxlon'], $ll['maxlat']), |
|
28 | - new Point($ll['maxlon'], $ll['minlat']), |
|
29 | - new Point($ll['minlon'], $ll['minlat']), |
|
30 | - new Point($ll['minlon'], $ll['maxlat']), |
|
31 | - )) |
|
32 | - )); |
|
33 | - } |
|
19 | + $ll = $this->decode($hash); |
|
20 | + if (!$as_grid) { |
|
21 | + return new Point($ll['medlon'], $ll['medlat']); |
|
22 | + } |
|
23 | + else { |
|
24 | + return new Polygon(array( |
|
25 | + new LineString(array( |
|
26 | + new Point($ll['minlon'], $ll['maxlat']), |
|
27 | + new Point($ll['maxlon'], $ll['maxlat']), |
|
28 | + new Point($ll['maxlon'], $ll['minlat']), |
|
29 | + new Point($ll['minlon'], $ll['minlat']), |
|
30 | + new Point($ll['minlon'], $ll['maxlat']), |
|
31 | + )) |
|
32 | + )); |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | /** |
@@ -40,32 +40,32 @@ discard block |
||
40 | 40 | * @see GeoAdapter::write() |
41 | 41 | */ |
42 | 42 | public function write(Geometry $geometry, $precision = NULL){ |
43 | - if ($geometry->isEmpty()) return ''; |
|
43 | + if ($geometry->isEmpty()) return ''; |
|
44 | 44 | |
45 | - if($geometry->geometryType() === 'Point'){ |
|
46 | - return $this->encodePoint($geometry, $precision); |
|
47 | - } |
|
48 | - else { |
|
49 | - // The geohash is the hash grid ID that fits the envelope |
|
50 | - $envelope = $geometry->envelope(); |
|
51 | - $geohashes = array(); |
|
52 | - $geohash = ''; |
|
53 | - foreach ($envelope->getPoints() as $point) { |
|
54 | - $geohashes[] = $this->encodePoint($point, 0.0000001); |
|
55 | - } |
|
56 | - $i = 0; |
|
57 | - while ($i < strlen($geohashes[0])) { |
|
58 | - $char = $geohashes[0][$i]; |
|
59 | - foreach ($geohashes as $hash) { |
|
60 | - if ($hash[$i] != $char) { |
|
61 | - return $geohash; |
|
62 | - } |
|
63 | - } |
|
64 | - $geohash .= $char; |
|
65 | - $i++; |
|
66 | - } |
|
67 | - return $geohash; |
|
68 | - } |
|
45 | + if($geometry->geometryType() === 'Point'){ |
|
46 | + return $this->encodePoint($geometry, $precision); |
|
47 | + } |
|
48 | + else { |
|
49 | + // The geohash is the hash grid ID that fits the envelope |
|
50 | + $envelope = $geometry->envelope(); |
|
51 | + $geohashes = array(); |
|
52 | + $geohash = ''; |
|
53 | + foreach ($envelope->getPoints() as $point) { |
|
54 | + $geohashes[] = $this->encodePoint($point, 0.0000001); |
|
55 | + } |
|
56 | + $i = 0; |
|
57 | + while ($i < strlen($geohashes[0])) { |
|
58 | + $char = $geohashes[0][$i]; |
|
59 | + foreach ($geohashes as $hash) { |
|
60 | + if ($hash[$i] != $char) { |
|
61 | + return $geohash; |
|
62 | + } |
|
63 | + } |
|
64 | + $geohash .= $char; |
|
65 | + $i++; |
|
66 | + } |
|
67 | + return $geohash; |
|
68 | + } |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -75,51 +75,51 @@ discard block |
||
75 | 75 | * @see https://github.com/asonge/php-geohash/issues/1 |
76 | 76 | */ |
77 | 77 | private function encodePoint($point, $precision = NULL){ |
78 | - if ($precision === NULL) { |
|
79 | - $lap = strlen($point->y())-strpos($point->y(),"."); |
|
80 | - $lop = strlen($point->x())-strpos($point->x(),"."); |
|
81 | - $precision = pow(10,-max($lap-1,$lop-1,0))/2; |
|
82 | - } |
|
78 | + if ($precision === NULL) { |
|
79 | + $lap = strlen($point->y())-strpos($point->y(),"."); |
|
80 | + $lop = strlen($point->x())-strpos($point->x(),"."); |
|
81 | + $precision = pow(10,-max($lap-1,$lop-1,0))/2; |
|
82 | + } |
|
83 | 83 | |
84 | - $minlat = -90; |
|
85 | - $maxlat = 90; |
|
86 | - $minlon = -180; |
|
87 | - $maxlon = 180; |
|
88 | - $latE = 90; |
|
89 | - $lonE = 180; |
|
90 | - $i = 0; |
|
91 | - $error = 180; |
|
92 | - $hash=''; |
|
93 | - while($error>=$precision) { |
|
94 | - $chr = 0; |
|
95 | - for($b=4;$b>=0;--$b) { |
|
96 | - if((1&$b) == (1&$i)) { |
|
97 | - // even char, even bit OR odd char, odd bit...a lon |
|
98 | - $next = ($minlon+$maxlon)/2; |
|
99 | - if($point->x()>$next) { |
|
100 | - $chr |= pow(2,$b); |
|
101 | - $minlon = $next; |
|
102 | - } else { |
|
103 | - $maxlon = $next; |
|
104 | - } |
|
105 | - $lonE /= 2; |
|
106 | - } else { |
|
107 | - // odd char, even bit OR even char, odd bit...a lat |
|
108 | - $next = ($minlat+$maxlat)/2; |
|
109 | - if($point->y()>$next) { |
|
110 | - $chr |= pow(2,$b); |
|
111 | - $minlat = $next; |
|
112 | - } else { |
|
113 | - $maxlat = $next; |
|
114 | - } |
|
115 | - $latE /= 2; |
|
116 | - } |
|
117 | - } |
|
118 | - $hash .= $this->table[$chr]; |
|
119 | - $i++; |
|
120 | - $error = min($latE,$lonE); |
|
121 | - } |
|
122 | - return $hash; |
|
84 | + $minlat = -90; |
|
85 | + $maxlat = 90; |
|
86 | + $minlon = -180; |
|
87 | + $maxlon = 180; |
|
88 | + $latE = 90; |
|
89 | + $lonE = 180; |
|
90 | + $i = 0; |
|
91 | + $error = 180; |
|
92 | + $hash=''; |
|
93 | + while($error>=$precision) { |
|
94 | + $chr = 0; |
|
95 | + for($b=4;$b>=0;--$b) { |
|
96 | + if((1&$b) == (1&$i)) { |
|
97 | + // even char, even bit OR odd char, odd bit...a lon |
|
98 | + $next = ($minlon+$maxlon)/2; |
|
99 | + if($point->x()>$next) { |
|
100 | + $chr |= pow(2,$b); |
|
101 | + $minlon = $next; |
|
102 | + } else { |
|
103 | + $maxlon = $next; |
|
104 | + } |
|
105 | + $lonE /= 2; |
|
106 | + } else { |
|
107 | + // odd char, even bit OR even char, odd bit...a lat |
|
108 | + $next = ($minlat+$maxlat)/2; |
|
109 | + if($point->y()>$next) { |
|
110 | + $chr |= pow(2,$b); |
|
111 | + $minlat = $next; |
|
112 | + } else { |
|
113 | + $maxlat = $next; |
|
114 | + } |
|
115 | + $latE /= 2; |
|
116 | + } |
|
117 | + } |
|
118 | + $hash .= $this->table[$chr]; |
|
119 | + $i++; |
|
120 | + $error = min($latE,$lonE); |
|
121 | + } |
|
122 | + return $hash; |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -128,39 +128,39 @@ discard block |
||
128 | 128 | * @see https://github.com/asonge/php-geohash/issues/1 |
129 | 129 | */ |
130 | 130 | private function decode($hash){ |
131 | - $ll = array(); |
|
132 | - $minlat = -90; |
|
133 | - $maxlat = 90; |
|
134 | - $minlon = -180; |
|
135 | - $maxlon = 180; |
|
136 | - $latE = 90; |
|
137 | - $lonE = 180; |
|
138 | - for($i=0,$c=strlen($hash);$i<$c;$i++) { |
|
139 | - $v = strpos($this->table,$hash[$i]); |
|
140 | - if(1&$i) { |
|
141 | - if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142 | - if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143 | - if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144 | - if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145 | - if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
146 | - $latE /= 8; |
|
147 | - $lonE /= 4; |
|
148 | - } else { |
|
149 | - if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150 | - if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151 | - if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152 | - if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153 | - if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
154 | - $latE /= 4; |
|
155 | - $lonE /= 8; |
|
156 | - } |
|
157 | - } |
|
158 | - $ll['minlat'] = $minlat; |
|
159 | - $ll['minlon'] = $minlon; |
|
160 | - $ll['maxlat'] = $maxlat; |
|
161 | - $ll['maxlon'] = $maxlon; |
|
162 | - $ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1); |
|
163 | - $ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1); |
|
164 | - return $ll; |
|
131 | + $ll = array(); |
|
132 | + $minlat = -90; |
|
133 | + $maxlat = 90; |
|
134 | + $minlon = -180; |
|
135 | + $maxlon = 180; |
|
136 | + $latE = 90; |
|
137 | + $lonE = 180; |
|
138 | + for($i=0,$c=strlen($hash);$i<$c;$i++) { |
|
139 | + $v = strpos($this->table,$hash[$i]); |
|
140 | + if(1&$i) { |
|
141 | + if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142 | + if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143 | + if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144 | + if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145 | + if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
146 | + $latE /= 8; |
|
147 | + $lonE /= 4; |
|
148 | + } else { |
|
149 | + if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150 | + if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151 | + if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152 | + if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153 | + if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
154 | + $latE /= 4; |
|
155 | + $lonE /= 8; |
|
156 | + } |
|
157 | + } |
|
158 | + $ll['minlat'] = $minlat; |
|
159 | + $ll['minlon'] = $minlon; |
|
160 | + $ll['maxlat'] = $maxlat; |
|
161 | + $ll['maxlon'] = $maxlon; |
|
162 | + $ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1); |
|
163 | + $ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1); |
|
164 | + return $ll; |
|
165 | 165 | } |
166 | 166 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * @return Geometry|GeometryCollection |
24 | 24 | */ |
25 | 25 | public function read($gpx) { |
26 | - return $this->geomFromText($gpx); |
|
26 | + return $this->geomFromText($gpx); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
@@ -34,147 +34,147 @@ discard block |
||
34 | 34 | * @return string The GPX string representation of the input geometries |
35 | 35 | */ |
36 | 36 | public function write(Geometry $geometry, $namespace = FALSE) { |
37 | - if ($geometry->isEmpty()) return NULL; |
|
38 | - if ($namespace) { |
|
39 | - $this->namespace = $namespace; |
|
40 | - $this->nss = $namespace.':'; |
|
41 | - } |
|
42 | - return '<'.$this->nss.'gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'</'.$this->nss.'gpx>'; |
|
37 | + if ($geometry->isEmpty()) return NULL; |
|
38 | + if ($namespace) { |
|
39 | + $this->namespace = $namespace; |
|
40 | + $this->nss = $namespace.':'; |
|
41 | + } |
|
42 | + return '<'.$this->nss.'gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'</'.$this->nss.'gpx>'; |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | public function geomFromText($text) { |
46 | - // Change to lower-case and strip all CDATA |
|
47 | - $text = strtolower($text); |
|
48 | - $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text); |
|
46 | + // Change to lower-case and strip all CDATA |
|
47 | + $text = strtolower($text); |
|
48 | + $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text); |
|
49 | 49 | |
50 | - // Load into DOMDocument |
|
51 | - $xmlobj = new DOMDocument(); |
|
52 | - @$xmlobj->loadXML($text); |
|
53 | - if ($xmlobj === false) { |
|
54 | - throw new Exception("Invalid GPX: ". $text); |
|
55 | - } |
|
50 | + // Load into DOMDocument |
|
51 | + $xmlobj = new DOMDocument(); |
|
52 | + @$xmlobj->loadXML($text); |
|
53 | + if ($xmlobj === false) { |
|
54 | + throw new Exception("Invalid GPX: ". $text); |
|
55 | + } |
|
56 | 56 | |
57 | - $this->xmlobj = $xmlobj; |
|
58 | - try { |
|
59 | - $geom = $this->geomFromXML(); |
|
60 | - } catch(InvalidText $e) { |
|
61 | - throw new Exception("Cannot Read Geometry From GPX: ". $text); |
|
62 | - } catch(Exception $e) { |
|
63 | - throw $e; |
|
64 | - } |
|
57 | + $this->xmlobj = $xmlobj; |
|
58 | + try { |
|
59 | + $geom = $this->geomFromXML(); |
|
60 | + } catch(InvalidText $e) { |
|
61 | + throw new Exception("Cannot Read Geometry From GPX: ". $text); |
|
62 | + } catch(Exception $e) { |
|
63 | + throw $e; |
|
64 | + } |
|
65 | 65 | |
66 | - return $geom; |
|
66 | + return $geom; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | protected function geomFromXML() { |
70 | - $geometries = array(); |
|
71 | - $geometries = array_merge($geometries, $this->parseWaypoints()); |
|
72 | - $geometries = array_merge($geometries, $this->parseTracks()); |
|
73 | - $geometries = array_merge($geometries, $this->parseRoutes()); |
|
70 | + $geometries = array(); |
|
71 | + $geometries = array_merge($geometries, $this->parseWaypoints()); |
|
72 | + $geometries = array_merge($geometries, $this->parseTracks()); |
|
73 | + $geometries = array_merge($geometries, $this->parseRoutes()); |
|
74 | 74 | |
75 | - if (empty($geometries)) { |
|
76 | - throw new Exception("Invalid / Empty GPX"); |
|
77 | - } |
|
75 | + if (empty($geometries)) { |
|
76 | + throw new Exception("Invalid / Empty GPX"); |
|
77 | + } |
|
78 | 78 | |
79 | - return geoPHP::geometryReduce($geometries); |
|
79 | + return geoPHP::geometryReduce($geometries); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | protected function childElements($xml, $nodename = '') { |
83 | - $children = array(); |
|
84 | - foreach ($xml->childNodes as $child) { |
|
85 | - if ($child->nodeName == $nodename) { |
|
86 | - $children[] = $child; |
|
87 | - } |
|
88 | - } |
|
89 | - return $children; |
|
83 | + $children = array(); |
|
84 | + foreach ($xml->childNodes as $child) { |
|
85 | + if ($child->nodeName == $nodename) { |
|
86 | + $children[] = $child; |
|
87 | + } |
|
88 | + } |
|
89 | + return $children; |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | protected function parseWaypoints() { |
93 | - $points = array(); |
|
94 | - $wpt_elements = $this->xmlobj->getElementsByTagName('wpt'); |
|
95 | - foreach ($wpt_elements as $wpt) { |
|
96 | - $lat = $wpt->attributes->getNamedItem("lat")->nodeValue; |
|
97 | - $lon = $wpt->attributes->getNamedItem("lon")->nodeValue; |
|
98 | - $points[] = new Point($lon, $lat); |
|
99 | - } |
|
100 | - return $points; |
|
93 | + $points = array(); |
|
94 | + $wpt_elements = $this->xmlobj->getElementsByTagName('wpt'); |
|
95 | + foreach ($wpt_elements as $wpt) { |
|
96 | + $lat = $wpt->attributes->getNamedItem("lat")->nodeValue; |
|
97 | + $lon = $wpt->attributes->getNamedItem("lon")->nodeValue; |
|
98 | + $points[] = new Point($lon, $lat); |
|
99 | + } |
|
100 | + return $points; |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | protected function parseTracks() { |
104 | - $lines = array(); |
|
105 | - $trk_elements = $this->xmlobj->getElementsByTagName('trk'); |
|
106 | - foreach ($trk_elements as $trk) { |
|
107 | - $components = array(); |
|
108 | - foreach ($this->childElements($trk, 'trkseg') as $trkseg) { |
|
109 | - foreach ($this->childElements($trkseg, 'trkpt') as $trkpt) { |
|
110 | - $lat = $trkpt->attributes->getNamedItem("lat")->nodeValue; |
|
111 | - $lon = $trkpt->attributes->getNamedItem("lon")->nodeValue; |
|
112 | - $components[] = new Point($lon, $lat); |
|
113 | - } |
|
114 | - } |
|
115 | - if ($components) {$lines[] = new LineString($components);} |
|
116 | - } |
|
117 | - return $lines; |
|
104 | + $lines = array(); |
|
105 | + $trk_elements = $this->xmlobj->getElementsByTagName('trk'); |
|
106 | + foreach ($trk_elements as $trk) { |
|
107 | + $components = array(); |
|
108 | + foreach ($this->childElements($trk, 'trkseg') as $trkseg) { |
|
109 | + foreach ($this->childElements($trkseg, 'trkpt') as $trkpt) { |
|
110 | + $lat = $trkpt->attributes->getNamedItem("lat")->nodeValue; |
|
111 | + $lon = $trkpt->attributes->getNamedItem("lon")->nodeValue; |
|
112 | + $components[] = new Point($lon, $lat); |
|
113 | + } |
|
114 | + } |
|
115 | + if ($components) {$lines[] = new LineString($components);} |
|
116 | + } |
|
117 | + return $lines; |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | protected function parseRoutes() { |
121 | - $lines = array(); |
|
122 | - $rte_elements = $this->xmlobj->getElementsByTagName('rte'); |
|
123 | - foreach ($rte_elements as $rte) { |
|
124 | - $components = array(); |
|
125 | - foreach ($this->childElements($rte, 'rtept') as $rtept) { |
|
126 | - $lat = $rtept->attributes->getNamedItem("lat")->nodeValue; |
|
127 | - $lon = $rtept->attributes->getNamedItem("lon")->nodeValue; |
|
128 | - $components[] = new Point($lon, $lat); |
|
129 | - } |
|
130 | - $lines[] = new LineString($components); |
|
131 | - } |
|
132 | - return $lines; |
|
121 | + $lines = array(); |
|
122 | + $rte_elements = $this->xmlobj->getElementsByTagName('rte'); |
|
123 | + foreach ($rte_elements as $rte) { |
|
124 | + $components = array(); |
|
125 | + foreach ($this->childElements($rte, 'rtept') as $rtept) { |
|
126 | + $lat = $rtept->attributes->getNamedItem("lat")->nodeValue; |
|
127 | + $lon = $rtept->attributes->getNamedItem("lon")->nodeValue; |
|
128 | + $components[] = new Point($lon, $lat); |
|
129 | + } |
|
130 | + $lines[] = new LineString($components); |
|
131 | + } |
|
132 | + return $lines; |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | protected function geometryToGPX($geom) { |
136 | - $type = strtolower($geom->getGeomType()); |
|
137 | - switch ($type) { |
|
138 | - case 'point': |
|
139 | - return $this->pointToGPX($geom); |
|
140 | - break; |
|
141 | - case 'linestring': |
|
142 | - return $this->linestringToGPX($geom); |
|
143 | - break; |
|
144 | - case 'polygon': |
|
145 | - case 'multipoint': |
|
146 | - case 'multilinestring': |
|
147 | - case 'multipolygon': |
|
148 | - case 'geometrycollection': |
|
149 | - return $this->collectionToGPX($geom); |
|
150 | - break; |
|
151 | - } |
|
136 | + $type = strtolower($geom->getGeomType()); |
|
137 | + switch ($type) { |
|
138 | + case 'point': |
|
139 | + return $this->pointToGPX($geom); |
|
140 | + break; |
|
141 | + case 'linestring': |
|
142 | + return $this->linestringToGPX($geom); |
|
143 | + break; |
|
144 | + case 'polygon': |
|
145 | + case 'multipoint': |
|
146 | + case 'multilinestring': |
|
147 | + case 'multipolygon': |
|
148 | + case 'geometrycollection': |
|
149 | + return $this->collectionToGPX($geom); |
|
150 | + break; |
|
151 | + } |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | private function pointToGPX($geom) { |
155 | - return '<'.$this->nss.'wpt lat="'.$geom->getY().'" lon="'.$geom->getX().'" />'; |
|
155 | + return '<'.$this->nss.'wpt lat="'.$geom->getY().'" lon="'.$geom->getX().'" />'; |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | private function linestringToGPX($geom) { |
159 | - $gpx = '<'.$this->nss.'trk><'.$this->nss.'trkseg>'; |
|
159 | + $gpx = '<'.$this->nss.'trk><'.$this->nss.'trkseg>'; |
|
160 | 160 | |
161 | - foreach ($geom->getComponents() as $comp) { |
|
162 | - $gpx .= '<'.$this->nss.'trkpt lat="'.$comp->getY().'" lon="'.$comp->getX().'" />'; |
|
163 | - } |
|
161 | + foreach ($geom->getComponents() as $comp) { |
|
162 | + $gpx .= '<'.$this->nss.'trkpt lat="'.$comp->getY().'" lon="'.$comp->getX().'" />'; |
|
163 | + } |
|
164 | 164 | |
165 | - $gpx .= '</'.$this->nss.'trkseg></'.$this->nss.'trk>'; |
|
165 | + $gpx .= '</'.$this->nss.'trkseg></'.$this->nss.'trk>'; |
|
166 | 166 | |
167 | - return $gpx; |
|
167 | + return $gpx; |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | public function collectionToGPX($geom) { |
171 | - $gpx = ''; |
|
172 | - $components = $geom->getComponents(); |
|
173 | - foreach ($geom->getComponents() as $comp) { |
|
174 | - $gpx .= $this->geometryToGPX($comp); |
|
175 | - } |
|
171 | + $gpx = ''; |
|
172 | + $components = $geom->getComponents(); |
|
173 | + foreach ($geom->getComponents() as $comp) { |
|
174 | + $gpx .= $this->geometryToGPX($comp); |
|
175 | + } |
|
176 | 176 | |
177 | - return $gpx; |
|
177 | + return $gpx; |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | } |
@@ -16,97 +16,97 @@ discard block |
||
16 | 16 | * @return object Geometry |
17 | 17 | */ |
18 | 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); |
|
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 | 45 | } |
46 | 46 | |
47 | 47 | private function objToGeom($obj) { |
48 | - $type = $obj->type; |
|
48 | + $type = $obj->type; |
|
49 | 49 | |
50 | - if ($type == 'GeometryCollection') { |
|
51 | - return $this->objToGeometryCollection($obj); |
|
52 | - } |
|
53 | - $method = 'arrayTo' . $type; |
|
54 | - return $this->$method($obj->coordinates); |
|
50 | + if ($type == 'GeometryCollection') { |
|
51 | + return $this->objToGeometryCollection($obj); |
|
52 | + } |
|
53 | + $method = 'arrayTo' . $type; |
|
54 | + return $this->$method($obj->coordinates); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | private function arrayToPoint($array) { |
58 | - return new Point($array[0], $array[1]); |
|
58 | + return new Point($array[0], $array[1]); |
|
59 | 59 | } |
60 | 60 | |
61 | 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); |
|
62 | + $points = array(); |
|
63 | + foreach ($array as $comp_array) { |
|
64 | + $points[] = $this->arrayToPoint($comp_array); |
|
65 | + } |
|
66 | + return new LineString($points); |
|
67 | 67 | } |
68 | 68 | |
69 | 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); |
|
70 | + $lines = array(); |
|
71 | + foreach ($array as $comp_array) { |
|
72 | + $lines[] = $this->arrayToLineString($comp_array); |
|
73 | + } |
|
74 | + return new Polygon($lines); |
|
75 | 75 | } |
76 | 76 | |
77 | 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); |
|
78 | + $points = array(); |
|
79 | + foreach ($array as $comp_array) { |
|
80 | + $points[] = $this->arrayToPoint($comp_array); |
|
81 | + } |
|
82 | + return new MultiPoint($points); |
|
83 | 83 | } |
84 | 84 | |
85 | 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); |
|
86 | + $lines = array(); |
|
87 | + foreach ($array as $comp_array) { |
|
88 | + $lines[] = $this->arrayToLineString($comp_array); |
|
89 | + } |
|
90 | + return new MultiLineString($lines); |
|
91 | 91 | } |
92 | 92 | |
93 | 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); |
|
94 | + $polys = array(); |
|
95 | + foreach ($array as $comp_array) { |
|
96 | + $polys[] = $this->arrayToPolygon($comp_array); |
|
97 | + } |
|
98 | + return new MultiPolygon($polys); |
|
99 | 99 | } |
100 | 100 | |
101 | 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); |
|
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 | 110 | } |
111 | 111 | |
112 | 112 | /** |
@@ -118,32 +118,32 @@ discard block |
||
118 | 118 | * @return string The GeoJSON string |
119 | 119 | */ |
120 | 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 | - } |
|
121 | + if ($return_array) { |
|
122 | + return $this->getArray($geometry); |
|
123 | + } |
|
124 | + else { |
|
125 | + return json_encode($this->getArray($geometry)); |
|
126 | + } |
|
127 | 127 | } |
128 | 128 | |
129 | 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 | - ); |
|
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 | 147 | } |
148 | 148 | } |
149 | 149 |
@@ -29,120 +29,120 @@ discard block |
||
29 | 29 | * @return Geometry |
30 | 30 | */ |
31 | 31 | public function read($wkb, $is_hex_string = FALSE) { |
32 | - if ($is_hex_string) { |
|
33 | - $wkb = pack('H*',$wkb); |
|
34 | - } |
|
32 | + if ($is_hex_string) { |
|
33 | + $wkb = pack('H*',$wkb); |
|
34 | + } |
|
35 | 35 | |
36 | - if (empty($wkb)) { |
|
37 | - throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb)); |
|
38 | - } |
|
36 | + if (empty($wkb)) { |
|
37 | + throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb)); |
|
38 | + } |
|
39 | 39 | |
40 | - $mem = fopen('php://memory', 'r+'); |
|
41 | - fwrite($mem, $wkb); |
|
42 | - fseek($mem, 0); |
|
40 | + $mem = fopen('php://memory', 'r+'); |
|
41 | + fwrite($mem, $wkb); |
|
42 | + fseek($mem, 0); |
|
43 | 43 | |
44 | - $geometry = $this->getGeometry($mem); |
|
45 | - fclose($mem); |
|
46 | - return $geometry; |
|
44 | + $geometry = $this->getGeometry($mem); |
|
45 | + fclose($mem); |
|
46 | + return $geometry; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | function getGeometry(&$mem) { |
50 | - $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
51 | - if ($base_info['order'] !== 1) { |
|
52 | - throw new Exception('Only NDR (little endian) SKB format is supported at the moment'); |
|
53 | - } |
|
54 | - |
|
55 | - if ($base_info['z']) { |
|
56 | - $this->dimension++; |
|
57 | - $this->z = TRUE; |
|
58 | - } |
|
59 | - if ($base_info['m']) { |
|
60 | - $this->dimension++; |
|
61 | - $this->m = TRUE; |
|
62 | - } |
|
63 | - |
|
64 | - // If there is SRID information, ignore it - use EWKB Adapter to get SRID support |
|
65 | - if ($base_info['s']) { |
|
66 | - fread($mem, 4); |
|
67 | - } |
|
68 | - |
|
69 | - switch ($base_info['type']) { |
|
70 | - case 1: |
|
71 | - return $this->getPoint($mem); |
|
72 | - case 2: |
|
73 | - return $this->getLinstring($mem); |
|
74 | - case 3: |
|
75 | - return $this->getPolygon($mem); |
|
76 | - case 4: |
|
77 | - return $this->getMulti($mem,'point'); |
|
78 | - case 5: |
|
79 | - return $this->getMulti($mem,'line'); |
|
80 | - case 6: |
|
81 | - return $this->getMulti($mem,'polygon'); |
|
82 | - case 7: |
|
83 | - return $this->getMulti($mem,'geometry'); |
|
84 | - } |
|
50 | + $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
51 | + if ($base_info['order'] !== 1) { |
|
52 | + throw new Exception('Only NDR (little endian) SKB format is supported at the moment'); |
|
53 | + } |
|
54 | + |
|
55 | + if ($base_info['z']) { |
|
56 | + $this->dimension++; |
|
57 | + $this->z = TRUE; |
|
58 | + } |
|
59 | + if ($base_info['m']) { |
|
60 | + $this->dimension++; |
|
61 | + $this->m = TRUE; |
|
62 | + } |
|
63 | + |
|
64 | + // If there is SRID information, ignore it - use EWKB Adapter to get SRID support |
|
65 | + if ($base_info['s']) { |
|
66 | + fread($mem, 4); |
|
67 | + } |
|
68 | + |
|
69 | + switch ($base_info['type']) { |
|
70 | + case 1: |
|
71 | + return $this->getPoint($mem); |
|
72 | + case 2: |
|
73 | + return $this->getLinstring($mem); |
|
74 | + case 3: |
|
75 | + return $this->getPolygon($mem); |
|
76 | + case 4: |
|
77 | + return $this->getMulti($mem,'point'); |
|
78 | + case 5: |
|
79 | + return $this->getMulti($mem,'line'); |
|
80 | + case 6: |
|
81 | + return $this->getMulti($mem,'polygon'); |
|
82 | + case 7: |
|
83 | + return $this->getMulti($mem,'geometry'); |
|
84 | + } |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | function getPoint(&$mem) { |
88 | - $point_coords = unpack("d*", fread($mem,$this->dimension*8)); |
|
89 | - return new Point($point_coords[1],$point_coords[2]); |
|
88 | + $point_coords = unpack("d*", fread($mem,$this->dimension*8)); |
|
89 | + return new Point($point_coords[1],$point_coords[2]); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | function getLinstring(&$mem) { |
93 | - // Get the number of points expected in this string out of the first 4 bytes |
|
94 | - $line_length = unpack('L',fread($mem,4)); |
|
95 | - |
|
96 | - // Return an empty linestring if there is no line-length |
|
97 | - if (!$line_length[1]) return new LineString(); |
|
98 | - |
|
99 | - // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
|
100 | - $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8)); |
|
101 | - |
|
102 | - // We have our coords, build up the linestring |
|
103 | - $components = array(); |
|
104 | - $i = 1; |
|
105 | - $num_coords = count($line_coords); |
|
106 | - while ($i <= $num_coords) { |
|
107 | - $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
108 | - $i += 2; |
|
109 | - } |
|
110 | - return new LineString($components); |
|
93 | + // Get the number of points expected in this string out of the first 4 bytes |
|
94 | + $line_length = unpack('L',fread($mem,4)); |
|
95 | + |
|
96 | + // Return an empty linestring if there is no line-length |
|
97 | + if (!$line_length[1]) return new LineString(); |
|
98 | + |
|
99 | + // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
|
100 | + $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8)); |
|
101 | + |
|
102 | + // We have our coords, build up the linestring |
|
103 | + $components = array(); |
|
104 | + $i = 1; |
|
105 | + $num_coords = count($line_coords); |
|
106 | + while ($i <= $num_coords) { |
|
107 | + $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
108 | + $i += 2; |
|
109 | + } |
|
110 | + return new LineString($components); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | function getPolygon(&$mem) { |
114 | - // Get the number of linestring expected in this poly out of the first 4 bytes |
|
115 | - $poly_length = unpack('L',fread($mem,4)); |
|
116 | - |
|
117 | - $components = array(); |
|
118 | - $i = 1; |
|
119 | - while ($i <= $poly_length[1]) { |
|
120 | - $components[] = $this->getLinstring($mem); |
|
121 | - $i++; |
|
122 | - } |
|
123 | - return new Polygon($components); |
|
114 | + // Get the number of linestring expected in this poly out of the first 4 bytes |
|
115 | + $poly_length = unpack('L',fread($mem,4)); |
|
116 | + |
|
117 | + $components = array(); |
|
118 | + $i = 1; |
|
119 | + while ($i <= $poly_length[1]) { |
|
120 | + $components[] = $this->getLinstring($mem); |
|
121 | + $i++; |
|
122 | + } |
|
123 | + return new Polygon($components); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | function getMulti(&$mem, $type) { |
127 | - // Get the number of items expected in this multi out of the first 4 bytes |
|
128 | - $multi_length = unpack('L',fread($mem,4)); |
|
129 | - |
|
130 | - $components = array(); |
|
131 | - $i = 1; |
|
132 | - while ($i <= $multi_length[1]) { |
|
133 | - $components[] = $this->getGeometry($mem); |
|
134 | - $i++; |
|
135 | - } |
|
136 | - switch ($type) { |
|
137 | - case 'point': |
|
138 | - return new MultiPoint($components); |
|
139 | - case 'line': |
|
140 | - return new MultiLineString($components); |
|
141 | - case 'polygon': |
|
142 | - return new MultiPolygon($components); |
|
143 | - case 'geometry': |
|
144 | - return new GeometryCollection($components); |
|
145 | - } |
|
127 | + // Get the number of items expected in this multi out of the first 4 bytes |
|
128 | + $multi_length = unpack('L',fread($mem,4)); |
|
129 | + |
|
130 | + $components = array(); |
|
131 | + $i = 1; |
|
132 | + while ($i <= $multi_length[1]) { |
|
133 | + $components[] = $this->getGeometry($mem); |
|
134 | + $i++; |
|
135 | + } |
|
136 | + switch ($type) { |
|
137 | + case 'point': |
|
138 | + return new MultiPoint($components); |
|
139 | + case 'line': |
|
140 | + return new MultiLineString($components); |
|
141 | + case 'polygon': |
|
142 | + return new MultiPolygon($components); |
|
143 | + case 'geometry': |
|
144 | + return new GeometryCollection($components); |
|
145 | + } |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -153,90 +153,90 @@ discard block |
||
153 | 153 | * @return string The WKB string representation of the input geometries |
154 | 154 | */ |
155 | 155 | public function write(Geometry $geometry, $write_as_hex = FALSE) { |
156 | - // We always write into NDR (little endian) |
|
157 | - $wkb = pack('c',1); |
|
158 | - |
|
159 | - switch ($geometry->getGeomType()) { |
|
160 | - case 'Point'; |
|
161 | - $wkb .= pack('L',1); |
|
162 | - $wkb .= $this->writePoint($geometry); |
|
163 | - break; |
|
164 | - case 'LineString'; |
|
165 | - $wkb .= pack('L',2); |
|
166 | - $wkb .= $this->writeLineString($geometry); |
|
167 | - break; |
|
168 | - case 'Polygon'; |
|
169 | - $wkb .= pack('L',3); |
|
170 | - $wkb .= $this->writePolygon($geometry); |
|
171 | - break; |
|
172 | - case 'MultiPoint'; |
|
173 | - $wkb .= pack('L',4); |
|
174 | - $wkb .= $this->writeMulti($geometry); |
|
175 | - break; |
|
176 | - case 'MultiLineString'; |
|
177 | - $wkb .= pack('L',5); |
|
178 | - $wkb .= $this->writeMulti($geometry); |
|
179 | - break; |
|
180 | - case 'MultiPolygon'; |
|
181 | - $wkb .= pack('L',6); |
|
182 | - $wkb .= $this->writeMulti($geometry); |
|
183 | - break; |
|
184 | - case 'GeometryCollection'; |
|
185 | - $wkb .= pack('L',7); |
|
186 | - $wkb .= $this->writeMulti($geometry); |
|
187 | - break; |
|
188 | - } |
|
189 | - |
|
190 | - if ($write_as_hex) { |
|
191 | - $unpacked = unpack('H*',$wkb); |
|
192 | - return $unpacked[1]; |
|
193 | - } |
|
194 | - else { |
|
195 | - return $wkb; |
|
196 | - } |
|
156 | + // We always write into NDR (little endian) |
|
157 | + $wkb = pack('c',1); |
|
158 | + |
|
159 | + switch ($geometry->getGeomType()) { |
|
160 | + case 'Point'; |
|
161 | + $wkb .= pack('L',1); |
|
162 | + $wkb .= $this->writePoint($geometry); |
|
163 | + break; |
|
164 | + case 'LineString'; |
|
165 | + $wkb .= pack('L',2); |
|
166 | + $wkb .= $this->writeLineString($geometry); |
|
167 | + break; |
|
168 | + case 'Polygon'; |
|
169 | + $wkb .= pack('L',3); |
|
170 | + $wkb .= $this->writePolygon($geometry); |
|
171 | + break; |
|
172 | + case 'MultiPoint'; |
|
173 | + $wkb .= pack('L',4); |
|
174 | + $wkb .= $this->writeMulti($geometry); |
|
175 | + break; |
|
176 | + case 'MultiLineString'; |
|
177 | + $wkb .= pack('L',5); |
|
178 | + $wkb .= $this->writeMulti($geometry); |
|
179 | + break; |
|
180 | + case 'MultiPolygon'; |
|
181 | + $wkb .= pack('L',6); |
|
182 | + $wkb .= $this->writeMulti($geometry); |
|
183 | + break; |
|
184 | + case 'GeometryCollection'; |
|
185 | + $wkb .= pack('L',7); |
|
186 | + $wkb .= $this->writeMulti($geometry); |
|
187 | + break; |
|
188 | + } |
|
189 | + |
|
190 | + if ($write_as_hex) { |
|
191 | + $unpacked = unpack('H*',$wkb); |
|
192 | + return $unpacked[1]; |
|
193 | + } |
|
194 | + else { |
|
195 | + return $wkb; |
|
196 | + } |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | function writePoint($point) { |
200 | - // Set the coords |
|
201 | - $wkb = pack('dd',$point->x(), $point->y()); |
|
200 | + // Set the coords |
|
201 | + $wkb = pack('dd',$point->x(), $point->y()); |
|
202 | 202 | |
203 | - return $wkb; |
|
203 | + return $wkb; |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | function writeLineString($line) { |
207 | - // Set the number of points in this line |
|
208 | - $wkb = pack('L',$line->numPoints()); |
|
207 | + // Set the number of points in this line |
|
208 | + $wkb = pack('L',$line->numPoints()); |
|
209 | 209 | |
210 | - // Set the coords |
|
211 | - foreach ($line->getComponents() as $point) { |
|
212 | - $wkb .= pack('dd',$point->x(), $point->y()); |
|
213 | - } |
|
210 | + // Set the coords |
|
211 | + foreach ($line->getComponents() as $point) { |
|
212 | + $wkb .= pack('dd',$point->x(), $point->y()); |
|
213 | + } |
|
214 | 214 | |
215 | - return $wkb; |
|
215 | + return $wkb; |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | function writePolygon($poly) { |
219 | - // Set the number of lines in this poly |
|
220 | - $wkb = pack('L',$poly->numGeometries()); |
|
219 | + // Set the number of lines in this poly |
|
220 | + $wkb = pack('L',$poly->numGeometries()); |
|
221 | 221 | |
222 | - // Write the lines |
|
223 | - foreach ($poly->getComponents() as $line) { |
|
224 | - $wkb .= $this->writeLineString($line); |
|
225 | - } |
|
222 | + // Write the lines |
|
223 | + foreach ($poly->getComponents() as $line) { |
|
224 | + $wkb .= $this->writeLineString($line); |
|
225 | + } |
|
226 | 226 | |
227 | - return $wkb; |
|
227 | + return $wkb; |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | function writeMulti($geometry) { |
231 | - // Set the number of components |
|
232 | - $wkb = pack('L',$geometry->numGeometries()); |
|
231 | + // Set the number of components |
|
232 | + $wkb = pack('L',$geometry->numGeometries()); |
|
233 | 233 | |
234 | - // Write the components |
|
235 | - foreach ($geometry->getComponents() as $component) { |
|
236 | - $wkb .= $this->write($component); |
|
237 | - } |
|
234 | + // Write the components |
|
235 | + foreach ($geometry->getComponents() as $component) { |
|
236 | + $wkb .= $this->write($component); |
|
237 | + } |
|
238 | 238 | |
239 | - return $wkb; |
|
239 | + return $wkb; |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | } |