@@ -11,24 +11,24 @@ discard block |
||
11 | 11 | include_once('../geoPHP.inc'); |
12 | 12 | |
13 | 13 | if (geoPHP::geosInstalled()) { |
14 | - print "GEOS is installed.\n"; |
|
14 | + print "GEOS is installed.\n"; |
|
15 | 15 | } |
16 | 16 | else { |
17 | - print "GEOS is not installed.\n"; |
|
17 | + print "GEOS is not installed.\n"; |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | foreach (scandir('./input') as $file) { |
21 | - $parts = explode('.',$file); |
|
22 | - if ($parts[0]) { |
|
23 | - $format = $parts[1]; |
|
24 | - $value = file_get_contents('./input/'.$file); |
|
25 | - print '---- Testing '.$file."\n"; |
|
26 | - $geometry = geoPHP::load($value, $format); |
|
27 | - test_adapters($geometry, $format, $value); |
|
28 | - test_methods($geometry); |
|
29 | - test_geometry($geometry); |
|
30 | - test_detection($value, $format, $file); |
|
31 | - } |
|
21 | + $parts = explode('.',$file); |
|
22 | + if ($parts[0]) { |
|
23 | + $format = $parts[1]; |
|
24 | + $value = file_get_contents('./input/'.$file); |
|
25 | + print '---- Testing '.$file."\n"; |
|
26 | + $geometry = geoPHP::load($value, $format); |
|
27 | + test_adapters($geometry, $format, $value); |
|
28 | + test_methods($geometry); |
|
29 | + test_geometry($geometry); |
|
30 | + test_detection($value, $format, $file); |
|
31 | + } |
|
32 | 32 | } |
33 | 33 | print "Testing Done!"; |
34 | 34 | } |
@@ -115,21 +115,21 @@ discard block |
||
115 | 115 | function test_adapters($geometry, $format, $input) { |
116 | 116 | // Test adapter output and input. Do a round-trip and re-test |
117 | 117 | foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { |
118 | - if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
119 | - $output = $geometry->out($adapter_key); |
|
120 | - if ($output) { |
|
121 | - $adapter_loader = new $adapter_class(); |
|
122 | - $test_geom_1 = $adapter_loader->read($output); |
|
123 | - $test_geom_2 = $adapter_loader->read($test_geom_1->out($adapter_key)); |
|
124 | - |
|
125 | - if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
126 | - print "Mismatched adapter output in ".$adapter_class."\n"; |
|
127 | - } |
|
128 | - } |
|
129 | - else { |
|
130 | - print "Empty output on " . $adapter_key . "\n"; |
|
131 | - } |
|
132 | - } |
|
118 | + if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
119 | + $output = $geometry->out($adapter_key); |
|
120 | + if ($output) { |
|
121 | + $adapter_loader = new $adapter_class(); |
|
122 | + $test_geom_1 = $adapter_loader->read($output); |
|
123 | + $test_geom_2 = $adapter_loader->read($test_geom_1->out($adapter_key)); |
|
124 | + |
|
125 | + if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
126 | + print "Mismatched adapter output in ".$adapter_class."\n"; |
|
127 | + } |
|
128 | + } |
|
129 | + else { |
|
130 | + print "Empty output on " . $adapter_key . "\n"; |
|
131 | + } |
|
132 | + } |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | // Test to make sure adapter work the same wether GEOS is ON or OFF |
@@ -137,30 +137,30 @@ discard block |
||
137 | 137 | if (!geoPHP::geosInstalled()) return; |
138 | 138 | |
139 | 139 | foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { |
140 | - if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
141 | - // Turn GEOS on |
|
142 | - geoPHP::geosInstalled(TRUE); |
|
140 | + if ($adapter_key != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test |
|
141 | + // Turn GEOS on |
|
142 | + geoPHP::geosInstalled(TRUE); |
|
143 | 143 | |
144 | - $output = $geometry->out($adapter_key); |
|
145 | - if ($output) { |
|
146 | - $adapter_loader = new $adapter_class(); |
|
144 | + $output = $geometry->out($adapter_key); |
|
145 | + if ($output) { |
|
146 | + $adapter_loader = new $adapter_class(); |
|
147 | 147 | |
148 | - $test_geom_1 = $adapter_loader->read($output); |
|
148 | + $test_geom_1 = $adapter_loader->read($output); |
|
149 | 149 | |
150 | - // Turn GEOS off |
|
151 | - geoPHP::geosInstalled(FALSE); |
|
150 | + // Turn GEOS off |
|
151 | + geoPHP::geosInstalled(FALSE); |
|
152 | 152 | |
153 | - $test_geom_2 = $adapter_loader->read($output); |
|
153 | + $test_geom_2 = $adapter_loader->read($output); |
|
154 | 154 | |
155 | - // Turn GEOS back On |
|
156 | - geoPHP::geosInstalled(TRUE); |
|
155 | + // Turn GEOS back On |
|
156 | + geoPHP::geosInstalled(TRUE); |
|
157 | 157 | |
158 | - // Check to make sure a both are the same with geos and without |
|
159 | - if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
160 | - print "Mismatched adapter output between GEOS and NORM in ".$adapter_class."\n"; |
|
161 | - } |
|
162 | - } |
|
163 | - } |
|
158 | + // Check to make sure a both are the same with geos and without |
|
159 | + if ($test_geom_1->out('wkt') != $test_geom_2->out('wkt')) { |
|
160 | + print "Mismatched adapter output between GEOS and NORM in ".$adapter_class."\n"; |
|
161 | + } |
|
162 | + } |
|
163 | + } |
|
164 | 164 | } |
165 | 165 | } |
166 | 166 | |
@@ -170,75 +170,75 @@ discard block |
||
170 | 170 | if (!geoPHP::geosInstalled()) return; |
171 | 171 | |
172 | 172 | $methods = array( |
173 | - //'boundary', //@@TODO: Uncomment this and fix errors |
|
174 | - 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
175 | - 'getBBox', |
|
176 | - 'x', |
|
177 | - 'y', |
|
178 | - 'startPoint', |
|
179 | - 'endPoint', |
|
180 | - 'isRing', |
|
181 | - 'isClosed', |
|
182 | - 'numPoints', |
|
173 | + //'boundary', //@@TODO: Uncomment this and fix errors |
|
174 | + 'envelope', //@@TODO: Testing reveales errors in this method -- POINT vs. POLYGON |
|
175 | + 'getBBox', |
|
176 | + 'x', |
|
177 | + 'y', |
|
178 | + 'startPoint', |
|
179 | + 'endPoint', |
|
180 | + 'isRing', |
|
181 | + 'isClosed', |
|
182 | + 'numPoints', |
|
183 | 183 | ); |
184 | 184 | |
185 | 185 | foreach ($methods as $method) { |
186 | - // Turn GEOS on |
|
187 | - geoPHP::geosInstalled(TRUE); |
|
188 | - $geos_result = $geometry->$method(); |
|
189 | - |
|
190 | - // Turn GEOS off |
|
191 | - geoPHP::geosInstalled(FALSE); |
|
192 | - $norm_result = $geometry->$method(); |
|
193 | - |
|
194 | - // Turn GEOS back On |
|
195 | - geoPHP::geosInstalled(TRUE); |
|
196 | - |
|
197 | - $geos_type = gettype($geos_result); |
|
198 | - $norm_type = gettype($norm_result); |
|
199 | - |
|
200 | - if ($geos_type != $norm_type) { |
|
201 | - print 'Type mismatch on '.$method."\n"; |
|
202 | - continue; |
|
203 | - } |
|
204 | - |
|
205 | - // Now check base on type |
|
206 | - if ($geos_type == 'object') { |
|
207 | - $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
208 | - |
|
209 | - // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
|
210 | - // Using Pythagorean theorem |
|
211 | - $bb = $geos_result->getBBox(); |
|
212 | - $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
|
213 | - |
|
214 | - // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
|
215 | - if ($haus_dist / $scale > 0.5) { |
|
216 | - print 'Output mismatch on '.$method.":\n"; |
|
217 | - print 'GEOS : '.$geos_result->out('wkt')."\n"; |
|
218 | - print 'NORM : '.$norm_result->out('wkt')."\n"; |
|
219 | - continue; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - if ($geos_type == 'boolean' || $geos_type == 'string') { |
|
224 | - if ($geos_result !== $norm_result) { |
|
225 | - print 'Output mismatch on '.$method.":\n"; |
|
226 | - print 'GEOS : '.(string) $geos_result."\n"; |
|
227 | - print 'NORM : '.(string) $norm_result."\n"; |
|
228 | - continue; |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - //@@TODO: Run tests for output of types arrays and float |
|
233 | - //@@TODO: centroid function is non-compliant for collections and strings |
|
186 | + // Turn GEOS on |
|
187 | + geoPHP::geosInstalled(TRUE); |
|
188 | + $geos_result = $geometry->$method(); |
|
189 | + |
|
190 | + // Turn GEOS off |
|
191 | + geoPHP::geosInstalled(FALSE); |
|
192 | + $norm_result = $geometry->$method(); |
|
193 | + |
|
194 | + // Turn GEOS back On |
|
195 | + geoPHP::geosInstalled(TRUE); |
|
196 | + |
|
197 | + $geos_type = gettype($geos_result); |
|
198 | + $norm_type = gettype($norm_result); |
|
199 | + |
|
200 | + if ($geos_type != $norm_type) { |
|
201 | + print 'Type mismatch on '.$method."\n"; |
|
202 | + continue; |
|
203 | + } |
|
204 | + |
|
205 | + // Now check base on type |
|
206 | + if ($geos_type == 'object') { |
|
207 | + $haus_dist = $geos_result->hausdorffDistance(geoPHP::load($norm_result->out('wkt'),'wkt')); |
|
208 | + |
|
209 | + // Get the length of the diagonal of the bbox - this is used to scale the haustorff distance |
|
210 | + // Using Pythagorean theorem |
|
211 | + $bb = $geos_result->getBBox(); |
|
212 | + $scale = sqrt((($bb['maxy'] - $bb['miny'])^2) + (($bb['maxx'] - $bb['minx'])^2)); |
|
213 | + |
|
214 | + // The difference in the output of GEOS and native-PHP methods should be less than 0.5 scaled haustorff units |
|
215 | + if ($haus_dist / $scale > 0.5) { |
|
216 | + print 'Output mismatch on '.$method.":\n"; |
|
217 | + print 'GEOS : '.$geos_result->out('wkt')."\n"; |
|
218 | + print 'NORM : '.$norm_result->out('wkt')."\n"; |
|
219 | + continue; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + if ($geos_type == 'boolean' || $geos_type == 'string') { |
|
224 | + if ($geos_result !== $norm_result) { |
|
225 | + print 'Output mismatch on '.$method.":\n"; |
|
226 | + print 'GEOS : '.(string) $geos_result."\n"; |
|
227 | + print 'NORM : '.(string) $norm_result."\n"; |
|
228 | + continue; |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + //@@TODO: Run tests for output of types arrays and float |
|
233 | + //@@TODO: centroid function is non-compliant for collections and strings |
|
234 | 234 | } |
235 | 235 | } |
236 | 236 | |
237 | 237 | function test_detection($value, $format, $file) { |
238 | 238 | $detected = geoPHP::detectFormat($value); |
239 | 239 | if ($detected != $format) { |
240 | - if ($detected) print 'detected as ' . $detected . "\n"; |
|
241 | - else print "not detected\n"; |
|
240 | + if ($detected) print 'detected as ' . $detected . "\n"; |
|
241 | + else print "not detected\n"; |
|
242 | 242 | } |
243 | 243 | // Make sure it loads using auto-detect |
244 | 244 | geoPHP::load($value); |
@@ -25,18 +25,18 @@ discard block |
||
25 | 25 | // ---------------------------- |
26 | 26 | |
27 | 27 | foreach (scandir('./input') as $file) { |
28 | - $parts = explode('.',$file); |
|
29 | - if ($parts[0]) { |
|
30 | - $name = $parts[0]; |
|
31 | - $format = $parts[1]; |
|
32 | - $value = file_get_contents('./input/'.$file); |
|
33 | - print '---- Testing '.$file."\n"; |
|
34 | - flush(); |
|
35 | - $geometry = geoPHP::load($value, $format); |
|
36 | - test_postgis($name, $format, $geometry, $connection, 'wkb'); |
|
37 | - $geometry->setSRID(4326); |
|
38 | - test_postgis($name, $format, $geometry, $connection, 'ewkb'); |
|
39 | - } |
|
28 | + $parts = explode('.',$file); |
|
29 | + if ($parts[0]) { |
|
30 | + $name = $parts[0]; |
|
31 | + $format = $parts[1]; |
|
32 | + $value = file_get_contents('./input/'.$file); |
|
33 | + print '---- Testing '.$file."\n"; |
|
34 | + flush(); |
|
35 | + $geometry = geoPHP::load($value, $format); |
|
36 | + test_postgis($name, $format, $geometry, $connection, 'wkb'); |
|
37 | + $geometry->setSRID(4326); |
|
38 | + test_postgis($name, $format, $geometry, $connection, 'ewkb'); |
|
39 | + } |
|
40 | 40 | } |
41 | 41 | print "Testing Done!"; |
42 | 42 | } |
@@ -51,32 +51,32 @@ discard block |
||
51 | 51 | // SELECT using asBinary PostGIS |
52 | 52 | $result = pg_fetch_all(pg_query($connection, "SELECT asBinary(geom) as geom FROM $table WHERE name='$name'")); |
53 | 53 | foreach ($result as $item) { |
54 | - $wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob |
|
55 | - $geom = geoPHP::load($wkb, $format); // We now a full geoPHP Geometry object |
|
54 | + $wkb = pg_unescape_bytea($item['geom']); // Make sure to unescape the hex blob |
|
55 | + $geom = geoPHP::load($wkb, $format); // We now a full geoPHP Geometry object |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | // SELECT and INSERT directly, with no wrapping functions |
59 | 59 | $result = pg_fetch_all(pg_query($connection, "SELECT geom as geom FROM $table WHERE name='$name'")); |
60 | 60 | foreach ($result as $item) { |
61 | - $wkb = pack('H*',$item['geom']); // Unpacking the hex blob |
|
62 | - $geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry |
|
61 | + $wkb = pack('H*',$item['geom']); // Unpacking the hex blob |
|
62 | + $geom = geoPHP::load($wkb, $format); // We now have a geoPHP Geometry |
|
63 | 63 | |
64 | - // Let's re-insert directly into postGIS |
|
65 | - // We need to unpack the WKB |
|
66 | - $unpacked = unpack('H*', $geom->out($format)); |
|
67 | - $insert_string = $unpacked[1]; |
|
68 | - pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', '$insert_string')"); |
|
64 | + // Let's re-insert directly into postGIS |
|
65 | + // We need to unpack the WKB |
|
66 | + $unpacked = unpack('H*', $geom->out($format)); |
|
67 | + $insert_string = $unpacked[1]; |
|
68 | + pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', '$insert_string')"); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | // SELECT and INSERT using as EWKT (ST_GeomFromEWKT and ST_AsEWKT) |
72 | 72 | $result = pg_fetch_all(pg_query($connection, "SELECT ST_AsEWKT(geom) as geom FROM $table WHERE name='$name'")); |
73 | 73 | foreach ($result as $item) { |
74 | - $wkt = $item['geom']; // Make sure to unescape the hex blob |
|
75 | - $geom = geoPHP::load($wkt, 'ewkt'); // We now a full geoPHP Geometry object |
|
74 | + $wkt = $item['geom']; // Make sure to unescape the hex blob |
|
75 | + $geom = geoPHP::load($wkt, 'ewkt'); // We now a full geoPHP Geometry object |
|
76 | 76 | |
77 | - // Let's re-insert directly into postGIS |
|
78 | - $insert_string = $geom->out('ewkt'); |
|
79 | - pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', ST_GeomFromEWKT('$insert_string'))"); |
|
77 | + // Let's re-insert directly into postGIS |
|
78 | + $insert_string = $geom->out('ewkt'); |
|
79 | + pg_query($connection, "INSERT INTO $table (name, type, geom) values ('$name', '$type', ST_GeomFromEWKT('$insert_string'))"); |
|
80 | 80 | } |
81 | 81 | } |
82 | 82 |
@@ -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 | } |