| @@ -13,6 +13,9 @@ discard block | ||
| 13 | 13 |  function sparql_connect( $endpoint ) { return new sparql_connection( $endpoint ); } | 
| 14 | 14 | |
| 15 | 15 |  function sparql_ns( $short, $long, $db = null ) { return _sparql_a_connection( $db )->ns( $short, $long ); } | 
| 16 | +/** | |
| 17 | + * @param string $sparql | |
| 18 | + */ | |
| 16 | 19 |  function sparql_query( $sparql, $db = null ) { return _sparql_a_connection( $db )->query( $sparql ); } | 
| 17 | 20 |  function sparql_errno( $db = null ) { return _sparql_a_connection( $db )->errno(); } | 
| 18 | 21 |  function sparql_error( $db = null ) { return _sparql_a_connection( $db )->error(); } | 
| @@ -84,6 +87,9 @@ discard block | ||
| 84 | 87 | $this->params = $params; | 
| 85 | 88 | } | 
| 86 | 89 | |
| 90 | + /** | |
| 91 | + * @param integer $timeout | |
| 92 | + */ | |
| 87 | 93 | function query( $query, $timeout=null ) | 
| 88 | 94 |  	{	 | 
| 89 | 95 | $prefixes = ""; | 
| @@ -324,6 +330,10 @@ discard block | ||
| 324 | 330 | var $fields; | 
| 325 | 331 | var $db; | 
| 326 | 332 | var $i = 0; | 
| 333 | + | |
| 334 | + /** | |
| 335 | + * @param sparql_connection $db | |
| 336 | + */ | |
| 327 | 337 | function __construct( $db, $rows, $fields ) | 
| 328 | 338 |  	{ | 
| 329 | 339 | $this->rows = $rows; | 
| @@ -10,37 +10,37 @@ discard block | ||
| 10 | 10 | |
| 11 | 11 | # to document: CGIParams | 
| 12 | 12 | |
| 13 | -function sparql_connect( $endpoint ) { return new sparql_connection( $endpoint ); } | |
| 13 | +function sparql_connect($endpoint) { return new sparql_connection($endpoint); } | |
| 14 | 14 | |
| 15 | -function sparql_ns( $short, $long, $db = null ) { return _sparql_a_connection( $db )->ns( $short, $long ); } | |
| 16 | -function sparql_query( $sparql, $db = null ) { return _sparql_a_connection( $db )->query( $sparql ); } | |
| 17 | -function sparql_errno( $db = null ) { return _sparql_a_connection( $db )->errno(); } | |
| 18 | -function sparql_error( $db = null ) { return _sparql_a_connection( $db )->error(); } | |
| 15 | +function sparql_ns($short, $long, $db = null) { return _sparql_a_connection($db)->ns($short, $long); } | |
| 16 | +function sparql_query($sparql, $db = null) { return _sparql_a_connection($db)->query($sparql); } | |
| 17 | +function sparql_errno($db = null) { return _sparql_a_connection($db)->errno(); } | |
| 18 | +function sparql_error($db = null) { return _sparql_a_connection($db)->error(); } | |
| 19 | 19 | |
| 20 | -function sparql_fetch_array( $result ) { return $result->fetch_array(); } | |
| 21 | -function sparql_num_rows( $result ) { return $result->num_rows(); } | |
| 22 | -function sparql_field_array( $result ) { return $result->field_array(); } | |
| 23 | -function sparql_field_name( $result, $i ) { return $result->field_name( $i ); } | |
| 20 | +function sparql_fetch_array($result) { return $result->fetch_array(); } | |
| 21 | +function sparql_num_rows($result) { return $result->num_rows(); } | |
| 22 | +function sparql_field_array($result) { return $result->field_array(); } | |
| 23 | +function sparql_field_name($result, $i) { return $result->field_name($i); } | |
| 24 | 24 | |
| 25 | -function sparql_fetch_all( $result ) { return $result->fetch_all(); } | |
| 25 | +function sparql_fetch_all($result) { return $result->fetch_all(); } | |
| 26 | 26 | |
| 27 | -function sparql_get( $endpoint, $sparql ) | |
| 27 | +function sparql_get($endpoint, $sparql) | |
| 28 | 28 |  {  | 
| 29 | - $db = sparql_connect( $endpoint ); | |
| 30 | -	if( !$db ) { return; } | |
| 31 | - $result = $db->query( $sparql ); | |
| 32 | -	if( !$result ) { return; } | |
| 29 | + $db = sparql_connect($endpoint); | |
| 30 | +	if (!$db) { return; } | |
| 31 | + $result = $db->query($sparql); | |
| 32 | +	if (!$result) { return; } | |
| 33 | 33 | return $result->fetch_all(); | 
| 34 | 34 | } | 
| 35 | 35 | |
| 36 | -function _sparql_a_connection( $db ) | |
| 36 | +function _sparql_a_connection($db) | |
| 37 | 37 |  { | 
| 38 | 38 | global $sparql_last_connection; | 
| 39 | - if( !isset( $db ) ) | |
| 39 | + if (!isset($db)) | |
| 40 | 40 |  	{ | 
| 41 | - if( !isset( $sparql_last_connection ) ) | |
| 41 | + if (!isset($sparql_last_connection)) | |
| 42 | 42 |  		{ | 
| 43 | - print( "No currect SPARQL connection (connection) in play!" ); | |
| 43 | +			print("No currect SPARQL connection (connection) in play!"); | |
| 44 | 44 | return; | 
| 45 | 45 | } | 
| 46 | 46 | $db = $sparql_last_connection; | 
| @@ -62,14 +62,14 @@ discard block | ||
| 62 | 62 | var $params = null; | 
| 63 | 63 | # capabilities are either true, false or null if not yet tested. | 
| 64 | 64 | |
| 65 | - function __construct( $endpoint ) | |
| 65 | + function __construct($endpoint) | |
| 66 | 66 |  	{ | 
| 67 | 67 | $this->endpoint = $endpoint; | 
| 68 | 68 | global $sparql_last_connection; | 
| 69 | 69 | $sparql_last_connection = $this; | 
| 70 | 70 | } | 
| 71 | 71 | |
| 72 | - function ns( $short, $long ) | |
| 72 | + function ns($short, $long) | |
| 73 | 73 |  	{ | 
| 74 | 74 | $this->ns[$short] = $long; | 
| 75 | 75 | } | 
| @@ -77,77 +77,77 @@ discard block | ||
| 77 | 77 |  	function errno() { return $this->errno; } | 
| 78 | 78 |  	function error() { return $this->error; } | 
| 79 | 79 | |
| 80 | - function cgiParams( $params = null ) | |
| 80 | + function cgiParams($params = null) | |
| 81 | 81 |  	{ | 
| 82 | -		if( $params === null ) { return $this->params; } | |
| 83 | -		if( $params === "" ) { $this->params = null; return; } | |
| 82 | +		if ($params === null) { return $this->params; } | |
| 83 | +		if ($params === "") { $this->params = null; return; } | |
| 84 | 84 | $this->params = $params; | 
| 85 | 85 | } | 
| 86 | 86 | |
| 87 | - function query( $query, $timeout=null ) | |
| 87 | + function query($query, $timeout = null) | |
| 88 | 88 |  	{	 | 
| 89 | 89 | $prefixes = ""; | 
| 90 | - foreach( $this->ns as $k=>$v ) | |
| 90 | + foreach ($this->ns as $k=>$v) | |
| 91 | 91 |  		{ | 
| 92 | 92 | $prefixes .= "PREFIX $k: <$v>\n"; | 
| 93 | 93 | } | 
| 94 | - $output = $this->dispatchQuery( $prefixes.$query, $timeout ); | |
| 95 | -		if( $this->errno ) { return; } | |
| 94 | + $output = $this->dispatchQuery($prefixes.$query, $timeout); | |
| 95 | +		if ($this->errno) { return; } | |
| 96 | 96 | $parser = new xx_xml($output, 'contents'); | 
| 97 | - if( $parser->error() ) | |
| 97 | + if ($parser->error()) | |
| 98 | 98 |  		{  | 
| 99 | 99 | $this->errno = -1; # to not clash with CURLOPT return; } | 
| 100 | 100 | $this->error = $parser->error(); | 
| 101 | 101 | return; | 
| 102 | 102 | } | 
| 103 | - return new sparql_result( $this, $parser->rows, $parser->fields ); | |
| 103 | + return new sparql_result($this, $parser->rows, $parser->fields); | |
| 104 | 104 | } | 
| 105 | 105 | |
| 106 | - function alive( $timeout=3 ) | |
| 106 | + function alive($timeout = 3) | |
| 107 | 107 |  	{ | 
| 108 | -		$result = $this->query( "SELECT * WHERE { ?s ?p ?o } LIMIT 1", $timeout ); | |
| 108 | +		$result = $this->query("SELECT * WHERE { ?s ?p ?o } LIMIT 1", $timeout); | |
| 109 | 109 | |
| 110 | -		if( $this->errno ) { return false; } | |
| 110 | +		if ($this->errno) { return false; } | |
| 111 | 111 | |
| 112 | 112 | return true; | 
| 113 | 113 | } | 
| 114 | 114 | |
| 115 | - function dispatchQuery( $sparql, $timeout=null ) | |
| 115 | + function dispatchQuery($sparql, $timeout = null) | |
| 116 | 116 |  	{ | 
| 117 | - $url = $this->endpoint."?query=".urlencode( $sparql ); | |
| 118 | - if( $this->params !== null ) | |
| 117 | + $url = $this->endpoint."?query=".urlencode($sparql); | |
| 118 | + if ($this->params !== null) | |
| 119 | 119 |  		{ | 
| 120 | 120 | $url .= "&".$this->params; | 
| 121 | 121 | } | 
| 122 | -		if( $this->debug ) { print "<div class='debug'><a href='".htmlspecialchars($url)."'>".htmlspecialchars($prefixes.$query)."</a></div>\n"; } | |
| 122 | +		if ($this->debug) { print "<div class='debug'><a href='".htmlspecialchars($url)."'>".htmlspecialchars($prefixes.$query)."</a></div>\n"; } | |
| 123 | 123 | $this->errno = null; | 
| 124 | 124 | $this->error = null; | 
| 125 | 125 | $ch = curl_init($url); | 
| 126 | 126 | #curl_setopt($ch, CURLOPT_HEADER, 1); | 
| 127 | - if( $timeout !== null ) | |
| 127 | + if ($timeout !== null) | |
| 128 | 128 |  		{ | 
| 129 | - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ); | |
| 129 | + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | |
| 130 | 130 | } | 
| 131 | 131 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | 
| 132 | - curl_setopt($ch, CURLOPT_HTTPHEADER,array ( | |
| 132 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
| 133 | 133 | "Accept: application/sparql-results+xml" | 
| 134 | 134 | )); | 
| 135 | 135 | |
| 136 | 136 | $output = curl_exec($ch); | 
| 137 | 137 | $info = curl_getinfo($ch); | 
| 138 | - if(curl_errno($ch)) | |
| 138 | + if (curl_errno($ch)) | |
| 139 | 139 |  		{ | 
| 140 | - $this->errno = curl_errno( $ch ); | |
| 141 | - $this->error = 'Curl error: ' . curl_error($ch); | |
| 140 | + $this->errno = curl_errno($ch); | |
| 141 | + $this->error = 'Curl error: '.curl_error($ch); | |
| 142 | 142 | return; | 
| 143 | 143 | } | 
| 144 | - if( $output === '' ) | |
| 144 | + if ($output === '') | |
| 145 | 145 |  		{ | 
| 146 | 146 | $this->errno = "-1"; | 
| 147 | 147 | $this->error = 'URL returned no data'; | 
| 148 | 148 | return; | 
| 149 | 149 | } | 
| 150 | - if( $info['http_code'] != 200) | |
| 150 | + if ($info['http_code'] != 200) | |
| 151 | 151 |  		{ | 
| 152 | 152 | $this->errno = $info['http_code']; | 
| 153 | 153 | $this->error = 'Bad response, '.$info['http_code'].': '.$output; | 
| @@ -179,13 +179,13 @@ discard block | ||
| 179 | 179 | |
| 180 | 180 | var $caps_cache; | 
| 181 | 181 | var $caps_anysubject; | 
| 182 | - function capabilityCache( $filename, $dba_type='db4' ) | |
| 182 | + function capabilityCache($filename, $dba_type = 'db4') | |
| 183 | 183 |  	{ | 
| 184 | - $this->caps_cache = dba_open($filename, "c", $dba_type ); | |
| 184 | + $this->caps_cache = dba_open($filename, "c", $dba_type); | |
| 185 | 185 | } | 
| 186 | 186 | function capabilityCodes() | 
| 187 | 187 |  	{ | 
| 188 | - return array_keys( $this->caps_desc ); | |
| 188 | + return array_keys($this->caps_desc); | |
| 189 | 189 | } | 
| 190 | 190 | function capabilityDescription($code) | 
| 191 | 191 |  	{ | 
| @@ -194,19 +194,19 @@ discard block | ||
| 194 | 194 | |
| 195 | 195 | # return true if the endpoint supports a capability | 
| 196 | 196 | # nb. returns false if connecion isn't authoriased to use the feature, eg LOAD | 
| 197 | - function supports( $code ) | |
| 197 | + function supports($code) | |
| 198 | 198 |  	{ | 
| 199 | -		if( isset( $this->caps[$code] ) ) { return $this->caps[$code]; } | |
| 199 | +		if (isset($this->caps[$code])) { return $this->caps[$code]; } | |
| 200 | 200 | $was_cached = false; | 
| 201 | - if( isset( $this->caps_cache ) ) | |
| 201 | + if (isset($this->caps_cache)) | |
| 202 | 202 |  		{ | 
| 203 | 203 | $CACHE_TIMEOUT_SECONDS = 7*24*60*60; | 
| 204 | 204 | $db_key = $this->endpoint.";".$code; | 
| 205 | - $db_val = dba_fetch( $db_key, $this->caps_cache ); | |
| 206 | - if( $db_val !== false ) | |
| 205 | + $db_val = dba_fetch($db_key, $this->caps_cache); | |
| 206 | + if ($db_val !== false) | |
| 207 | 207 |  			{ | 
| 208 | - list( $result, $when ) = preg_split( '/;/', $db_val ); | |
| 209 | - if( $when + $CACHE_TIMEOUT_SECONDS > time() ) | |
| 208 | +				list($result, $when) = preg_split('/;/', $db_val); | |
| 209 | + if ($when + $CACHE_TIMEOUT_SECONDS > time()) | |
| 210 | 210 |  				{ | 
| 211 | 211 | return $result; | 
| 212 | 212 | } | 
| @@ -215,26 +215,26 @@ discard block | ||
| 215 | 215 | } | 
| 216 | 216 | $r = null; | 
| 217 | 217 | |
| 218 | -		if( $code == "select" ) { $r = $this->test_select(); } | |
| 219 | -		elseif( $code == "constant_as" ) { $r = $this->test_constant_as(); } | |
| 220 | -		elseif( $code == "math_as" ) { $r = $this->test_math_as(); } | |
| 221 | -		elseif( $code == "count" ) { $r = $this->test_count(); } | |
| 222 | -		elseif( $code == "max" ) { $r = $this->test_max(); } | |
| 223 | -		elseif( $code == "load" ) { $r = $this->test_load(); } | |
| 224 | -		elseif( $code == "sample" ) { $r = $this->test_sample(); } | |
| 218 | +		if ($code == "select") { $r = $this->test_select(); } | |
| 219 | +		elseif ($code == "constant_as") { $r = $this->test_constant_as(); } | |
| 220 | +		elseif ($code == "math_as") { $r = $this->test_math_as(); } | |
| 221 | +		elseif ($code == "count") { $r = $this->test_count(); } | |
| 222 | +		elseif ($code == "max") { $r = $this->test_max(); } | |
| 223 | +		elseif ($code == "load") { $r = $this->test_load(); } | |
| 224 | +		elseif ($code == "sample") { $r = $this->test_sample(); } | |
| 225 | 225 |  		else { print "<p>Unknown capability code: '$code'</p>"; return false; } | 
| 226 | 226 | $this->caps[$code] = $r; | 
| 227 | - if( isset( $this->caps_cache ) ) | |
| 227 | + if (isset($this->caps_cache)) | |
| 228 | 228 |  		{ | 
| 229 | 229 | $db_key = $this->endpoint.";".$code; | 
| 230 | 230 | $db_val = $r.";".time(); | 
| 231 | - if( $was_cached ) | |
| 231 | + if ($was_cached) | |
| 232 | 232 |  			{ | 
| 233 | - dba_replace( $db_key, $db_val, $this->caps_cache ); | |
| 233 | + dba_replace($db_key, $db_val, $this->caps_cache); | |
| 234 | 234 | } | 
| 235 | 235 | else | 
| 236 | 236 |  			{ | 
| 237 | - dba_insert( $db_key, $db_val, $this->caps_cache ); | |
| 237 | + dba_insert($db_key, $db_val, $this->caps_cache); | |
| 238 | 238 | } | 
| 239 | 239 | } | 
| 240 | 240 | return $r; | 
| @@ -242,11 +242,11 @@ discard block | ||
| 242 | 242 | |
| 243 | 243 | function anySubject() | 
| 244 | 244 |  	{ | 
| 245 | - if( !isset( $this->caps_anysubject ) ) | |
| 245 | + if (!isset($this->caps_anysubject)) | |
| 246 | 246 |  		{ | 
| 247 | 247 | $results = $this->query( | 
| 248 | 248 |  			  "SELECT * WHERE { ?s ?p ?o } LIMIT 1" ); | 
| 249 | - if( sizeof($results)) | |
| 249 | + if (sizeof($results)) | |
| 250 | 250 |  			{ | 
| 251 | 251 | $row = $results->fetch_array(); | 
| 252 | 252 | $this->caps_anysubject = $row["s"]; | 
| @@ -260,7 +260,7 @@ discard block | ||
| 260 | 260 |  	{ | 
| 261 | 261 | $output = $this->dispatchQuery( | 
| 262 | 262 |  		  "SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 1" ); | 
| 263 | - return !isset( $this->errno ); | |
| 263 | + return !isset($this->errno); | |
| 264 | 264 | } | 
| 265 | 265 | |
| 266 | 266 | # return true if the endpoint supports AS | 
| @@ -268,7 +268,7 @@ discard block | ||
| 268 | 268 |  	{ | 
| 269 | 269 | $output = $this->dispatchQuery( | 
| 270 | 270 |  		  "SELECT (1+2 AS ?bar) WHERE { ?s ?p ?o } LIMIT 1" ); | 
| 271 | - return !isset( $this->errno ); | |
| 271 | + return !isset($this->errno); | |
| 272 | 272 | } | 
| 273 | 273 | |
| 274 | 274 | # return true if the endpoint supports AS | 
| @@ -276,7 +276,7 @@ discard block | ||
| 276 | 276 |  	{ | 
| 277 | 277 | $output = $this->dispatchQuery( | 
| 278 | 278 |  		  "SELECT (\"foo\" AS ?bar) WHERE { ?s ?p ?o } LIMIT 1" ); | 
| 279 | - return !isset( $this->errno ); | |
| 279 | + return !isset($this->errno); | |
| 280 | 280 | } | 
| 281 | 281 | |
| 282 | 282 | # return true if the endpoint supports SELECT (COUNT(?x) as ?n) ... GROUP BY | 
| @@ -284,35 +284,35 @@ discard block | ||
| 284 | 284 |  	{ | 
| 285 | 285 | # assumes at least one rdf:type predicate | 
| 286 | 286 | $s = $this->anySubject(); | 
| 287 | -		if( !isset($s) ) { return false; } | |
| 287 | +		if (!isset($s)) { return false; } | |
| 288 | 288 | $output = $this->dispatchQuery( | 
| 289 | 289 |  		  "SELECT (COUNT(?p) AS ?n) ?o WHERE { <$s> ?p ?o } GROUP BY ?o" ); | 
| 290 | - return !isset( $this->errno ); | |
| 290 | + return !isset($this->errno); | |
| 291 | 291 | } | 
| 292 | 292 | |
| 293 | 293 | function test_max() | 
| 294 | 294 |  	{ | 
| 295 | 295 | $s = $this->anySubject(); | 
| 296 | -		if( !isset($s) ) { return false; } | |
| 296 | +		if (!isset($s)) { return false; } | |
| 297 | 297 | $output = $this->dispatchQuery( | 
| 298 | 298 |  		  "SELECT (MAX(?p) AS ?max) ?o WHERE { <$s> ?p ?o } GROUP BY ?o" ); | 
| 299 | - return !isset( $this->errno ); | |
| 299 | + return !isset($this->errno); | |
| 300 | 300 | } | 
| 301 | 301 | |
| 302 | 302 | function test_sample() | 
| 303 | 303 |  	{ | 
| 304 | 304 | $s = $this->anySubject(); | 
| 305 | -		if( !isset($s) ) { return false; } | |
| 305 | +		if (!isset($s)) { return false; } | |
| 306 | 306 | $output = $this->dispatchQuery( | 
| 307 | 307 |  		  "SELECT (SAMPLE(?p) AS ?sam) ?o WHERE { <$s> ?p ?o } GROUP BY ?o" ); | 
| 308 | - return !isset( $this->errno ); | |
| 308 | + return !isset($this->errno); | |
| 309 | 309 | } | 
| 310 | 310 | |
| 311 | 311 | function test_load() | 
| 312 | 312 |  	{ | 
| 313 | 313 | $output = $this->dispatchQuery( | 
| 314 | 314 | "LOAD <http://graphite.ecs.soton.ac.uk/sparqllib/examples/loadtest.rdf>" ); | 
| 315 | - return !isset( $this->errno ); | |
| 315 | + return !isset($this->errno); | |
| 316 | 316 | } | 
| 317 | 317 | |
| 318 | 318 | |
| @@ -324,7 +324,7 @@ discard block | ||
| 324 | 324 | var $fields; | 
| 325 | 325 | var $db; | 
| 326 | 326 | var $i = 0; | 
| 327 | - function __construct( $db, $rows, $fields ) | |
| 327 | + function __construct($db, $rows, $fields) | |
| 328 | 328 |  	{ | 
| 329 | 329 | $this->rows = $rows; | 
| 330 | 330 | $this->fields = $fields; | 
| @@ -333,17 +333,17 @@ discard block | ||
| 333 | 333 | |
| 334 | 334 | function fetch_array() | 
| 335 | 335 |  	{ | 
| 336 | -		if( !@$this->rows[$this->i] ) { return; } | |
| 336 | +		if (!@$this->rows[$this->i]) { return; } | |
| 337 | 337 | $r = array(); | 
| 338 | - foreach( $this->rows[$this->i++] as $k=>$v ) | |
| 338 | + foreach ($this->rows[$this->i++] as $k=>$v) | |
| 339 | 339 |  		{ | 
| 340 | 340 | $r[$k] = $v["value"]; | 
| 341 | 341 | $r["$k.type"] = $v["type"]; | 
| 342 | - if( isset( $v["language"] ) ) | |
| 342 | + if (isset($v["language"])) | |
| 343 | 343 |  			{ | 
| 344 | 344 | $r["$k.language"] = $v["language"]; | 
| 345 | 345 | } | 
| 346 | - if( isset( $v["datatype"] ) ) | |
| 346 | + if (isset($v["datatype"])) | |
| 347 | 347 |  			{ | 
| 348 | 348 | $r["$k.datatype"] = $v["datatype"]; | 
| 349 | 349 | } | 
| @@ -355,16 +355,16 @@ discard block | ||
| 355 | 355 |  	{ | 
| 356 | 356 | $r = new sparql_results(); | 
| 357 | 357 | $r->fields = $this->fields; | 
| 358 | - foreach( $this->rows as $i=>$row ) | |
| 358 | + foreach ($this->rows as $i=>$row) | |
| 359 | 359 |  		{ | 
| 360 | - $r []= $this->fetch_array(); | |
| 360 | + $r [] = $this->fetch_array(); | |
| 361 | 361 | } | 
| 362 | 362 | return $r; | 
| 363 | 363 | } | 
| 364 | 364 | |
| 365 | 365 | function num_rows() | 
| 366 | 366 |  	{ | 
| 367 | - return sizeof( $this->rows ); | |
| 367 | + return sizeof($this->rows); | |
| 368 | 368 | } | 
| 369 | 369 | |
| 370 | 370 | function field_array() | 
| @@ -400,7 +400,7 @@ discard block | ||
| 400 | 400 | var $type; | 
| 401 | 401 | |
| 402 | 402 | // function with the default parameter value | 
| 403 | -	function xx_xml($url='http://www.opocot.com', $type='url') { | |
| 403 | +	function xx_xml($url = 'http://www.opocot.com', $type = 'url') { | |
| 404 | 404 | $this->type = $type; | 
| 405 | 405 | $this->url = $url; | 
| 406 | 406 | $this->parse(); | 
| @@ -414,7 +414,7 @@ discard block | ||
| 414 | 414 | $this->rows = array(); | 
| 415 | 415 | $this->fields = array(); | 
| 416 | 416 | $data = ''; | 
| 417 | -		$this->parser = xml_parser_create ("UTF-8"); | |
| 417 | +		$this->parser = xml_parser_create("UTF-8"); | |
| 418 | 418 | xml_set_object($this->parser, $this); | 
| 419 | 419 | xml_set_element_handler($this->parser, 'startXML', 'endXML'); | 
| 420 | 420 | xml_set_character_data_handler($this->parser, 'charXML'); | 
| @@ -439,9 +439,9 @@ discard block | ||
| 439 | 439 |  	 	} else if ($this->type == 'contents') { | 
| 440 | 440 | // Now we can pass the contents, maybe if you want | 
| 441 | 441 | // to use CURL, SOCK or other method. | 
| 442 | -			$lines = explode("\n",$this->url); | |
| 442 | +			$lines = explode("\n", $this->url); | |
| 443 | 443 |  			foreach ($lines as $val) { | 
| 444 | - $data = $val . "\n"; | |
| 444 | + $data = $val."\n"; | |
| 445 | 445 |  				if (!xml_parse($this->parser, $data)) { | 
| 446 | 446 |  					$this->error = $data."\n".sprintf('XML error at line %d column %d', | 
| 447 | 447 | xml_get_current_line_number($this->parser), | 
| @@ -450,7 +450,7 @@ discard block | ||
| 450 | 450 | } | 
| 451 | 451 | } | 
| 452 | 452 | } | 
| 453 | - if( !$this->looks_legit ) | |
| 453 | + if (!$this->looks_legit) | |
| 454 | 454 |  		{ | 
| 455 | 455 | $this->error = "Didn't even see a sparql element, is this really an endpoint?"; | 
| 456 | 456 | } | 
| @@ -458,53 +458,53 @@ discard block | ||
| 458 | 458 | |
| 459 | 459 | function startXML($parser, $name, $attr) | 
| 460 | 460 |  	{ | 
| 461 | -		if( $name == "sparql" ) { $this->looks_legit = true; } | |
| 462 | - if( $name == "result" ) | |
| 461 | +		if ($name == "sparql") { $this->looks_legit = true; } | |
| 462 | + if ($name == "result") | |
| 463 | 463 |  		{ | 
| 464 | 464 | $this->result = array(); | 
| 465 | 465 | } | 
| 466 | - if( $name == "binding" ) | |
| 466 | + if ($name == "binding") | |
| 467 | 467 |  		{ | 
| 468 | 468 | $this->part = $attr["name"]; | 
| 469 | 469 | } | 
| 470 | - if( $name == "uri" || $name == "bnode" ) | |
| 470 | + if ($name == "uri" || $name == "bnode") | |
| 471 | 471 |  		{ | 
| 472 | 472 | $this->part_type = $name; | 
| 473 | 473 | $this->chars = ""; | 
| 474 | 474 | } | 
| 475 | - if( $name == "literal" ) | |
| 475 | + if ($name == "literal") | |
| 476 | 476 |  		{ | 
| 477 | 477 | $this->part_type = "literal"; | 
| 478 | - if( isset( $attr["datatype"] ) ) | |
| 478 | + if (isset($attr["datatype"])) | |
| 479 | 479 |  			{ | 
| 480 | 480 | $this->part_datatype = $attr["datatype"]; | 
| 481 | 481 | } | 
| 482 | - if( isset( $attr["xml:lang"] ) ) | |
| 482 | + if (isset($attr["xml:lang"])) | |
| 483 | 483 |  			{ | 
| 484 | 484 | $this->part_lang = $attr["xml:lang"]; | 
| 485 | 485 | } | 
| 486 | 486 | $this->chars = ""; | 
| 487 | 487 | } | 
| 488 | - if( $name == "variable" ) | |
| 488 | + if ($name == "variable") | |
| 489 | 489 |  		{ | 
| 490 | 490 | $this->fields[] = $attr["name"]; | 
| 491 | 491 | } | 
| 492 | 492 | } | 
| 493 | 493 | |
| 494 | -	function endXML($parser, $name)	{ | |
| 495 | - if( $name == "result" ) | |
| 494 | +	function endXML($parser, $name) { | |
| 495 | + if ($name == "result") | |
| 496 | 496 |  		{ | 
| 497 | 497 | $this->rows[] = $this->result; | 
| 498 | 498 | $this->result = array(); | 
| 499 | 499 | } | 
| 500 | - if( $name == "uri" || $name == "bnode" || $name == "literal" ) | |
| 500 | + if ($name == "uri" || $name == "bnode" || $name == "literal") | |
| 501 | 501 |  		{ | 
| 502 | - $this->result[$this->part] = array( "type"=>$name, "value"=>$this->chars ); | |
| 503 | - if( isset( $this->part_lang ) ) | |
| 502 | +			$this->result[$this->part] = array("type"=>$name, "value"=>$this->chars); | |
| 503 | + if (isset($this->part_lang)) | |
| 504 | 504 |  			{ | 
| 505 | 505 | $this->result[$this->part]["lang"] = $this->part_lang; | 
| 506 | 506 | } | 
| 507 | - if( isset( $this->part_datatype ) ) | |
| 507 | + if (isset($this->part_datatype)) | |
| 508 | 508 |  			{ | 
| 509 | 509 | $this->result[$this->part]["datatype"] = $this->part_datatype; | 
| 510 | 510 | } | 
| @@ -513,7 +513,7 @@ discard block | ||
| 513 | 513 | } | 
| 514 | 514 | } | 
| 515 | 515 | |
| 516 | -	function charXML($parser, $data)	{ | |
| 516 | +	function charXML($parser, $data) { | |
| 517 | 517 | @$this->chars .= $data; | 
| 518 | 518 | } | 
| 519 | 519 | |
| @@ -527,28 +527,28 @@ discard block | ||
| 527 | 527 | function render_table() | 
| 528 | 528 |  	{ | 
| 529 | 529 | $html = "<table class='sparql-results'><tr>"; | 
| 530 | - foreach( $this->fields as $i=>$field ) | |
| 530 | + foreach ($this->fields as $i=>$field) | |
| 531 | 531 |  		{ | 
| 532 | 532 | $html .= "<th>?$field</th>"; | 
| 533 | 533 | } | 
| 534 | 534 | $html .= "</tr>"; | 
| 535 | - foreach( $this as $row ) | |
| 535 | + foreach ($this as $row) | |
| 536 | 536 |  		{ | 
| 537 | - $html.="<tr>"; | |
| 538 | - foreach( $row as $cell ) | |
| 537 | + $html .= "<tr>"; | |
| 538 | + foreach ($row as $cell) | |
| 539 | 539 |  			{ | 
| 540 | - $html .= "<td>".htmlspecialchars( $cell )."</td>"; | |
| 540 | + $html .= "<td>".htmlspecialchars($cell)."</td>"; | |
| 541 | 541 | } | 
| 542 | - $html.="</tr>"; | |
| 542 | + $html .= "</tr>"; | |
| 543 | 543 | } | 
| 544 | - $html.="</table> | |
| 544 | + $html .= "</table> | |
| 545 | 545 | <style> | 
| 546 | 546 |  table.sparql-results { border-collapse: collapse; } | 
| 547 | 547 |  table.sparql-results tr td { border: solid 1px #000 ; padding:4px;vertical-align:top} | 
| 548 | 548 |  table.sparql-results tr th { border: solid 1px #000 ; padding:4px;vertical-align:top ; background-color:#eee;} | 
| 549 | 549 | </style> | 
| 550 | 550 | "; | 
| 551 | - return $html;exit; | |
| 551 | + return $html; exit; | |
| 552 | 552 | } | 
| 553 | 553 | |
| 554 | 554 | } | 
| @@ -215,14 +215,7 @@ discard block | ||
| 215 | 215 | } | 
| 216 | 216 | $r = null; | 
| 217 | 217 | |
| 218 | -		if( $code == "select" ) { $r = $this->test_select(); } | |
| 219 | -		elseif( $code == "constant_as" ) { $r = $this->test_constant_as(); } | |
| 220 | -		elseif( $code == "math_as" ) { $r = $this->test_math_as(); } | |
| 221 | -		elseif( $code == "count" ) { $r = $this->test_count(); } | |
| 222 | -		elseif( $code == "max" ) { $r = $this->test_max(); } | |
| 223 | -		elseif( $code == "load" ) { $r = $this->test_load(); } | |
| 224 | -		elseif( $code == "sample" ) { $r = $this->test_sample(); } | |
| 225 | -		else { print "<p>Unknown capability code: '$code'</p>"; return false; } | |
| 218 | +		if( $code == "select" ) { $r = $this->test_select(); } elseif( $code == "constant_as" ) { $r = $this->test_constant_as(); } elseif( $code == "math_as" ) { $r = $this->test_math_as(); } elseif( $code == "count" ) { $r = $this->test_count(); } elseif( $code == "max" ) { $r = $this->test_max(); } elseif( $code == "load" ) { $r = $this->test_load(); } elseif( $code == "sample" ) { $r = $this->test_sample(); } else { print "<p>Unknown capability code: '$code'</p>"; return false; } | |
| 226 | 219 | $this->caps[$code] = $r; | 
| 227 | 220 | if( isset( $this->caps_cache ) ) | 
| 228 | 221 |  		{ | 
| @@ -231,8 +224,7 @@ discard block | ||
| 231 | 224 | if( $was_cached ) | 
| 232 | 225 |  			{ | 
| 233 | 226 | dba_replace( $db_key, $db_val, $this->caps_cache ); | 
| 234 | - } | |
| 235 | - else | |
| 227 | + } else | |
| 236 | 228 |  			{ | 
| 237 | 229 | dba_insert( $db_key, $db_val, $this->caps_cache ); | 
| 238 | 230 | } | 
| @@ -118,6 +118,9 @@ discard block | ||
| 118 | 118 | ); | 
| 119 | 119 | } | 
| 120 | 120 | |
| 121 | + /** | |
| 122 | + * @param boolean $force | |
| 123 | + */ | |
| 121 | 124 |    static function geosInstalled($force = NULL) {
 | 
| 122 | 125 | static $geos_installed = NULL; | 
| 123 | 126 | if ($force !== NULL) $geos_installed = $force; | 
| @@ -222,6 +225,10 @@ discard block | ||
| 222 | 225 | |
| 223 | 226 | // Detect a format given a value. This function is meant to be SPEEDY. | 
| 224 | 227 | // It could make a mistake in XML detection if you are mixing or using namespaces in weird ways (ie, KML inside an RSS feed) | 
| 228 | + | |
| 229 | + /** | |
| 230 | + * @return string | |
| 231 | + */ | |
| 225 | 232 |    static function detectFormat(&$input) {
 | 
| 226 | 233 |      $mem = fopen('php://memory', 'r+');
 | 
| 227 | 234 | fwrite($mem, $input, 11); // Write 11 bytes - we can detect the vast majority of formats in the first 11 bytes | 
| @@ -35,110 +35,110 @@ discard block | ||
| 35 | 35 |  {
 | 
| 36 | 36 | |
| 37 | 37 |    static function version() {
 | 
| 38 | - return '1.1'; | |
| 38 | + return '1.1'; | |
| 39 | 39 | } | 
| 40 | 40 | |
| 41 | 41 | // geoPHP::load($data, $type, $other_args); | 
| 42 | 42 | // if $data is an array, all passed in values will be combined into a single geometry | 
| 43 | 43 |    static function load() {
 | 
| 44 | - $args = func_get_args(); | |
| 44 | + $args = func_get_args(); | |
| 45 | 45 | |
| 46 | - $data = array_shift($args); | |
| 47 | - $type = array_shift($args); | |
| 46 | + $data = array_shift($args); | |
| 47 | + $type = array_shift($args); | |
| 48 | 48 | |
| 49 | - $type_map = geoPHP::getAdapterMap(); | |
| 49 | + $type_map = geoPHP::getAdapterMap(); | |
| 50 | 50 | |
| 51 | - // Auto-detect type if needed | |
| 52 | -    if (!$type) {
 | |
| 53 | - // If the user is trying to load a Geometry from a Geometry... Just pass it back | |
| 54 | -      if (is_object($data)) {
 | |
| 55 | - if ($data instanceOf Geometry) return $data; | |
| 56 | - } | |
| 51 | + // Auto-detect type if needed | |
| 52 | +	if (!$type) {
 | |
| 53 | + // If the user is trying to load a Geometry from a Geometry... Just pass it back | |
| 54 | +	  if (is_object($data)) {
 | |
| 55 | + if ($data instanceOf Geometry) return $data; | |
| 56 | + } | |
| 57 | 57 | |
| 58 | - $detected = geoPHP::detectFormat($data); | |
| 59 | -      if (!$detected) {
 | |
| 60 | - return FALSE; | |
| 61 | - } | |
| 58 | + $detected = geoPHP::detectFormat($data); | |
| 59 | +	  if (!$detected) {
 | |
| 60 | + return FALSE; | |
| 61 | + } | |
| 62 | 62 | |
| 63 | -      $format = explode(':', $detected);
 | |
| 64 | - $type = array_shift($format); | |
| 65 | - $args = $format; | |
| 66 | - } | |
| 67 | - | |
| 68 | - $processor_type = $type_map[$type]; | |
| 69 | - | |
| 70 | -    if (!$processor_type) {
 | |
| 71 | -      throw new exception('geoPHP could not find an adapter of type '.htmlentities($type));
 | |
| 72 | - exit; | |
| 73 | - } | |
| 74 | - | |
| 75 | - $processor = new $processor_type(); | |
| 76 | - | |
| 77 | - // Data is not an array, just pass it normally | |
| 78 | -    if (!is_array($data)) {
 | |
| 79 | - $result = call_user_func_array(array($processor, "read"), array_merge(array($data), $args)); | |
| 80 | - } | |
| 81 | - // Data is an array, combine all passed in items into a single geomtetry | |
| 82 | -    else {
 | |
| 83 | - $geoms = array(); | |
| 84 | -      foreach ($data as $item) {
 | |
| 85 | - $geoms[] = call_user_func_array(array($processor, "read"), array_merge(array($item), $args)); | |
| 86 | - } | |
| 87 | - $result = geoPHP::geometryReduce($geoms); | |
| 88 | - } | |
| 89 | - | |
| 90 | - return $result; | |
| 63 | +	  $format = explode(':', $detected);
 | |
| 64 | + $type = array_shift($format); | |
| 65 | + $args = $format; | |
| 66 | + } | |
| 67 | + | |
| 68 | + $processor_type = $type_map[$type]; | |
| 69 | + | |
| 70 | +	if (!$processor_type) {
 | |
| 71 | +	  throw new exception('geoPHP could not find an adapter of type '.htmlentities($type));
 | |
| 72 | + exit; | |
| 73 | + } | |
| 74 | + | |
| 75 | + $processor = new $processor_type(); | |
| 76 | + | |
| 77 | + // Data is not an array, just pass it normally | |
| 78 | +	if (!is_array($data)) {
 | |
| 79 | + $result = call_user_func_array(array($processor, "read"), array_merge(array($data), $args)); | |
| 80 | + } | |
| 81 | + // Data is an array, combine all passed in items into a single geomtetry | |
| 82 | +	else {
 | |
| 83 | + $geoms = array(); | |
| 84 | +	  foreach ($data as $item) {
 | |
| 85 | + $geoms[] = call_user_func_array(array($processor, "read"), array_merge(array($item), $args)); | |
| 86 | + } | |
| 87 | + $result = geoPHP::geometryReduce($geoms); | |
| 88 | + } | |
| 89 | + | |
| 90 | + return $result; | |
| 91 | 91 | } | 
| 92 | 92 | |
| 93 | 93 |    static function getAdapterMap() {
 | 
| 94 | - return array ( | |
| 95 | - 'wkt' => 'WKT', | |
| 96 | - 'ewkt' => 'EWKT', | |
| 97 | - 'wkb' => 'WKB', | |
| 98 | - 'ewkb' => 'EWKB', | |
| 99 | - 'json' => 'GeoJSON', | |
| 100 | - 'geojson' => 'GeoJSON', | |
| 101 | - 'kml' => 'KML', | |
| 102 | - 'gpx' => 'GPX', | |
| 103 | - 'georss' => 'GeoRSS', | |
| 104 | - 'google_geocode' => 'GoogleGeocode', | |
| 105 | - 'geohash' => 'GeoHash', | |
| 106 | - ); | |
| 94 | + return array ( | |
| 95 | + 'wkt' => 'WKT', | |
| 96 | + 'ewkt' => 'EWKT', | |
| 97 | + 'wkb' => 'WKB', | |
| 98 | + 'ewkb' => 'EWKB', | |
| 99 | + 'json' => 'GeoJSON', | |
| 100 | + 'geojson' => 'GeoJSON', | |
| 101 | + 'kml' => 'KML', | |
| 102 | + 'gpx' => 'GPX', | |
| 103 | + 'georss' => 'GeoRSS', | |
| 104 | + 'google_geocode' => 'GoogleGeocode', | |
| 105 | + 'geohash' => 'GeoHash', | |
| 106 | + ); | |
| 107 | 107 | } | 
| 108 | 108 | |
| 109 | 109 |    static function geometryList() {
 | 
| 110 | - return array( | |
| 111 | - 'point' => 'Point', | |
| 112 | - 'linestring' => 'LineString', | |
| 113 | - 'polygon' => 'Polygon', | |
| 114 | - 'multipoint' => 'MultiPoint', | |
| 115 | - 'multilinestring' => 'MultiLineString', | |
| 116 | - 'multipolygon' => 'MultiPolygon', | |
| 117 | - 'geometrycollection' => 'GeometryCollection', | |
| 118 | - ); | |
| 110 | + return array( | |
| 111 | + 'point' => 'Point', | |
| 112 | + 'linestring' => 'LineString', | |
| 113 | + 'polygon' => 'Polygon', | |
| 114 | + 'multipoint' => 'MultiPoint', | |
| 115 | + 'multilinestring' => 'MultiLineString', | |
| 116 | + 'multipolygon' => 'MultiPolygon', | |
| 117 | + 'geometrycollection' => 'GeometryCollection', | |
| 118 | + ); | |
| 119 | 119 | } | 
| 120 | 120 | |
| 121 | 121 |    static function geosInstalled($force = NULL) {
 | 
| 122 | - static $geos_installed = NULL; | |
| 123 | - if ($force !== NULL) $geos_installed = $force; | |
| 124 | -    if ($geos_installed !== NULL) {
 | |
| 125 | - return $geos_installed; | |
| 126 | - } | |
| 127 | -    $geos_installed = class_exists('GEOSGeometry');
 | |
| 128 | - return $geos_installed; | |
| 122 | + static $geos_installed = NULL; | |
| 123 | + if ($force !== NULL) $geos_installed = $force; | |
| 124 | +	if ($geos_installed !== NULL) {
 | |
| 125 | + return $geos_installed; | |
| 126 | + } | |
| 127 | +	$geos_installed = class_exists('GEOSGeometry');
 | |
| 128 | + return $geos_installed; | |
| 129 | 129 | } | 
| 130 | 130 | |
| 131 | 131 |    static function geosToGeometry($geos) {
 | 
| 132 | -    if (!geoPHP::geosInstalled()) {
 | |
| 133 | - return NULL; | |
| 134 | - } | |
| 135 | - $wkb_writer = new GEOSWKBWriter(); | |
| 136 | - $wkb = $wkb_writer->writeHEX($geos); | |
| 137 | - $geometry = geoPHP::load($wkb, 'wkb', TRUE); | |
| 138 | -    if ($geometry) {
 | |
| 139 | - $geometry->setGeos($geos); | |
| 140 | - return $geometry; | |
| 141 | - } | |
| 132 | +	if (!geoPHP::geosInstalled()) {
 | |
| 133 | + return NULL; | |
| 134 | + } | |
| 135 | + $wkb_writer = new GEOSWKBWriter(); | |
| 136 | + $wkb = $wkb_writer->writeHEX($geos); | |
| 137 | + $geometry = geoPHP::load($wkb, 'wkb', TRUE); | |
| 138 | +	if ($geometry) {
 | |
| 139 | + $geometry->setGeos($geos); | |
| 140 | + return $geometry; | |
| 141 | + } | |
| 142 | 142 | } | 
| 143 | 143 | |
| 144 | 144 | // Reduce a geometry, or an array of geometries, into their 'lowest' available common geometry. | 
| @@ -146,155 +146,155 @@ discard block | ||
| 146 | 146 | // A multi-point containing a single point will return a point. | 
| 147 | 147 | // An array of geometries can be passed and they will be compiled into a single geometry | 
| 148 | 148 |    static function geometryReduce($geometry) {
 | 
| 149 | - // If it's an array of one, then just parse the one | |
| 150 | -    if (is_array($geometry)) {
 | |
| 151 | - if (empty($geometry)) return FALSE; | |
| 152 | - if (count($geometry) == 1) return geoPHP::geometryReduce(array_shift($geometry)); | |
| 153 | - } | |
| 154 | - | |
| 155 | - // If the geometry cannot even theoretically be reduced more, then pass it back | |
| 156 | -    if (gettype($geometry) == 'object') {
 | |
| 157 | -      $passbacks = array('Point','LineString','Polygon');
 | |
| 158 | -      if (in_array($geometry->geometryType(),$passbacks)) {
 | |
| 159 | - return $geometry; | |
| 160 | - } | |
| 161 | - } | |
| 162 | - | |
| 163 | - // If it is a mutlti-geometry, check to see if it just has one member | |
| 164 | - // If it does, then pass the member, if not, then just pass back the geometry | |
| 165 | -    if (gettype($geometry) == 'object') {
 | |
| 166 | -      $simple_collections = array('MultiPoint','MultiLineString','MultiPolygon');
 | |
| 167 | -      if (in_array(get_class($geometry),$passbacks)) {
 | |
| 168 | - $components = $geometry->getComponents(); | |
| 169 | -        if (count($components) == 1) {
 | |
| 170 | - return $components[0]; | |
| 171 | - } | |
| 172 | -        else {
 | |
| 173 | - return $geometry; | |
| 174 | - } | |
| 175 | - } | |
| 176 | - } | |
| 177 | - | |
| 178 | - // So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections | |
| 179 | -    if (!is_array($geometry)) {
 | |
| 180 | - $geometry = array($geometry); | |
| 181 | - } | |
| 182 | - | |
| 183 | - $geometries = array(); | |
| 184 | - $geom_types = array(); | |
| 185 | - | |
| 186 | -    $collections = array('MultiPoint','MultiLineString','MultiPolygon','GeometryCollection');
 | |
| 187 | - | |
| 188 | -    foreach ($geometry as $item) {
 | |
| 189 | -      if ($item) {
 | |
| 190 | -        if (in_array(get_class($item), $collections)) {
 | |
| 191 | -          foreach ($item->getComponents() as $component) {
 | |
| 192 | - $geometries[] = $component; | |
| 193 | - $geom_types[] = $component->geometryType(); | |
| 194 | - } | |
| 195 | - } | |
| 196 | -        else {
 | |
| 197 | - $geometries[] = $item; | |
| 198 | - $geom_types[] = $item->geometryType(); | |
| 199 | - } | |
| 200 | - } | |
| 201 | - } | |
| 202 | - | |
| 203 | - $geom_types = array_unique($geom_types); | |
| 149 | + // If it's an array of one, then just parse the one | |
| 150 | +	if (is_array($geometry)) {
 | |
| 151 | + if (empty($geometry)) return FALSE; | |
| 152 | + if (count($geometry) == 1) return geoPHP::geometryReduce(array_shift($geometry)); | |
| 153 | + } | |
| 154 | + | |
| 155 | + // If the geometry cannot even theoretically be reduced more, then pass it back | |
| 156 | +	if (gettype($geometry) == 'object') {
 | |
| 157 | +	  $passbacks = array('Point','LineString','Polygon');
 | |
| 158 | +	  if (in_array($geometry->geometryType(),$passbacks)) {
 | |
| 159 | + return $geometry; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + // If it is a mutlti-geometry, check to see if it just has one member | |
| 164 | + // If it does, then pass the member, if not, then just pass back the geometry | |
| 165 | +	if (gettype($geometry) == 'object') {
 | |
| 166 | +	  $simple_collections = array('MultiPoint','MultiLineString','MultiPolygon');
 | |
| 167 | +	  if (in_array(get_class($geometry),$passbacks)) {
 | |
| 168 | + $components = $geometry->getComponents(); | |
| 169 | +		if (count($components) == 1) {
 | |
| 170 | + return $components[0]; | |
| 171 | + } | |
| 172 | +		else {
 | |
| 173 | + return $geometry; | |
| 174 | + } | |
| 175 | + } | |
| 176 | + } | |
| 177 | + | |
| 178 | + // So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections | |
| 179 | +	if (!is_array($geometry)) {
 | |
| 180 | + $geometry = array($geometry); | |
| 181 | + } | |
| 182 | + | |
| 183 | + $geometries = array(); | |
| 184 | + $geom_types = array(); | |
| 185 | + | |
| 186 | +	$collections = array('MultiPoint','MultiLineString','MultiPolygon','GeometryCollection');
 | |
| 187 | + | |
| 188 | +	foreach ($geometry as $item) {
 | |
| 189 | +	  if ($item) {
 | |
| 190 | +		if (in_array(get_class($item), $collections)) {
 | |
| 191 | +		  foreach ($item->getComponents() as $component) {
 | |
| 192 | + $geometries[] = $component; | |
| 193 | + $geom_types[] = $component->geometryType(); | |
| 194 | + } | |
| 195 | + } | |
| 196 | +		else {
 | |
| 197 | + $geometries[] = $item; | |
| 198 | + $geom_types[] = $item->geometryType(); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + $geom_types = array_unique($geom_types); | |
| 204 | 204 | |
| 205 | -    if (empty($geom_types)) {
 | |
| 206 | - return FALSE; | |
| 207 | - } | |
| 208 | - | |
| 209 | -    if (count($geom_types) == 1) {
 | |
| 210 | -      if (count($geometries) == 1) {
 | |
| 211 | - return $geometries[0]; | |
| 212 | - } | |
| 213 | -      else {
 | |
| 214 | - $class = 'Multi'.$geom_types[0]; | |
| 215 | - return new $class($geometries); | |
| 216 | - } | |
| 217 | - } | |
| 218 | -    else {
 | |
| 219 | - return new GeometryCollection($geometries); | |
| 220 | - } | |
| 205 | +	if (empty($geom_types)) {
 | |
| 206 | + return FALSE; | |
| 207 | + } | |
| 208 | + | |
| 209 | +	if (count($geom_types) == 1) {
 | |
| 210 | +	  if (count($geometries) == 1) {
 | |
| 211 | + return $geometries[0]; | |
| 212 | + } | |
| 213 | +	  else {
 | |
| 214 | + $class = 'Multi'.$geom_types[0]; | |
| 215 | + return new $class($geometries); | |
| 216 | + } | |
| 217 | + } | |
| 218 | +	else {
 | |
| 219 | + return new GeometryCollection($geometries); | |
| 220 | + } | |
| 221 | 221 | } | 
| 222 | 222 | |
| 223 | 223 | // Detect a format given a value. This function is meant to be SPEEDY. | 
| 224 | 224 | // It could make a mistake in XML detection if you are mixing or using namespaces in weird ways (ie, KML inside an RSS feed) | 
| 225 | 225 |    static function detectFormat(&$input) {
 | 
| 226 | -    $mem = fopen('php://memory', 'r+');
 | |
| 227 | - fwrite($mem, $input, 11); // Write 11 bytes - we can detect the vast majority of formats in the first 11 bytes | |
| 228 | - fseek($mem, 0); | |
| 229 | - | |
| 230 | -    $bytes = unpack("c*", fread($mem, 11));
 | |
| 231 | - | |
| 232 | - // If bytes is empty, then we were passed empty input | |
| 233 | - if (empty($bytes)) return FALSE; | |
| 234 | - | |
| 235 | - // First char is a tab, space or carriage-return. trim it and try again | |
| 236 | -    if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
 | |
| 237 | - return geoPHP::detectFormat(ltrim($input)); | |
| 238 | - } | |
| 239 | - | |
| 240 | - // Detect WKB or EWKB -- first byte is 1 (little endian indicator) | |
| 241 | -    if ($bytes[1] == 1) {
 | |
| 242 | - // If SRID byte is TRUE (1), it's EWKB | |
| 243 | - if ($bytes[5]) return 'ewkb'; | |
| 244 | - else return 'wkb'; | |
| 245 | - } | |
| 246 | - | |
| 247 | - // Detect HEX encoded WKB or EWKB (PostGIS format) -- first byte is 48, second byte is 49 (hex '01' => first-byte = 1) | |
| 248 | -    if ($bytes[1] == 48 && $bytes[2] == 49) {
 | |
| 249 | - // The shortest possible WKB string (LINESTRING EMPTY) is 18 hex-chars (9 encoded bytes) long | |
| 250 | - // This differentiates it from a geohash, which is always shorter than 18 characters. | |
| 251 | -      if (strlen($input) >= 18) {
 | |
| 252 | - //@@TODO: Differentiate between EWKB and WKB -- check hex-char 10 or 11 (SRID bool indicator at encoded byte 5) | |
| 253 | - return 'ewkb:1'; | |
| 254 | - } | |
| 255 | - } | |
| 256 | - | |
| 257 | -    // Detect GeoJSON - first char starts with {
 | |
| 258 | -    if ($bytes[1] == 123) {
 | |
| 259 | - return 'json'; | |
| 260 | - } | |
| 261 | - | |
| 262 | - // Detect EWKT - first char is S | |
| 263 | -    if ($bytes[1] == 83) {
 | |
| 264 | - return 'ewkt'; | |
| 265 | - } | |
| 266 | - | |
| 267 | - // Detect WKT - first char starts with P (80), L (76), M (77), or G (71) | |
| 268 | - $wkt_chars = array(80, 76, 77, 71); | |
| 269 | -    if (in_array($bytes[1], $wkt_chars)) {
 | |
| 270 | - return 'wkt'; | |
| 271 | - } | |
| 272 | - | |
| 273 | - // Detect XML -- first char is < | |
| 274 | -    if ($bytes[1] == 60) {
 | |
| 275 | - // grab the first 256 characters | |
| 276 | - $string = substr($input, 0, 256); | |
| 277 | - if (strpos($string, '<kml') !== FALSE) return 'kml'; | |
| 278 | - if (strpos($string, '<coordinate') !== FALSE) return 'kml'; | |
| 279 | - if (strpos($string, '<gpx') !== FALSE) return 'gpx'; | |
| 280 | - if (strpos($string, '<georss') !== FALSE) return 'georss'; | |
| 281 | - if (strpos($string, '<rss') !== FALSE) return 'georss'; | |
| 282 | - if (strpos($string, '<feed') !== FALSE) return 'georss'; | |
| 283 | - } | |
| 284 | - | |
| 285 | - // We need an 8 byte string for geohash and unpacked WKB / WKT | |
| 286 | - fseek($mem, 0); | |
| 287 | - $string = trim(fread($mem, 8)); | |
| 288 | - | |
| 289 | - // Detect geohash - geohash ONLY contains lowercase chars and numerics | |
| 290 | -    preg_match('/[a-z0-9]+/', $string, $matches);
 | |
| 291 | -    if ($matches[0] == $string) {
 | |
| 292 | - return 'geohash'; | |
| 293 | - } | |
| 294 | - | |
| 295 | - // What do you get when you cross an elephant with a rhino? | |
| 296 | - // http://youtu.be/RCBn5J83Poc | |
| 297 | - return FALSE; | |
| 226 | +	$mem = fopen('php://memory', 'r+');
 | |
| 227 | + fwrite($mem, $input, 11); // Write 11 bytes - we can detect the vast majority of formats in the first 11 bytes | |
| 228 | + fseek($mem, 0); | |
| 229 | + | |
| 230 | +	$bytes = unpack("c*", fread($mem, 11));
 | |
| 231 | + | |
| 232 | + // If bytes is empty, then we were passed empty input | |
| 233 | + if (empty($bytes)) return FALSE; | |
| 234 | + | |
| 235 | + // First char is a tab, space or carriage-return. trim it and try again | |
| 236 | +	if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
 | |
| 237 | + return geoPHP::detectFormat(ltrim($input)); | |
| 238 | + } | |
| 239 | + | |
| 240 | + // Detect WKB or EWKB -- first byte is 1 (little endian indicator) | |
| 241 | +	if ($bytes[1] == 1) {
 | |
| 242 | + // If SRID byte is TRUE (1), it's EWKB | |
| 243 | + if ($bytes[5]) return 'ewkb'; | |
| 244 | + else return 'wkb'; | |
| 245 | + } | |
| 246 | + | |
| 247 | + // Detect HEX encoded WKB or EWKB (PostGIS format) -- first byte is 48, second byte is 49 (hex '01' => first-byte = 1) | |
| 248 | +	if ($bytes[1] == 48 && $bytes[2] == 49) {
 | |
| 249 | + // The shortest possible WKB string (LINESTRING EMPTY) is 18 hex-chars (9 encoded bytes) long | |
| 250 | + // This differentiates it from a geohash, which is always shorter than 18 characters. | |
| 251 | +	  if (strlen($input) >= 18) {
 | |
| 252 | + //@@TODO: Differentiate between EWKB and WKB -- check hex-char 10 or 11 (SRID bool indicator at encoded byte 5) | |
| 253 | + return 'ewkb:1'; | |
| 254 | + } | |
| 255 | + } | |
| 256 | + | |
| 257 | +	// Detect GeoJSON - first char starts with {
 | |
| 258 | +	if ($bytes[1] == 123) {
 | |
| 259 | + return 'json'; | |
| 260 | + } | |
| 261 | + | |
| 262 | + // Detect EWKT - first char is S | |
| 263 | +	if ($bytes[1] == 83) {
 | |
| 264 | + return 'ewkt'; | |
| 265 | + } | |
| 266 | + | |
| 267 | + // Detect WKT - first char starts with P (80), L (76), M (77), or G (71) | |
| 268 | + $wkt_chars = array(80, 76, 77, 71); | |
| 269 | +	if (in_array($bytes[1], $wkt_chars)) {
 | |
| 270 | + return 'wkt'; | |
| 271 | + } | |
| 272 | + | |
| 273 | + // Detect XML -- first char is < | |
| 274 | +	if ($bytes[1] == 60) {
 | |
| 275 | + // grab the first 256 characters | |
| 276 | + $string = substr($input, 0, 256); | |
| 277 | + if (strpos($string, '<kml') !== FALSE) return 'kml'; | |
| 278 | + if (strpos($string, '<coordinate') !== FALSE) return 'kml'; | |
| 279 | + if (strpos($string, '<gpx') !== FALSE) return 'gpx'; | |
| 280 | + if (strpos($string, '<georss') !== FALSE) return 'georss'; | |
| 281 | + if (strpos($string, '<rss') !== FALSE) return 'georss'; | |
| 282 | + if (strpos($string, '<feed') !== FALSE) return 'georss'; | |
| 283 | + } | |
| 284 | + | |
| 285 | + // We need an 8 byte string for geohash and unpacked WKB / WKT | |
| 286 | + fseek($mem, 0); | |
| 287 | + $string = trim(fread($mem, 8)); | |
| 288 | + | |
| 289 | + // Detect geohash - geohash ONLY contains lowercase chars and numerics | |
| 290 | +	preg_match('/[a-z0-9]+/', $string, $matches);
 | |
| 291 | +	if ($matches[0] == $string) {
 | |
| 292 | + return 'geohash'; | |
| 293 | + } | |
| 294 | + | |
| 295 | + // What do you get when you cross an elephant with a rhino? | |
| 296 | + // http://youtu.be/RCBn5J83Poc | |
| 297 | + return FALSE; | |
| 298 | 298 | } | 
| 299 | 299 | |
| 300 | 300 | } | 
| @@ -91,7 +91,7 @@ discard block | ||
| 91 | 91 | } | 
| 92 | 92 | |
| 93 | 93 |    static function getAdapterMap() {
 | 
| 94 | - return array ( | |
| 94 | + return array( | |
| 95 | 95 | 'wkt' => 'WKT', | 
| 96 | 96 | 'ewkt' => 'EWKT', | 
| 97 | 97 | 'wkb' => 'WKB', | 
| @@ -154,8 +154,8 @@ discard block | ||
| 154 | 154 | |
| 155 | 155 | // If the geometry cannot even theoretically be reduced more, then pass it back | 
| 156 | 156 |      if (gettype($geometry) == 'object') {
 | 
| 157 | -      $passbacks = array('Point','LineString','Polygon');
 | |
| 158 | -      if (in_array($geometry->geometryType(),$passbacks)) {
 | |
| 157 | +      $passbacks = array('Point', 'LineString', 'Polygon');
 | |
| 158 | +      if (in_array($geometry->geometryType(), $passbacks)) {
 | |
| 159 | 159 | return $geometry; | 
| 160 | 160 | } | 
| 161 | 161 | } | 
| @@ -163,8 +163,8 @@ discard block | ||
| 163 | 163 | // If it is a mutlti-geometry, check to see if it just has one member | 
| 164 | 164 | // If it does, then pass the member, if not, then just pass back the geometry | 
| 165 | 165 |      if (gettype($geometry) == 'object') {
 | 
| 166 | -      $simple_collections = array('MultiPoint','MultiLineString','MultiPolygon');
 | |
| 167 | -      if (in_array(get_class($geometry),$passbacks)) {
 | |
| 166 | +      $simple_collections = array('MultiPoint', 'MultiLineString', 'MultiPolygon');
 | |
| 167 | +      if (in_array(get_class($geometry), $passbacks)) {
 | |
| 168 | 168 | $components = $geometry->getComponents(); | 
| 169 | 169 |          if (count($components) == 1) {
 | 
| 170 | 170 | return $components[0]; | 
| @@ -183,7 +183,7 @@ discard block | ||
| 183 | 183 | $geometries = array(); | 
| 184 | 184 | $geom_types = array(); | 
| 185 | 185 | |
| 186 | -    $collections = array('MultiPoint','MultiLineString','MultiPolygon','GeometryCollection');
 | |
| 186 | +    $collections = array('MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection');
 | |
| 187 | 187 | |
| 188 | 188 |      foreach ($geometry as $item) {
 | 
| 189 | 189 |        if ($item) {
 | 
| @@ -52,7 +52,9 @@ discard block | ||
| 52 | 52 |      if (!$type) {
 | 
| 53 | 53 | // If the user is trying to load a Geometry from a Geometry... Just pass it back | 
| 54 | 54 |        if (is_object($data)) {
 | 
| 55 | - if ($data instanceOf Geometry) return $data; | |
| 55 | +        if ($data instanceOf Geometry) { | |
| 56 | + return $data; | |
| 57 | + } | |
| 56 | 58 | } | 
| 57 | 59 | |
| 58 | 60 | $detected = geoPHP::detectFormat($data); | 
| @@ -120,7 +122,9 @@ discard block | ||
| 120 | 122 | |
| 121 | 123 |    static function geosInstalled($force = NULL) {
 | 
| 122 | 124 | static $geos_installed = NULL; | 
| 123 | - if ($force !== NULL) $geos_installed = $force; | |
| 125 | +    if ($force !== NULL) { | |
| 126 | + $geos_installed = $force; | |
| 127 | + } | |
| 124 | 128 |      if ($geos_installed !== NULL) {
 | 
| 125 | 129 | return $geos_installed; | 
| 126 | 130 | } | 
| @@ -148,8 +152,12 @@ discard block | ||
| 148 | 152 |    static function geometryReduce($geometry) {
 | 
| 149 | 153 | // If it's an array of one, then just parse the one | 
| 150 | 154 |      if (is_array($geometry)) {
 | 
| 151 | - if (empty($geometry)) return FALSE; | |
| 152 | - if (count($geometry) == 1) return geoPHP::geometryReduce(array_shift($geometry)); | |
| 155 | +      if (empty($geometry)) { | |
| 156 | + return FALSE; | |
| 157 | + } | |
| 158 | +      if (count($geometry) == 1) { | |
| 159 | + return geoPHP::geometryReduce(array_shift($geometry)); | |
| 160 | + } | |
| 153 | 161 | } | 
| 154 | 162 | |
| 155 | 163 | // If the geometry cannot even theoretically be reduced more, then pass it back | 
| @@ -168,8 +176,7 @@ discard block | ||
| 168 | 176 | $components = $geometry->getComponents(); | 
| 169 | 177 |          if (count($components) == 1) {
 | 
| 170 | 178 | return $components[0]; | 
| 171 | - } | |
| 172 | -        else {
 | |
| 179 | +        } else {
 | |
| 173 | 180 | return $geometry; | 
| 174 | 181 | } | 
| 175 | 182 | } | 
| @@ -192,8 +199,7 @@ discard block | ||
| 192 | 199 | $geometries[] = $component; | 
| 193 | 200 | $geom_types[] = $component->geometryType(); | 
| 194 | 201 | } | 
| 195 | - } | |
| 196 | -        else {
 | |
| 202 | +        } else {
 | |
| 197 | 203 | $geometries[] = $item; | 
| 198 | 204 | $geom_types[] = $item->geometryType(); | 
| 199 | 205 | } | 
| @@ -209,13 +215,11 @@ discard block | ||
| 209 | 215 |      if (count($geom_types) == 1) {
 | 
| 210 | 216 |        if (count($geometries) == 1) {
 | 
| 211 | 217 | return $geometries[0]; | 
| 212 | - } | |
| 213 | -      else {
 | |
| 218 | +      } else {
 | |
| 214 | 219 | $class = 'Multi'.$geom_types[0]; | 
| 215 | 220 | return new $class($geometries); | 
| 216 | 221 | } | 
| 217 | - } | |
| 218 | -    else {
 | |
| 222 | +    } else {
 | |
| 219 | 223 | return new GeometryCollection($geometries); | 
| 220 | 224 | } | 
| 221 | 225 | } | 
| @@ -230,7 +234,9 @@ discard block | ||
| 230 | 234 |      $bytes = unpack("c*", fread($mem, 11));
 | 
| 231 | 235 | |
| 232 | 236 | // If bytes is empty, then we were passed empty input | 
| 233 | - if (empty($bytes)) return FALSE; | |
| 237 | +    if (empty($bytes)) { | |
| 238 | + return FALSE; | |
| 239 | + } | |
| 234 | 240 | |
| 235 | 241 | // First char is a tab, space or carriage-return. trim it and try again | 
| 236 | 242 |      if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
 | 
| @@ -240,8 +246,11 @@ discard block | ||
| 240 | 246 | // Detect WKB or EWKB -- first byte is 1 (little endian indicator) | 
| 241 | 247 |      if ($bytes[1] == 1) {
 | 
| 242 | 248 | // If SRID byte is TRUE (1), it's EWKB | 
| 243 | - if ($bytes[5]) return 'ewkb'; | |
| 244 | - else return 'wkb'; | |
| 249 | +      if ($bytes[5]) { | |
| 250 | + return 'ewkb'; | |
| 251 | +      } else { | |
| 252 | + return 'wkb'; | |
| 253 | + } | |
| 245 | 254 | } | 
| 246 | 255 | |
| 247 | 256 | // Detect HEX encoded WKB or EWKB (PostGIS format) -- first byte is 48, second byte is 49 (hex '01' => first-byte = 1) | 
| @@ -274,12 +283,24 @@ discard block | ||
| 274 | 283 |      if ($bytes[1] == 60) {
 | 
| 275 | 284 | // grab the first 256 characters | 
| 276 | 285 | $string = substr($input, 0, 256); | 
| 277 | - if (strpos($string, '<kml') !== FALSE) return 'kml'; | |
| 278 | - if (strpos($string, '<coordinate') !== FALSE) return 'kml'; | |
| 279 | - if (strpos($string, '<gpx') !== FALSE) return 'gpx'; | |
| 280 | - if (strpos($string, '<georss') !== FALSE) return 'georss'; | |
| 281 | - if (strpos($string, '<rss') !== FALSE) return 'georss'; | |
| 282 | - if (strpos($string, '<feed') !== FALSE) return 'georss'; | |
| 286 | +      if (strpos($string, '<kml') !== FALSE) { | |
| 287 | + return 'kml'; | |
| 288 | + } | |
| 289 | +      if (strpos($string, '<coordinate') !== FALSE) { | |
| 290 | + return 'kml'; | |
| 291 | + } | |
| 292 | +      if (strpos($string, '<gpx') !== FALSE) { | |
| 293 | + return 'gpx'; | |
| 294 | + } | |
| 295 | +      if (strpos($string, '<georss') !== FALSE) { | |
| 296 | + return 'georss'; | |
| 297 | + } | |
| 298 | +      if (strpos($string, '<rss') !== FALSE) { | |
| 299 | + return 'georss'; | |
| 300 | + } | |
| 301 | +      if (strpos($string, '<feed') !== FALSE) { | |
| 302 | + return 'georss'; | |
| 303 | + } | |
| 283 | 304 | } | 
| 284 | 305 | |
| 285 | 306 | // We need an 8 byte string for geohash and unpacked WKB / WKT | 
| @@ -113,7 +113,6 @@ discard block | ||
| 113 | 113 | * Serializes an object into a geojson string | 
| 114 | 114 | * | 
| 115 | 115 | * | 
| 116 | - * @param Geometry $obj The object to serialize | |
| 117 | 116 | * | 
| 118 | 117 | * @return string The GeoJSON string | 
| 119 | 118 | */ | 
| @@ -126,6 +125,9 @@ discard block | ||
| 126 | 125 | } | 
| 127 | 126 | } | 
| 128 | 127 | |
| 128 | + /** | |
| 129 | + * @param Geometry $geometry | |
| 130 | + */ | |
| 129 | 131 |    public function getArray($geometry) { | 
| 130 | 132 |      if ($geometry->getGeomType() == 'GeometryCollection') { | 
| 131 | 133 | $component_array = array(); | 
| @@ -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 | |
| @@ -50,7 +50,7 @@ | ||
| 50 | 50 |      if ($type == 'GeometryCollection') { | 
| 51 | 51 | return $this->objToGeometryCollection($obj); | 
| 52 | 52 | } | 
| 53 | - $method = 'arrayTo' . $type; | |
| 53 | + $method = 'arrayTo'.$type; | |
| 54 | 54 | return $this->$method($obj->coordinates); | 
| 55 | 55 | } | 
| 56 | 56 | |
| @@ -120,8 +120,7 @@ discard block | ||
| 120 | 120 |    public function write(Geometry $geometry, $return_array = FALSE) { | 
| 121 | 121 |      if ($return_array) { | 
| 122 | 122 | return $this->getArray($geometry); | 
| 123 | - } | |
| 124 | -    else { | |
| 123 | +    } else { | |
| 125 | 124 | return json_encode($this->getArray($geometry)); | 
| 126 | 125 | } | 
| 127 | 126 | } | 
| @@ -139,11 +138,12 @@ discard block | ||
| 139 | 138 | 'type'=> 'GeometryCollection', | 
| 140 | 139 | 'geometries'=> $component_array, | 
| 141 | 140 | ); | 
| 142 | - } | |
| 143 | - else return array( | |
| 141 | +    } else { | |
| 142 | + return array( | |
| 144 | 143 | 'type'=> $geometry->getGeomType(), | 
| 145 | 144 | 'coordinates'=> $geometry->asArray(), | 
| 146 | 145 | ); | 
| 146 | + } | |
| 147 | 147 | } | 
| 148 | 148 | } | 
| 149 | 149 | |
| @@ -18,8 +18,6 @@ discard block | ||
| 18 | 18 | /** | 
| 19 | 19 | * Read GeoRSS string into geometry objects | 
| 20 | 20 | * | 
| 21 | - * @param string $georss - an XML feed containing geoRSS | |
| 22 | - * | |
| 23 | 21 | * @return Geometry|GeometryCollection | 
| 24 | 22 | */ | 
| 25 | 23 |    public function read($gpx) {
 | 
| @@ -80,6 +78,9 @@ discard block | ||
| 80 | 78 | return geoPHP::geometryReduce($geometries); | 
| 81 | 79 | } | 
| 82 | 80 | |
| 81 | + /** | |
| 82 | + * @param string $string | |
| 83 | + */ | |
| 83 | 84 |    protected function getPointsFromCoords($string) {
 | 
| 84 | 85 | $coords = array(); | 
| 85 | 86 |      $latlon = explode(' ',$string);
 | 
| @@ -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 | } | 
| @@ -44,21 +44,21 @@ discard block | ||
| 44 | 44 |    public function geomFromText($text) {
 | 
| 45 | 45 | // Change to lower-case, strip all CDATA, and de-namespace | 
| 46 | 46 | $text = strtolower($text); | 
| 47 | -    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
 | |
| 47 | +    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s', '', $text);
 | |
| 48 | 48 | |
| 49 | 49 | // Load into DOMDOcument | 
| 50 | 50 | $xmlobj = new DOMDocument(); | 
| 51 | 51 | @$xmlobj->loadXML($text); | 
| 52 | 52 |      if ($xmlobj === false) {
 | 
| 53 | -      throw new Exception("Invalid GeoRSS: ". $text);
 | |
| 53 | +      throw new Exception("Invalid GeoRSS: ".$text);
 | |
| 54 | 54 | } | 
| 55 | 55 | |
| 56 | 56 | $this->xmlobj = $xmlobj; | 
| 57 | 57 |      try {
 | 
| 58 | 58 | $geom = $this->geomFromXML(); | 
| 59 | -    } catch(InvalidText $e) {
 | |
| 60 | -        throw new Exception("Cannot Read Geometry From GeoRSS: ". $text);
 | |
| 61 | -    } catch(Exception $e) {
 | |
| 59 | +    } catch (InvalidText $e) {
 | |
| 60 | +        throw new Exception("Cannot Read Geometry From GeoRSS: ".$text);
 | |
| 61 | +    } catch (Exception $e) {
 | |
| 62 | 62 | throw $e; | 
| 63 | 63 | } | 
| 64 | 64 | |
| @@ -82,9 +82,9 @@ discard block | ||
| 82 | 82 | |
| 83 | 83 |    protected function getPointsFromCoords($string) {
 | 
| 84 | 84 | $coords = array(); | 
| 85 | -    $latlon = explode(' ',$string);
 | |
| 85 | +    $latlon = explode(' ', $string);
 | |
| 86 | 86 |      foreach ($latlon as $key => $item) {
 | 
| 87 | -      if (!($key % 2)) {
 | |
| 87 | +      if (!($key%2)) {
 | |
| 88 | 88 | // It's a latitude | 
| 89 | 89 | $lat = $item; | 
| 90 | 90 | } | 
| @@ -139,7 +139,7 @@ discard block | ||
| 139 | 139 | $polygons = array(); | 
| 140 | 140 |      $box_elements = $this->xmlobj->getElementsByTagName('box');
 | 
| 141 | 141 |      foreach ($box_elements as $box) {
 | 
| 142 | -      $parts = explode(' ',trim($box->firstChild->nodeValue));
 | |
| 142 | +      $parts = explode(' ', trim($box->firstChild->nodeValue));
 | |
| 143 | 143 | $components = array( | 
| 144 | 144 | new Point($parts[3], $parts[2]), | 
| 145 | 145 | new Point($parts[3], $parts[0]), | 
| @@ -159,7 +159,7 @@ discard block | ||
| 159 | 159 | $points = array(); | 
| 160 | 160 |      $circle_elements = $this->xmlobj->getElementsByTagName('circle');
 | 
| 161 | 161 |      foreach ($circle_elements as $circle) {
 | 
| 162 | -      $parts = explode(' ',trim($circle->firstChild->nodeValue));
 | |
| 162 | +      $parts = explode(' ', trim($circle->firstChild->nodeValue));
 | |
| 163 | 163 | $points[] = new Point($parts[1], $parts[0]); | 
| 164 | 164 | } | 
| 165 | 165 | return $points; | 
| @@ -196,7 +196,7 @@ discard block | ||
| 196 | 196 | $output = '<'.$this->nss.'line>'; | 
| 197 | 197 |      foreach ($geom->getComponents() as $k => $point) {
 | 
| 198 | 198 | $output .= $point->getY().' '.$point->getX(); | 
| 199 | - if ($k < ($geom->numGeometries() -1)) $output .= ' '; | |
| 199 | + if ($k < ($geom->numGeometries() - 1)) $output .= ' '; | |
| 200 | 200 | } | 
| 201 | 201 | $output .= '</'.$this->nss.'line>'; | 
| 202 | 202 | return $output; | 
| @@ -207,7 +207,7 @@ discard block | ||
| 207 | 207 | $exterior_ring = $geom->exteriorRing(); | 
| 208 | 208 |      foreach ($exterior_ring->getComponents() as $k => $point) {
 | 
| 209 | 209 | $output .= $point->getY().' '.$point->getX(); | 
| 210 | - if ($k < ($exterior_ring->numGeometries() -1)) $output .= ' '; | |
| 210 | + if ($k < ($exterior_ring->numGeometries() - 1)) $output .= ' '; | |
| 211 | 211 | } | 
| 212 | 212 | $output .= '</'.$this->nss.'polygon>'; | 
| 213 | 213 | return $output; | 
| @@ -87,8 +87,7 @@ discard block | ||
| 87 | 87 |        if (!($key % 2)) {
 | 
| 88 | 88 | // It's a latitude | 
| 89 | 89 | $lat = $item; | 
| 90 | - } | |
| 91 | -      else {
 | |
| 90 | +      } else {
 | |
| 92 | 91 | // It's a longitude | 
| 93 | 92 | $lon = $item; | 
| 94 | 93 | $coords[] = new Point($lon, $lat); | 
| @@ -125,8 +124,7 @@ discard block | ||
| 125 | 124 | $points = $this->getPointsFromCoords(trim($poly->firstChild->nodeValue)); | 
| 126 | 125 | $exterior_ring = new LineString($points); | 
| 127 | 126 | $polygons[] = new Polygon(array($exterior_ring)); | 
| 128 | - } | |
| 129 | -      else {
 | |
| 127 | +      } else {
 | |
| 130 | 128 | // It's an EMPTY polygon | 
| 131 | 129 | $polygons[] = new Polygon(); | 
| 132 | 130 | } | 
| @@ -196,7 +194,9 @@ discard block | ||
| 196 | 194 | $output = '<'.$this->nss.'line>'; | 
| 197 | 195 |      foreach ($geom->getComponents() as $k => $point) {
 | 
| 198 | 196 | $output .= $point->getY().' '.$point->getX(); | 
| 199 | - if ($k < ($geom->numGeometries() -1)) $output .= ' '; | |
| 197 | +      if ($k < ($geom->numGeometries() -1)) { | |
| 198 | + $output .= ' '; | |
| 199 | + } | |
| 200 | 200 | } | 
| 201 | 201 | $output .= '</'.$this->nss.'line>'; | 
| 202 | 202 | return $output; | 
| @@ -207,7 +207,9 @@ discard block | ||
| 207 | 207 | $exterior_ring = $geom->exteriorRing(); | 
| 208 | 208 |      foreach ($exterior_ring->getComponents() as $k => $point) {
 | 
| 209 | 209 | $output .= $point->getY().' '.$point->getX(); | 
| 210 | - if ($k < ($exterior_ring->numGeometries() -1)) $output .= ' '; | |
| 210 | +      if ($k < ($exterior_ring->numGeometries() -1)) { | |
| 211 | + $output .= ' '; | |
| 212 | + } | |
| 211 | 213 | } | 
| 212 | 214 | $output .= '</'.$this->nss.'polygon>'; | 
| 213 | 215 | return $output; | 
| @@ -42,6 +42,9 @@ | ||
| 42 | 42 | return '<'.$this->nss.'gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'</'.$this->nss.'gpx>'; | 
| 43 | 43 | } | 
| 44 | 44 | |
| 45 | + /** | |
| 46 | + * @param string $text | |
| 47 | + */ | |
| 45 | 48 |    public function geomFromText($text) {
 | 
| 46 | 49 | // Change to lower-case and strip all CDATA | 
| 47 | 50 | $text = strtolower($text); | 
| @@ -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 | } | 
| @@ -45,21 +45,21 @@ discard block | ||
| 45 | 45 |    public function geomFromText($text) {
 | 
| 46 | 46 | // Change to lower-case and strip all CDATA | 
| 47 | 47 | $text = strtolower($text); | 
| 48 | -    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
 | |
| 48 | +    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s', '', $text);
 | |
| 49 | 49 | |
| 50 | 50 | // Load into DOMDocument | 
| 51 | 51 | $xmlobj = new DOMDocument(); | 
| 52 | 52 | @$xmlobj->loadXML($text); | 
| 53 | 53 |      if ($xmlobj === false) {
 | 
| 54 | -      throw new Exception("Invalid GPX: ". $text);
 | |
| 54 | +      throw new Exception("Invalid GPX: ".$text);
 | |
| 55 | 55 | } | 
| 56 | 56 | |
| 57 | 57 | $this->xmlobj = $xmlobj; | 
| 58 | 58 |      try {
 | 
| 59 | 59 | $geom = $this->geomFromXML(); | 
| 60 | -    } catch(InvalidText $e) {
 | |
| 61 | -        throw new Exception("Cannot Read Geometry From GPX: ". $text);
 | |
| 62 | -    } catch(Exception $e) {
 | |
| 60 | +    } catch (InvalidText $e) {
 | |
| 61 | +        throw new Exception("Cannot Read Geometry From GPX: ".$text);
 | |
| 62 | +    } catch (Exception $e) {
 | |
| 63 | 63 | throw $e; | 
| 64 | 64 | } | 
| 65 | 65 | |
| @@ -112,7 +112,7 @@ discard block | ||
| 112 | 112 | $components[] = new Point($lon, $lat); | 
| 113 | 113 | } | 
| 114 | 114 | } | 
| 115 | -      if ($components) {$lines[] = new LineString($components);}
 | |
| 115 | +      if ($components) {$lines[] = new LineString($components); }
 | |
| 116 | 116 | } | 
| 117 | 117 | return $lines; | 
| 118 | 118 | } | 
| @@ -34,7 +34,9 @@ | ||
| 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; | |
| 37 | +    if ($geometry->isEmpty()) { | |
| 38 | + return NULL; | |
| 39 | + } | |
| 38 | 40 |      if ($namespace) {
 | 
| 39 | 41 | $this->namespace = $namespace; | 
| 40 | 42 | $this->nss = $namespace.':'; | 
| @@ -49,6 +49,9 @@ discard block | ||
| 49 | 49 | return $this->geometryToKML($geometry); | 
| 50 | 50 | } | 
| 51 | 51 | |
| 52 | + /** | |
| 53 | + * @param string $text | |
| 54 | + */ | |
| 52 | 55 |    public function geomFromText($text) {
 | 
| 53 | 56 | |
| 54 | 57 | // Change to lower-case and strip all CDATA | 
| @@ -184,6 +187,9 @@ discard block | ||
| 184 | 187 | return $coordinates; | 
| 185 | 188 | } | 
| 186 | 189 | |
| 190 | + /** | |
| 191 | + * @param Geometry $geom | |
| 192 | + */ | |
| 187 | 193 |    private function geometryToKML($geom) {
 | 
| 188 | 194 | $type = strtolower($geom->getGeomType()); | 
| 189 | 195 |      switch ($type) {
 | 
| @@ -31,7 +31,7 @@ discard block | ||
| 31 | 31 | * @return Geometry|GeometryCollection | 
| 32 | 32 | */ | 
| 33 | 33 |    public function read($kml) {
 | 
| 34 | - return $this->geomFromText($kml); | |
| 34 | + return $this->geomFromText($kml); | |
| 35 | 35 | } | 
| 36 | 36 | |
| 37 | 37 | /** | 
| @@ -42,218 +42,218 @@ discard block | ||
| 42 | 42 | * @return string The KML string representation of the input geometries | 
| 43 | 43 | */ | 
| 44 | 44 |    public function write(Geometry $geometry, $namespace = FALSE) {
 | 
| 45 | -    if ($namespace) {
 | |
| 46 | - $this->namespace = $namespace; | |
| 47 | - $this->nss = $namespace.':'; | |
| 48 | - } | |
| 49 | - return $this->geometryToKML($geometry); | |
| 45 | +	if ($namespace) {
 | |
| 46 | + $this->namespace = $namespace; | |
| 47 | + $this->nss = $namespace.':'; | |
| 48 | + } | |
| 49 | + return $this->geometryToKML($geometry); | |
| 50 | 50 | } | 
| 51 | 51 | |
| 52 | 52 |    public function geomFromText($text) {
 | 
| 53 | 53 | |
| 54 | - // Change to lower-case and strip all CDATA | |
| 55 | - $text = mb_strtolower($text, mb_detect_encoding($text)); | |
| 56 | -    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
 | |
| 57 | - | |
| 58 | - // Load into DOMDOcument | |
| 59 | - $xmlobj = new DOMDocument(); | |
| 60 | - @$xmlobj->loadXML($text); | |
| 61 | -    if ($xmlobj === false) {
 | |
| 62 | -      throw new Exception("Invalid KML: ". $text);
 | |
| 63 | - } | |
| 64 | - | |
| 65 | - $this->xmlobj = $xmlobj; | |
| 66 | -    try {
 | |
| 67 | - $geom = $this->geomFromXML(); | |
| 68 | -    } catch(InvalidText $e) {
 | |
| 69 | -        throw new Exception("Cannot Read Geometry From KML: ". $text);
 | |
| 70 | -    } catch(Exception $e) {
 | |
| 71 | - throw $e; | |
| 72 | - } | |
| 73 | - | |
| 74 | - return $geom; | |
| 54 | + // Change to lower-case and strip all CDATA | |
| 55 | + $text = mb_strtolower($text, mb_detect_encoding($text)); | |
| 56 | +	$text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
 | |
| 57 | + | |
| 58 | + // Load into DOMDOcument | |
| 59 | + $xmlobj = new DOMDocument(); | |
| 60 | + @$xmlobj->loadXML($text); | |
| 61 | +	if ($xmlobj === false) {
 | |
| 62 | +	  throw new Exception("Invalid KML: ". $text);
 | |
| 63 | + } | |
| 64 | + | |
| 65 | + $this->xmlobj = $xmlobj; | |
| 66 | +	try {
 | |
| 67 | + $geom = $this->geomFromXML(); | |
| 68 | +	} catch(InvalidText $e) {
 | |
| 69 | +		throw new Exception("Cannot Read Geometry From KML: ". $text);
 | |
| 70 | +	} catch(Exception $e) {
 | |
| 71 | + throw $e; | |
| 72 | + } | |
| 73 | + | |
| 74 | + return $geom; | |
| 75 | 75 | } | 
| 76 | 76 | |
| 77 | 77 |    protected function geomFromXML() {
 | 
| 78 | - $geometries = array(); | |
| 79 | - $geom_types = geoPHP::geometryList(); | |
| 80 | -    $placemark_elements = $this->xmlobj->getElementsByTagName('placemark');
 | |
| 81 | -    if ($placemark_elements->length) {
 | |
| 82 | -      foreach ($placemark_elements as $placemark) {
 | |
| 83 | -        foreach ($placemark->childNodes as $child) {
 | |
| 84 | - // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection | |
| 85 | - $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; | |
| 86 | -          if (array_key_exists($node_name, $geom_types)) {
 | |
| 87 | - $function = 'parse'.$geom_types[$node_name]; | |
| 88 | - $geometries[] = $this->$function($child); | |
| 89 | - } | |
| 90 | - } | |
| 91 | - } | |
| 92 | - } | |
| 93 | -    else {
 | |
| 94 | - // The document does not have a placemark, try to create a valid geometry from the root element | |
| 95 | - $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; | |
| 96 | -      if (array_key_exists($node_name, $geom_types)) {
 | |
| 97 | - $function = 'parse'.$geom_types[$node_name]; | |
| 98 | - $geometries[] = $this->$function($this->xmlobj->documentElement); | |
| 99 | - } | |
| 100 | - } | |
| 101 | - return geoPHP::geometryReduce($geometries); | |
| 78 | + $geometries = array(); | |
| 79 | + $geom_types = geoPHP::geometryList(); | |
| 80 | +	$placemark_elements = $this->xmlobj->getElementsByTagName('placemark');
 | |
| 81 | +	if ($placemark_elements->length) {
 | |
| 82 | +	  foreach ($placemark_elements as $placemark) {
 | |
| 83 | +		foreach ($placemark->childNodes as $child) {
 | |
| 84 | + // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection | |
| 85 | + $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; | |
| 86 | +		  if (array_key_exists($node_name, $geom_types)) {
 | |
| 87 | + $function = 'parse'.$geom_types[$node_name]; | |
| 88 | + $geometries[] = $this->$function($child); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 93 | +	else {
 | |
| 94 | + // The document does not have a placemark, try to create a valid geometry from the root element | |
| 95 | + $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; | |
| 96 | +	  if (array_key_exists($node_name, $geom_types)) {
 | |
| 97 | + $function = 'parse'.$geom_types[$node_name]; | |
| 98 | + $geometries[] = $this->$function($this->xmlobj->documentElement); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + return geoPHP::geometryReduce($geometries); | |
| 102 | 102 | } | 
| 103 | 103 | |
| 104 | 104 |    protected function childElements($xml, $nodename = '') {
 | 
| 105 | - $children = array(); | |
| 106 | -    if ($xml->childNodes) {
 | |
| 107 | -      foreach ($xml->childNodes as $child) {
 | |
| 108 | -        if ($child->nodeName == $nodename) {
 | |
| 109 | - $children[] = $child; | |
| 110 | - } | |
| 111 | - } | |
| 112 | - } | |
| 113 | - return $children; | |
| 105 | + $children = array(); | |
| 106 | +	if ($xml->childNodes) {
 | |
| 107 | +	  foreach ($xml->childNodes as $child) {
 | |
| 108 | +		if ($child->nodeName == $nodename) {
 | |
| 109 | + $children[] = $child; | |
| 110 | + } | |
| 111 | + } | |
| 112 | + } | |
| 113 | + return $children; | |
| 114 | 114 | } | 
| 115 | 115 | |
| 116 | 116 |    protected function parsePoint($xml) {
 | 
| 117 | - $coordinates = $this->_extractCoordinates($xml); | |
| 118 | - return new Point($coordinates[0][0],$coordinates[0][1]); | |
| 117 | + $coordinates = $this->_extractCoordinates($xml); | |
| 118 | + return new Point($coordinates[0][0],$coordinates[0][1]); | |
| 119 | 119 | } | 
| 120 | 120 | |
| 121 | 121 |    protected function parseLineString($xml) {
 | 
| 122 | - $coordinates = $this->_extractCoordinates($xml); | |
| 123 | - $point_array = array(); | |
| 124 | -    foreach ($coordinates as $set) {
 | |
| 125 | - $point_array[] = new Point($set[0],$set[1]); | |
| 126 | - } | |
| 127 | - return new LineString($point_array); | |
| 122 | + $coordinates = $this->_extractCoordinates($xml); | |
| 123 | + $point_array = array(); | |
| 124 | +	foreach ($coordinates as $set) {
 | |
| 125 | + $point_array[] = new Point($set[0],$set[1]); | |
| 126 | + } | |
| 127 | + return new LineString($point_array); | |
| 128 | 128 | } | 
| 129 | 129 | |
| 130 | 130 |    protected function parsePolygon($xml) {
 | 
| 131 | - $components = array(); | |
| 132 | - | |
| 133 | - $outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis'); | |
| 134 | - $outer_boundary_element = $outer_boundary_element_a[0]; | |
| 135 | - $outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring'); | |
| 136 | - $outer_ring_element = $outer_ring_element_a[0]; | |
| 137 | - $components[] = $this->parseLineString($outer_ring_element); | |
| 138 | - | |
| 139 | -    if (count($components) != 1) {
 | |
| 140 | -      throw new Exception("Invalid KML");
 | |
| 141 | - } | |
| 142 | - | |
| 143 | - $inner_boundary_element_a = $this->childElements($xml, 'innerboundaryis'); | |
| 144 | -    if (count($inner_boundary_element_a)) {
 | |
| 145 | -      foreach ($inner_boundary_element_a as $inner_boundary_element) {
 | |
| 146 | -        foreach ($this->childElements($inner_boundary_element, 'linearring') as $inner_ring_element) {
 | |
| 147 | - $components[] = $this->parseLineString($inner_ring_element); | |
| 148 | - } | |
| 149 | - } | |
| 150 | - } | |
| 151 | - | |
| 152 | - return new Polygon($components); | |
| 131 | + $components = array(); | |
| 132 | + | |
| 133 | + $outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis'); | |
| 134 | + $outer_boundary_element = $outer_boundary_element_a[0]; | |
| 135 | + $outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring'); | |
| 136 | + $outer_ring_element = $outer_ring_element_a[0]; | |
| 137 | + $components[] = $this->parseLineString($outer_ring_element); | |
| 138 | + | |
| 139 | +	if (count($components) != 1) {
 | |
| 140 | +	  throw new Exception("Invalid KML");
 | |
| 141 | + } | |
| 142 | + | |
| 143 | + $inner_boundary_element_a = $this->childElements($xml, 'innerboundaryis'); | |
| 144 | +	if (count($inner_boundary_element_a)) {
 | |
| 145 | +	  foreach ($inner_boundary_element_a as $inner_boundary_element) {
 | |
| 146 | +		foreach ($this->childElements($inner_boundary_element, 'linearring') as $inner_ring_element) {
 | |
| 147 | + $components[] = $this->parseLineString($inner_ring_element); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + return new Polygon($components); | |
| 153 | 153 | } | 
| 154 | 154 | |
| 155 | 155 |    protected function parseGeometryCollection($xml) {
 | 
| 156 | - $components = array(); | |
| 157 | - $geom_types = geoPHP::geometryList(); | |
| 158 | -    foreach ($xml->childNodes as $child) {
 | |
| 159 | - $nodeName = ($child->nodeName == 'linearring') ? 'linestring' : $child->nodeName; | |
| 160 | -      if (array_key_exists($nodeName, $geom_types)) {
 | |
| 161 | - $function = 'parse'.$geom_types[$nodeName]; | |
| 162 | - $components[] = $this->$function($child); | |
| 163 | - } | |
| 164 | - } | |
| 165 | - return new GeometryCollection($components); | |
| 156 | + $components = array(); | |
| 157 | + $geom_types = geoPHP::geometryList(); | |
| 158 | +	foreach ($xml->childNodes as $child) {
 | |
| 159 | + $nodeName = ($child->nodeName == 'linearring') ? 'linestring' : $child->nodeName; | |
| 160 | +	  if (array_key_exists($nodeName, $geom_types)) {
 | |
| 161 | + $function = 'parse'.$geom_types[$nodeName]; | |
| 162 | + $components[] = $this->$function($child); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + return new GeometryCollection($components); | |
| 166 | 166 | } | 
| 167 | 167 | |
| 168 | 168 |    protected function _extractCoordinates($xml) {
 | 
| 169 | - $coord_elements = $this->childElements($xml, 'coordinates'); | |
| 170 | - $coordinates = array(); | |
| 171 | -    if (count($coord_elements)) {
 | |
| 172 | -      $coord_sets = explode(' ', preg_replace('/[\r\n]+/', ' ', $coord_elements[0]->nodeValue));
 | |
| 173 | -      foreach ($coord_sets as $set_string) {
 | |
| 174 | - $set_string = trim($set_string); | |
| 175 | -        if ($set_string) {
 | |
| 176 | -          $set_array = explode(',',$set_string);
 | |
| 177 | -          if (count($set_array) >= 2) {
 | |
| 178 | - $coordinates[] = $set_array; | |
| 179 | - } | |
| 180 | - } | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - return $coordinates; | |
| 169 | + $coord_elements = $this->childElements($xml, 'coordinates'); | |
| 170 | + $coordinates = array(); | |
| 171 | +	if (count($coord_elements)) {
 | |
| 172 | +	  $coord_sets = explode(' ', preg_replace('/[\r\n]+/', ' ', $coord_elements[0]->nodeValue));
 | |
| 173 | +	  foreach ($coord_sets as $set_string) {
 | |
| 174 | + $set_string = trim($set_string); | |
| 175 | +		if ($set_string) {
 | |
| 176 | +		  $set_array = explode(',',$set_string);
 | |
| 177 | +		  if (count($set_array) >= 2) {
 | |
| 178 | + $coordinates[] = $set_array; | |
| 179 | + } | |
| 180 | + } | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + return $coordinates; | |
| 185 | 185 | } | 
| 186 | 186 | |
| 187 | 187 |    private function geometryToKML($geom) {
 | 
| 188 | - $type = strtolower($geom->getGeomType()); | |
| 189 | -    switch ($type) {
 | |
| 190 | - case 'point': | |
| 191 | - return $this->pointToKML($geom); | |
| 192 | - break; | |
| 193 | - case 'linestring': | |
| 194 | - return $this->linestringToKML($geom); | |
| 195 | - break; | |
| 196 | - case 'polygon': | |
| 197 | - return $this->polygonToKML($geom); | |
| 198 | - break; | |
| 199 | - case 'multipoint': | |
| 200 | - case 'multilinestring': | |
| 201 | - case 'multipolygon': | |
| 202 | - case 'geometrycollection': | |
| 203 | - return $this->collectionToKML($geom); | |
| 204 | - break; | |
| 205 | - } | |
| 188 | + $type = strtolower($geom->getGeomType()); | |
| 189 | +	switch ($type) {
 | |
| 190 | + case 'point': | |
| 191 | + return $this->pointToKML($geom); | |
| 192 | + break; | |
| 193 | + case 'linestring': | |
| 194 | + return $this->linestringToKML($geom); | |
| 195 | + break; | |
| 196 | + case 'polygon': | |
| 197 | + return $this->polygonToKML($geom); | |
| 198 | + break; | |
| 199 | + case 'multipoint': | |
| 200 | + case 'multilinestring': | |
| 201 | + case 'multipolygon': | |
| 202 | + case 'geometrycollection': | |
| 203 | + return $this->collectionToKML($geom); | |
| 204 | + break; | |
| 205 | + } | |
| 206 | 206 | } | 
| 207 | 207 | |
| 208 | 208 |    private function pointToKML($geom) {
 | 
| 209 | - return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>'; | |
| 209 | + return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>'; | |
| 210 | 210 | } | 
| 211 | 211 | |
| 212 | 212 |    private function linestringToKML($geom, $type = FALSE) {
 | 
| 213 | -    if (!$type) {
 | |
| 214 | - $type = $geom->getGeomType(); | |
| 215 | - } | |
| 213 | +	if (!$type) {
 | |
| 214 | + $type = $geom->getGeomType(); | |
| 215 | + } | |
| 216 | 216 | |
| 217 | - $str = '<'.$this->nss . $type .'>'; | |
| 217 | + $str = '<'.$this->nss . $type .'>'; | |
| 218 | 218 | |
| 219 | -    if (!$geom->isEmpty()) {
 | |
| 220 | - $str .= '<'.$this->nss.'coordinates>'; | |
| 221 | - $i=0; | |
| 222 | -      foreach ($geom->getComponents() as $comp) {
 | |
| 223 | - if ($i != 0) $str .= ' '; | |
| 224 | - $str .= $comp->getX() .','. $comp->getY(); | |
| 225 | - $i++; | |
| 226 | - } | |
| 219 | +	if (!$geom->isEmpty()) {
 | |
| 220 | + $str .= '<'.$this->nss.'coordinates>'; | |
| 221 | + $i=0; | |
| 222 | +	  foreach ($geom->getComponents() as $comp) {
 | |
| 223 | + if ($i != 0) $str .= ' '; | |
| 224 | + $str .= $comp->getX() .','. $comp->getY(); | |
| 225 | + $i++; | |
| 226 | + } | |
| 227 | 227 | |
| 228 | - $str .= '</'.$this->nss.'coordinates>'; | |
| 229 | - } | |
| 228 | + $str .= '</'.$this->nss.'coordinates>'; | |
| 229 | + } | |
| 230 | 230 | |
| 231 | - $str .= '</'. $this->nss . $type .'>'; | |
| 231 | + $str .= '</'. $this->nss . $type .'>'; | |
| 232 | 232 | |
| 233 | - return $str; | |
| 233 | + return $str; | |
| 234 | 234 | } | 
| 235 | 235 | |
| 236 | 236 |    public function polygonToKML($geom) {
 | 
| 237 | - $components = $geom->getComponents(); | |
| 238 | -    if (!empty($components)) {
 | |
| 239 | - $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; | |
| 240 | -      foreach (array_slice($components, 1) as $comp) {
 | |
| 241 | - $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; | |
| 242 | - } | |
| 243 | - } | |
| 244 | - | |
| 245 | - return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; | |
| 237 | + $components = $geom->getComponents(); | |
| 238 | +	if (!empty($components)) {
 | |
| 239 | + $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; | |
| 240 | +	  foreach (array_slice($components, 1) as $comp) {
 | |
| 241 | + $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 245 | + return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; | |
| 246 | 246 | } | 
| 247 | 247 | |
| 248 | 248 |    public function collectionToKML($geom) {
 | 
| 249 | - $components = $geom->getComponents(); | |
| 250 | - $str = '<'.$this->nss.'MultiGeometry>'; | |
| 251 | -    foreach ($geom->getComponents() as $comp) {
 | |
| 252 | - $sub_adapter = new KML(); | |
| 253 | - $str .= $sub_adapter->write($comp); | |
| 254 | - } | |
| 255 | - | |
| 256 | - return $str .'</'.$this->nss.'MultiGeometry>'; | |
| 249 | + $components = $geom->getComponents(); | |
| 250 | + $str = '<'.$this->nss.'MultiGeometry>'; | |
| 251 | +	foreach ($geom->getComponents() as $comp) {
 | |
| 252 | + $sub_adapter = new KML(); | |
| 253 | + $str .= $sub_adapter->write($comp); | |
| 254 | + } | |
| 255 | + | |
| 256 | + return $str .'</'.$this->nss.'MultiGeometry>'; | |
| 257 | 257 | } | 
| 258 | 258 | |
| 259 | 259 | } | 
| @@ -53,21 +53,21 @@ discard block | ||
| 53 | 53 | |
| 54 | 54 | // Change to lower-case and strip all CDATA | 
| 55 | 55 | $text = mb_strtolower($text, mb_detect_encoding($text)); | 
| 56 | -    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
 | |
| 56 | +    $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s', '', $text);
 | |
| 57 | 57 | |
| 58 | 58 | // Load into DOMDOcument | 
| 59 | 59 | $xmlobj = new DOMDocument(); | 
| 60 | 60 | @$xmlobj->loadXML($text); | 
| 61 | 61 |      if ($xmlobj === false) {
 | 
| 62 | -      throw new Exception("Invalid KML: ". $text);
 | |
| 62 | +      throw new Exception("Invalid KML: ".$text);
 | |
| 63 | 63 | } | 
| 64 | 64 | |
| 65 | 65 | $this->xmlobj = $xmlobj; | 
| 66 | 66 |      try {
 | 
| 67 | 67 | $geom = $this->geomFromXML(); | 
| 68 | -    } catch(InvalidText $e) {
 | |
| 69 | -        throw new Exception("Cannot Read Geometry From KML: ". $text);
 | |
| 70 | -    } catch(Exception $e) {
 | |
| 68 | +    } catch (InvalidText $e) {
 | |
| 69 | +        throw new Exception("Cannot Read Geometry From KML: ".$text);
 | |
| 70 | +    } catch (Exception $e) {
 | |
| 71 | 71 | throw $e; | 
| 72 | 72 | } | 
| 73 | 73 | |
| @@ -115,14 +115,14 @@ discard block | ||
| 115 | 115 | |
| 116 | 116 |    protected function parsePoint($xml) {
 | 
| 117 | 117 | $coordinates = $this->_extractCoordinates($xml); | 
| 118 | - return new Point($coordinates[0][0],$coordinates[0][1]); | |
| 118 | + return new Point($coordinates[0][0], $coordinates[0][1]); | |
| 119 | 119 | } | 
| 120 | 120 | |
| 121 | 121 |    protected function parseLineString($xml) {
 | 
| 122 | 122 | $coordinates = $this->_extractCoordinates($xml); | 
| 123 | 123 | $point_array = array(); | 
| 124 | 124 |      foreach ($coordinates as $set) {
 | 
| 125 | - $point_array[] = new Point($set[0],$set[1]); | |
| 125 | + $point_array[] = new Point($set[0], $set[1]); | |
| 126 | 126 | } | 
| 127 | 127 | return new LineString($point_array); | 
| 128 | 128 | } | 
| @@ -173,7 +173,7 @@ discard block | ||
| 173 | 173 |        foreach ($coord_sets as $set_string) {
 | 
| 174 | 174 | $set_string = trim($set_string); | 
| 175 | 175 |          if ($set_string) {
 | 
| 176 | -          $set_array = explode(',',$set_string);
 | |
| 176 | +          $set_array = explode(',', $set_string);
 | |
| 177 | 177 |            if (count($set_array) >= 2) {
 | 
| 178 | 178 | $coordinates[] = $set_array; | 
| 179 | 179 | } | 
| @@ -214,21 +214,21 @@ discard block | ||
| 214 | 214 | $type = $geom->getGeomType(); | 
| 215 | 215 | } | 
| 216 | 216 | |
| 217 | - $str = '<'.$this->nss . $type .'>'; | |
| 217 | + $str = '<'.$this->nss.$type.'>'; | |
| 218 | 218 | |
| 219 | 219 |      if (!$geom->isEmpty()) {
 | 
| 220 | 220 | $str .= '<'.$this->nss.'coordinates>'; | 
| 221 | - $i=0; | |
| 221 | + $i = 0; | |
| 222 | 222 |        foreach ($geom->getComponents() as $comp) {
 | 
| 223 | 223 | if ($i != 0) $str .= ' '; | 
| 224 | - $str .= $comp->getX() .','. $comp->getY(); | |
| 224 | + $str .= $comp->getX().','.$comp->getY(); | |
| 225 | 225 | $i++; | 
| 226 | 226 | } | 
| 227 | 227 | |
| 228 | 228 | $str .= '</'.$this->nss.'coordinates>'; | 
| 229 | 229 | } | 
| 230 | 230 | |
| 231 | - $str .= '</'. $this->nss . $type .'>'; | |
| 231 | + $str .= '</'.$this->nss.$type.'>'; | |
| 232 | 232 | |
| 233 | 233 | return $str; | 
| 234 | 234 | } | 
| @@ -236,13 +236,13 @@ discard block | ||
| 236 | 236 |    public function polygonToKML($geom) {
 | 
| 237 | 237 | $components = $geom->getComponents(); | 
| 238 | 238 |      if (!empty($components)) {
 | 
| 239 | - $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; | |
| 239 | + $str = '<'.$this->nss.'outerBoundaryIs>'.$this->linestringToKML($components[0], 'LinearRing').'</'.$this->nss.'outerBoundaryIs>'; | |
| 240 | 240 |        foreach (array_slice($components, 1) as $comp) {
 | 
| 241 | - $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; | |
| 241 | + $str .= '<'.$this->nss.'innerBoundaryIs>'.$this->linestringToKML($comp).'</'.$this->nss.'innerBoundaryIs>'; | |
| 242 | 242 | } | 
| 243 | 243 | } | 
| 244 | 244 | |
| 245 | - return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; | |
| 245 | + return '<'.$this->nss.'Polygon>'.$str.'</'.$this->nss.'Polygon>'; | |
| 246 | 246 | } | 
| 247 | 247 | |
| 248 | 248 |    public function collectionToKML($geom) {
 | 
| @@ -253,7 +253,7 @@ discard block | ||
| 253 | 253 | $str .= $sub_adapter->write($comp); | 
| 254 | 254 | } | 
| 255 | 255 | |
| 256 | - return $str .'</'.$this->nss.'MultiGeometry>'; | |
| 256 | + return $str.'</'.$this->nss.'MultiGeometry>'; | |
| 257 | 257 | } | 
| 258 | 258 | |
| 259 | 259 | } | 
| @@ -89,8 +89,7 @@ discard block | ||
| 89 | 89 | } | 
| 90 | 90 | } | 
| 91 | 91 | } | 
| 92 | - } | |
| 93 | -    else {
 | |
| 92 | +    } else {
 | |
| 94 | 93 | // The document does not have a placemark, try to create a valid geometry from the root element | 
| 95 | 94 | $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; | 
| 96 | 95 |        if (array_key_exists($node_name, $geom_types)) {
 | 
| @@ -220,7 +219,9 @@ discard block | ||
| 220 | 219 | $str .= '<'.$this->nss.'coordinates>'; | 
| 221 | 220 | $i=0; | 
| 222 | 221 |        foreach ($geom->getComponents() as $comp) {
 | 
| 223 | - if ($i != 0) $str .= ' '; | |
| 222 | +        if ($i != 0) { | |
| 223 | + $str .= ' '; | |
| 224 | + } | |
| 224 | 225 | $str .= $comp->getX() .','. $comp->getY(); | 
| 225 | 226 | $i++; | 
| 226 | 227 | } | 
| @@ -123,6 +123,9 @@ discard block | ||
| 123 | 123 | return new Polygon($components); | 
| 124 | 124 | } | 
| 125 | 125 | |
| 126 | + /** | |
| 127 | + * @param string $type | |
| 128 | + */ | |
| 126 | 129 |    function getMulti(&$mem, $type) {
 | 
| 127 | 130 | // Get the number of items expected in this multi out of the first 4 bytes | 
| 128 | 131 |      $multi_length = unpack('L',fread($mem,4));
 | 
| @@ -196,6 +199,9 @@ discard block | ||
| 196 | 199 | } | 
| 197 | 200 | } | 
| 198 | 201 | |
| 202 | + /** | |
| 203 | + * @param Geometry $point | |
| 204 | + */ | |
| 199 | 205 |    function writePoint($point) {
 | 
| 200 | 206 | // Set the coords | 
| 201 | 207 |      $wkb = pack('dd',$point->x(), $point->y());
 | 
| @@ -215,6 +221,9 @@ discard block | ||
| 215 | 221 | return $wkb; | 
| 216 | 222 | } | 
| 217 | 223 | |
| 224 | + /** | |
| 225 | + * @param Geometry $poly | |
| 226 | + */ | |
| 218 | 227 |    function writePolygon($poly) {
 | 
| 219 | 228 | // Set the number of lines in this poly | 
| 220 | 229 |      $wkb = pack('L',$poly->numGeometries());
 | 
| @@ -227,6 +236,9 @@ discard block | ||
| 227 | 236 | return $wkb; | 
| 228 | 237 | } | 
| 229 | 238 | |
| 239 | + /** | |
| 240 | + * @param Geometry $geometry | |
| 241 | + */ | |
| 230 | 242 |    function writeMulti($geometry) {
 | 
| 231 | 243 | // Set the number of components | 
| 232 | 244 |      $wkb = pack('L',$geometry->numGeometries());
 | 
| @@ -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 | } | 
| @@ -30,11 +30,11 @@ discard block | ||
| 30 | 30 | */ | 
| 31 | 31 |    public function read($wkb, $is_hex_string = FALSE) {
 | 
| 32 | 32 |      if ($is_hex_string) {
 | 
| 33 | -      $wkb = pack('H*',$wkb);
 | |
| 33 | +      $wkb = pack('H*', $wkb);
 | |
| 34 | 34 | } | 
| 35 | 35 | |
| 36 | 36 |      if (empty($wkb)) {
 | 
| 37 | -      throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb));
 | |
| 37 | +      throw new Exception('Cannot read empty WKB geometry. Found '.gettype($wkb));
 | |
| 38 | 38 | } | 
| 39 | 39 | |
| 40 | 40 |      $mem = fopen('php://memory', 'r+');
 | 
| @@ -74,37 +74,37 @@ discard block | ||
| 74 | 74 | case 3: | 
| 75 | 75 | return $this->getPolygon($mem); | 
| 76 | 76 | case 4: | 
| 77 | - return $this->getMulti($mem,'point'); | |
| 77 | + return $this->getMulti($mem, 'point'); | |
| 78 | 78 | case 5: | 
| 79 | - return $this->getMulti($mem,'line'); | |
| 79 | + return $this->getMulti($mem, 'line'); | |
| 80 | 80 | case 6: | 
| 81 | - return $this->getMulti($mem,'polygon'); | |
| 81 | + return $this->getMulti($mem, 'polygon'); | |
| 82 | 82 | case 7: | 
| 83 | - return $this->getMulti($mem,'geometry'); | |
| 83 | + return $this->getMulti($mem, 'geometry'); | |
| 84 | 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 | 93 | // Get the number of points expected in this string out of the first 4 bytes | 
| 94 | -    $line_length = unpack('L',fread($mem,4));
 | |
| 94 | +    $line_length = unpack('L', fread($mem, 4));
 | |
| 95 | 95 | |
| 96 | 96 | // Return an empty linestring if there is no line-length | 
| 97 | 97 | if (!$line_length[1]) return new LineString(); | 
| 98 | 98 | |
| 99 | 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));
 | |
| 100 | +    $line_coords = unpack('d*', fread($mem, $line_length[1]*$this->dimension*8));
 | |
| 101 | 101 | |
| 102 | 102 | // We have our coords, build up the linestring | 
| 103 | 103 | $components = array(); | 
| 104 | 104 | $i = 1; | 
| 105 | 105 | $num_coords = count($line_coords); | 
| 106 | 106 |      while ($i <= $num_coords) {
 | 
| 107 | - $components[] = new Point($line_coords[$i],$line_coords[$i+1]); | |
| 107 | + $components[] = new Point($line_coords[$i], $line_coords[$i + 1]); | |
| 108 | 108 | $i += 2; | 
| 109 | 109 | } | 
| 110 | 110 | return new LineString($components); | 
| @@ -112,7 +112,7 @@ discard block | ||
| 112 | 112 | |
| 113 | 113 |    function getPolygon(&$mem) {
 | 
| 114 | 114 | // Get the number of linestring expected in this poly out of the first 4 bytes | 
| 115 | -    $poly_length = unpack('L',fread($mem,4));
 | |
| 115 | +    $poly_length = unpack('L', fread($mem, 4));
 | |
| 116 | 116 | |
| 117 | 117 | $components = array(); | 
| 118 | 118 | $i = 1; | 
| @@ -125,7 +125,7 @@ discard block | ||
| 125 | 125 | |
| 126 | 126 |    function getMulti(&$mem, $type) {
 | 
| 127 | 127 | // Get the number of items expected in this multi out of the first 4 bytes | 
| 128 | -    $multi_length = unpack('L',fread($mem,4));
 | |
| 128 | +    $multi_length = unpack('L', fread($mem, 4));
 | |
| 129 | 129 | |
| 130 | 130 | $components = array(); | 
| 131 | 131 | $i = 1; | 
| @@ -154,41 +154,41 @@ discard block | ||
| 154 | 154 | */ | 
| 155 | 155 |    public function write(Geometry $geometry, $write_as_hex = FALSE) {
 | 
| 156 | 156 | // We always write into NDR (little endian) | 
| 157 | -    $wkb = pack('c',1);
 | |
| 157 | +    $wkb = pack('c', 1);
 | |
| 158 | 158 | |
| 159 | 159 |      switch ($geometry->getGeomType()) {
 | 
| 160 | 160 | case 'Point'; | 
| 161 | -        $wkb .= pack('L',1);
 | |
| 161 | +        $wkb .= pack('L', 1);
 | |
| 162 | 162 | $wkb .= $this->writePoint($geometry); | 
| 163 | 163 | break; | 
| 164 | 164 | case 'LineString'; | 
| 165 | -        $wkb .= pack('L',2);
 | |
| 165 | +        $wkb .= pack('L', 2);
 | |
| 166 | 166 | $wkb .= $this->writeLineString($geometry); | 
| 167 | 167 | break; | 
| 168 | 168 | case 'Polygon'; | 
| 169 | -        $wkb .= pack('L',3);
 | |
| 169 | +        $wkb .= pack('L', 3);
 | |
| 170 | 170 | $wkb .= $this->writePolygon($geometry); | 
| 171 | 171 | break; | 
| 172 | 172 | case 'MultiPoint'; | 
| 173 | -        $wkb .= pack('L',4);
 | |
| 173 | +        $wkb .= pack('L', 4);
 | |
| 174 | 174 | $wkb .= $this->writeMulti($geometry); | 
| 175 | 175 | break; | 
| 176 | 176 | case 'MultiLineString'; | 
| 177 | -        $wkb .= pack('L',5);
 | |
| 177 | +        $wkb .= pack('L', 5);
 | |
| 178 | 178 | $wkb .= $this->writeMulti($geometry); | 
| 179 | 179 | break; | 
| 180 | 180 | case 'MultiPolygon'; | 
| 181 | -        $wkb .= pack('L',6);
 | |
| 181 | +        $wkb .= pack('L', 6);
 | |
| 182 | 182 | $wkb .= $this->writeMulti($geometry); | 
| 183 | 183 | break; | 
| 184 | 184 | case 'GeometryCollection'; | 
| 185 | -        $wkb .= pack('L',7);
 | |
| 185 | +        $wkb .= pack('L', 7);
 | |
| 186 | 186 | $wkb .= $this->writeMulti($geometry); | 
| 187 | 187 | break; | 
| 188 | 188 | } | 
| 189 | 189 | |
| 190 | 190 |      if ($write_as_hex) {
 | 
| 191 | -      $unpacked = unpack('H*',$wkb);
 | |
| 191 | +      $unpacked = unpack('H*', $wkb);
 | |
| 192 | 192 | return $unpacked[1]; | 
| 193 | 193 | } | 
| 194 | 194 |      else {
 | 
| @@ -198,18 +198,18 @@ discard block | ||
| 198 | 198 | |
| 199 | 199 |    function writePoint($point) {
 | 
| 200 | 200 | // Set the coords | 
| 201 | -    $wkb = pack('dd',$point->x(), $point->y());
 | |
| 201 | +    $wkb = pack('dd', $point->x(), $point->y());
 | |
| 202 | 202 | |
| 203 | 203 | return $wkb; | 
| 204 | 204 | } | 
| 205 | 205 | |
| 206 | 206 |    function writeLineString($line) {
 | 
| 207 | 207 | // Set the number of points in this line | 
| 208 | -    $wkb = pack('L',$line->numPoints());
 | |
| 208 | +    $wkb = pack('L', $line->numPoints());
 | |
| 209 | 209 | |
| 210 | 210 | // Set the coords | 
| 211 | 211 |      foreach ($line->getComponents() as $point) {
 | 
| 212 | -      $wkb .= pack('dd',$point->x(), $point->y());
 | |
| 212 | +      $wkb .= pack('dd', $point->x(), $point->y());
 | |
| 213 | 213 | } | 
| 214 | 214 | |
| 215 | 215 | return $wkb; | 
| @@ -217,7 +217,7 @@ discard block | ||
| 217 | 217 | |
| 218 | 218 |    function writePolygon($poly) {
 | 
| 219 | 219 | // Set the number of lines in this poly | 
| 220 | -    $wkb = pack('L',$poly->numGeometries());
 | |
| 220 | +    $wkb = pack('L', $poly->numGeometries());
 | |
| 221 | 221 | |
| 222 | 222 | // Write the lines | 
| 223 | 223 |      foreach ($poly->getComponents() as $line) {
 | 
| @@ -229,7 +229,7 @@ discard block | ||
| 229 | 229 | |
| 230 | 230 |    function writeMulti($geometry) {
 | 
| 231 | 231 | // Set the number of components | 
| 232 | -    $wkb = pack('L',$geometry->numGeometries());
 | |
| 232 | +    $wkb = pack('L', $geometry->numGeometries());
 | |
| 233 | 233 | |
| 234 | 234 | // Write the components | 
| 235 | 235 |      foreach ($geometry->getComponents() as $component) {
 | 
| @@ -94,7 +94,9 @@ discard block | ||
| 94 | 94 |      $line_length = unpack('L',fread($mem,4));
 | 
| 95 | 95 | |
| 96 | 96 | // Return an empty linestring if there is no line-length | 
| 97 | - if (!$line_length[1]) return new LineString(); | |
| 97 | +    if (!$line_length[1]) { | |
| 98 | + return new LineString(); | |
| 99 | + } | |
| 98 | 100 | |
| 99 | 101 | // Read the nubmer of points x2 (each point is two coords) into decimal-floats | 
| 100 | 102 |      $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8));
 | 
| @@ -190,8 +192,7 @@ discard block | ||
| 190 | 192 |      if ($write_as_hex) {
 | 
| 191 | 193 |        $unpacked = unpack('H*',$wkb);
 | 
| 192 | 194 | return $unpacked[1]; | 
| 193 | - } | |
| 194 | -    else {
 | |
| 195 | +    } else {
 | |
| 195 | 196 | return $wkb; | 
| 196 | 197 | } | 
| 197 | 198 | } | 
| @@ -8,7 +8,6 @@ discard block | ||
| 8 | 8 | /** | 
| 9 | 9 | * Read WKT string into geometry objects | 
| 10 | 10 | * | 
| 11 | - * @param string $WKT A WKT string | |
| 12 | 11 | * | 
| 13 | 12 | * @return Geometry | 
| 14 | 13 | */ | 
| @@ -184,11 +183,17 @@ discard block | ||
| 184 | 183 | else return $str; | 
| 185 | 184 | } | 
| 186 | 185 | |
| 186 | + /** | |
| 187 | + * @param string $char | |
| 188 | + */ | |
| 187 | 189 |    protected function beginsWith($str, $char) { | 
| 188 | 190 | if (substr($str,0,strlen($char)) == $char) return TRUE; | 
| 189 | 191 | else return FALSE; | 
| 190 | 192 | } | 
| 191 | 193 | |
| 194 | + /** | |
| 195 | + * @param string $char | |
| 196 | + */ | |
| 192 | 197 |    protected function endsWith($str, $char) { | 
| 193 | 198 | if (substr($str,(0 - strlen($char))) == $char) return TRUE; | 
| 194 | 199 | else return FALSE; | 
| @@ -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 | } | 
| @@ -16,10 +16,10 @@ discard block | ||
| 16 | 16 | $wkt = trim($wkt); | 
| 17 | 17 | |
| 18 | 18 | // If it contains a ';', then it contains additional SRID data | 
| 19 | -    if (strpos($wkt,';')) { | |
| 19 | +    if (strpos($wkt, ';')) { | |
| 20 | 20 |        $parts = explode(';', $wkt); | 
| 21 | 21 | $wkt = $parts[1]; | 
| 22 | -      $eparts = explode('=',$parts[0]); | |
| 22 | +      $eparts = explode('=', $parts[0]); | |
| 23 | 23 | $srid = $eparts[1]; | 
| 24 | 24 | } | 
| 25 | 25 |      else { | 
| @@ -63,7 +63,7 @@ discard block | ||
| 63 | 63 | |
| 64 | 64 |    private function parsePoint($data_string) { | 
| 65 | 65 | $data_string = $this->trimParens($data_string); | 
| 66 | -    $parts = explode(' ',$data_string); | |
| 66 | +    $parts = explode(' ', $data_string); | |
| 67 | 67 | return new Point($parts[0], $parts[1]); | 
| 68 | 68 | } | 
| 69 | 69 | |
| @@ -73,7 +73,7 @@ discard block | ||
| 73 | 73 | // If it's marked as empty, then return an empty line | 
| 74 | 74 | if ($data_string == 'EMPTY') return new LineString(); | 
| 75 | 75 | |
| 76 | -    $parts = explode(',',$data_string); | |
| 76 | +    $parts = explode(',', $data_string); | |
| 77 | 77 | $points = array(); | 
| 78 | 78 |      foreach ($parts as $part) { | 
| 79 | 79 | $points[] = $this->parsePoint($part); | 
| @@ -87,11 +87,11 @@ discard block | ||
| 87 | 87 | // If it's marked as empty, then return an empty polygon | 
| 88 | 88 | if ($data_string == 'EMPTY') return new Polygon(); | 
| 89 | 89 | |
| 90 | -    $parts = explode('),(',$data_string); | |
| 90 | +    $parts = explode('),(', $data_string); | |
| 91 | 91 | $lines = array(); | 
| 92 | 92 |      foreach ($parts as $part) { | 
| 93 | -      if (!$this->beginsWith($part,'(')) $part = '(' . $part; | |
| 94 | - if (!$this->endsWith($part,')')) $part = $part . ')'; | |
| 93 | +      if (!$this->beginsWith($part, '(')) $part = '('.$part; | |
| 94 | + if (!$this->endsWith($part, ')')) $part = $part.')'; | |
| 95 | 95 | $lines[] = $this->parseLineString($part); | 
| 96 | 96 | } | 
| 97 | 97 | return new Polygon($lines); | 
| @@ -103,7 +103,7 @@ discard block | ||
| 103 | 103 | // If it's marked as empty, then return an empty MutiPoint | 
| 104 | 104 | if ($data_string == 'EMPTY') return new MultiPoint(); | 
| 105 | 105 | |
| 106 | -    $parts = explode(',',$data_string); | |
| 106 | +    $parts = explode(',', $data_string); | |
| 107 | 107 | $points = array(); | 
| 108 | 108 |      foreach ($parts as $part) { | 
| 109 | 109 | $points[] = $this->parsePoint($part); | 
| @@ -117,12 +117,12 @@ discard block | ||
| 117 | 117 | // If it's marked as empty, then return an empty multi-linestring | 
| 118 | 118 | if ($data_string == 'EMPTY') return new MultiLineString(); | 
| 119 | 119 | |
| 120 | -    $parts = explode('),(',$data_string); | |
| 120 | +    $parts = explode('),(', $data_string); | |
| 121 | 121 | $lines = array(); | 
| 122 | 122 |      foreach ($parts as $part) { | 
| 123 | 123 | // Repair the string if the explode broke it | 
| 124 | -      if (!$this->beginsWith($part,'(')) $part = '(' . $part; | |
| 125 | - if (!$this->endsWith($part,')')) $part = $part . ')'; | |
| 124 | +      if (!$this->beginsWith($part, '(')) $part = '('.$part; | |
| 125 | + if (!$this->endsWith($part, ')')) $part = $part.')'; | |
| 126 | 126 | $lines[] = $this->parseLineString($part); | 
| 127 | 127 | } | 
| 128 | 128 | return new MultiLineString($lines); | 
| @@ -134,12 +134,12 @@ discard block | ||
| 134 | 134 | // If it's marked as empty, then return an empty multi-polygon | 
| 135 | 135 | if ($data_string == 'EMPTY') return new MultiPolygon(); | 
| 136 | 136 | |
| 137 | -    $parts = explode(')),((',$data_string); | |
| 137 | +    $parts = explode(')),((', $data_string); | |
| 138 | 138 | $polys = array(); | 
| 139 | 139 |      foreach ($parts as $part) { | 
| 140 | 140 | // Repair the string if the explode broke it | 
| 141 | -      if (!$this->beginsWith($part,'((')) $part = '((' . $part; | |
| 142 | - if (!$this->endsWith($part,'))')) $part = $part . '))'; | |
| 141 | +      if (!$this->beginsWith($part, '((')) $part = '(('.$part; | |
| 142 | + if (!$this->endsWith($part, '))')) $part = $part.'))'; | |
| 143 | 143 | $polys[] = $this->parsePolygon($part); | 
| 144 | 144 | } | 
| 145 | 145 | return new MultiPolygon($polys); | 
| @@ -179,18 +179,18 @@ discard block | ||
| 179 | 179 | |
| 180 | 180 | // We want to only strip off one set of parenthesis | 
| 181 | 181 |      if ($this->beginsWith($str, '(')) { | 
| 182 | - return substr($str,1,-1); | |
| 182 | + return substr($str, 1, -1); | |
| 183 | 183 | } | 
| 184 | 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; | |
| 188 | + if (substr($str, 0, strlen($char)) == $char) return TRUE; | |
| 189 | 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; | |
| 193 | + if (substr($str, (0 - strlen($char))) == $char) return TRUE; | |
| 194 | 194 | else return FALSE; | 
| 195 | 195 | } | 
| 196 | 196 | |
| @@ -21,8 +21,7 @@ discard block | ||
| 21 | 21 | $wkt = $parts[1]; | 
| 22 | 22 |        $eparts = explode('=',$parts[0]); | 
| 23 | 23 | $srid = $eparts[1]; | 
| 24 | - } | |
| 25 | -    else { | |
| 24 | +    } else { | |
| 26 | 25 | $srid = NULL; | 
| 27 | 26 | } | 
| 28 | 27 | |
| @@ -33,8 +32,7 @@ discard block | ||
| 33 | 32 | $geom = geoPHP::geosToGeometry($reader->read($wkt)); | 
| 34 | 33 | $geom->setSRID($srid); | 
| 35 | 34 | return $geom; | 
| 36 | - } | |
| 37 | -      else {  | |
| 35 | +      } else {  | |
| 38 | 36 | return geoPHP::geosToGeometry($reader->read($wkt)); | 
| 39 | 37 | } | 
| 40 | 38 | } | 
| @@ -52,8 +50,7 @@ discard block | ||
| 52 | 50 | $geom = $this->$method($data_string); | 
| 53 | 51 | $geom->setSRID($srid); | 
| 54 | 52 | return $geom; | 
| 55 | - } | |
| 56 | -        else {  | |
| 53 | +        } else {  | |
| 57 | 54 | return $this->$method($data_string); | 
| 58 | 55 | } | 
| 59 | 56 | |
| @@ -71,7 +68,9 @@ discard block | ||
| 71 | 68 | $data_string = $this->trimParens($data_string); | 
| 72 | 69 | |
| 73 | 70 | // If it's marked as empty, then return an empty line | 
| 74 | - if ($data_string == 'EMPTY') return new LineString(); | |
| 71 | +    if ($data_string == 'EMPTY') { | |
| 72 | + return new LineString(); | |
| 73 | + } | |
| 75 | 74 | |
| 76 | 75 |      $parts = explode(',',$data_string); | 
| 77 | 76 | $points = array(); | 
| @@ -85,13 +84,19 @@ discard block | ||
| 85 | 84 | $data_string = $this->trimParens($data_string); | 
| 86 | 85 | |
| 87 | 86 | // If it's marked as empty, then return an empty polygon | 
| 88 | - if ($data_string == 'EMPTY') return new Polygon(); | |
| 87 | +    if ($data_string == 'EMPTY') { | |
| 88 | + return new Polygon(); | |
| 89 | + } | |
| 89 | 90 | |
| 90 | 91 |      $parts = explode('),(',$data_string); | 
| 91 | 92 | $lines = array(); | 
| 92 | 93 |      foreach ($parts as $part) { | 
| 93 | -      if (!$this->beginsWith($part,'(')) $part = '(' . $part; | |
| 94 | - if (!$this->endsWith($part,')')) $part = $part . ')'; | |
| 94 | +      if (!$this->beginsWith($part,'(')) { | |
| 95 | +      	$part = '(' . $part; | |
| 96 | + } | |
| 97 | +      if (!$this->endsWith($part,')')) { | |
| 98 | + $part = $part . ')'; | |
| 99 | + } | |
| 95 | 100 | $lines[] = $this->parseLineString($part); | 
| 96 | 101 | } | 
| 97 | 102 | return new Polygon($lines); | 
| @@ -101,7 +106,9 @@ discard block | ||
| 101 | 106 | $data_string = $this->trimParens($data_string); | 
| 102 | 107 | |
| 103 | 108 | // If it's marked as empty, then return an empty MutiPoint | 
| 104 | - if ($data_string == 'EMPTY') return new MultiPoint(); | |
| 109 | +    if ($data_string == 'EMPTY') { | |
| 110 | + return new MultiPoint(); | |
| 111 | + } | |
| 105 | 112 | |
| 106 | 113 |      $parts = explode(',',$data_string); | 
| 107 | 114 | $points = array(); | 
| @@ -115,14 +122,20 @@ discard block | ||
| 115 | 122 | $data_string = $this->trimParens($data_string); | 
| 116 | 123 | |
| 117 | 124 | // If it's marked as empty, then return an empty multi-linestring | 
| 118 | - if ($data_string == 'EMPTY') return new MultiLineString(); | |
| 125 | +    if ($data_string == 'EMPTY') { | |
| 126 | + return new MultiLineString(); | |
| 127 | + } | |
| 119 | 128 | |
| 120 | 129 |      $parts = explode('),(',$data_string); | 
| 121 | 130 | $lines = array(); | 
| 122 | 131 |      foreach ($parts as $part) { | 
| 123 | 132 | // Repair the string if the explode broke it | 
| 124 | -      if (!$this->beginsWith($part,'(')) $part = '(' . $part; | |
| 125 | - if (!$this->endsWith($part,')')) $part = $part . ')'; | |
| 133 | +      if (!$this->beginsWith($part,'(')) { | |
| 134 | +      	$part = '(' . $part; | |
| 135 | + } | |
| 136 | +      if (!$this->endsWith($part,')')) { | |
| 137 | + $part = $part . ')'; | |
| 138 | + } | |
| 126 | 139 | $lines[] = $this->parseLineString($part); | 
| 127 | 140 | } | 
| 128 | 141 | return new MultiLineString($lines); | 
| @@ -132,14 +145,20 @@ discard block | ||
| 132 | 145 | $data_string = $this->trimParens($data_string); | 
| 133 | 146 | |
| 134 | 147 | // If it's marked as empty, then return an empty multi-polygon | 
| 135 | - if ($data_string == 'EMPTY') return new MultiPolygon(); | |
| 148 | +    if ($data_string == 'EMPTY') { | |
| 149 | + return new MultiPolygon(); | |
| 150 | + } | |
| 136 | 151 | |
| 137 | 152 |      $parts = explode(')),((',$data_string); | 
| 138 | 153 | $polys = array(); | 
| 139 | 154 |      foreach ($parts as $part) { | 
| 140 | 155 | // Repair the string if the explode broke it | 
| 141 | -      if (!$this->beginsWith($part,'((')) $part = '((' . $part; | |
| 142 | - if (!$this->endsWith($part,'))')) $part = $part . '))'; | |
| 156 | +      if (!$this->beginsWith($part,'((')) { | |
| 157 | +      	$part = '((' . $part; | |
| 158 | + } | |
| 159 | +      if (!$this->endsWith($part,'))')) { | |
| 160 | + $part = $part . '))'; | |
| 161 | + } | |
| 143 | 162 | $polys[] = $this->parsePolygon($part); | 
| 144 | 163 | } | 
| 145 | 164 | return new MultiPolygon($polys); | 
| @@ -149,7 +168,9 @@ discard block | ||
| 149 | 168 | $data_string = $this->trimParens($data_string); | 
| 150 | 169 | |
| 151 | 170 | // If it's marked as empty, then return an empty geom-collection | 
| 152 | - if ($data_string == 'EMPTY') return new GeometryCollection(); | |
| 171 | +    if ($data_string == 'EMPTY') { | |
| 172 | + return new GeometryCollection(); | |
| 173 | + } | |
| 153 | 174 | |
| 154 | 175 | $geometries = array(); | 
| 155 | 176 | $matches = array(); | 
| @@ -180,18 +201,25 @@ discard block | ||
| 180 | 201 | // We want to only strip off one set of parenthesis | 
| 181 | 202 |      if ($this->beginsWith($str, '(')) { | 
| 182 | 203 | return substr($str,1,-1); | 
| 204 | +    } else { | |
| 205 | + return $str; | |
| 183 | 206 | } | 
| 184 | - else return $str; | |
| 185 | 207 | } | 
| 186 | 208 | |
| 187 | 209 |    protected function beginsWith($str, $char) { | 
| 188 | - if (substr($str,0,strlen($char)) == $char) return TRUE; | |
| 189 | - else return FALSE; | |
| 210 | +    if (substr($str,0,strlen($char)) == $char) { | |
| 211 | + return TRUE; | |
| 212 | +    } else { | |
| 213 | + return FALSE; | |
| 214 | + } | |
| 190 | 215 | } | 
| 191 | 216 | |
| 192 | 217 |    protected function endsWith($str, $char) { | 
| 193 | - if (substr($str,(0 - strlen($char))) == $char) return TRUE; | |
| 194 | - else return FALSE; | |
| 218 | +    if (substr($str,(0 - strlen($char))) == $char) { | |
| 219 | + return TRUE; | |
| 220 | +    } else { | |
| 221 | + return FALSE; | |
| 222 | + } | |
| 195 | 223 | } | 
| 196 | 224 | |
| 197 | 225 | /** | 
| @@ -211,8 +239,7 @@ discard block | ||
| 211 | 239 | |
| 212 | 240 |      if ($geometry->isEmpty()) { | 
| 213 | 241 | return strtoupper($geometry->geometryType()).' EMPTY'; | 
| 214 | - } | |
| 215 | -    else if ($data = $this->extractData($geometry)) { | |
| 242 | +    } else if ($data = $this->extractData($geometry)) { | |
| 216 | 243 |        return strtoupper($geometry->geometryType()).' ('.$data.')'; | 
| 217 | 244 | } | 
| 218 | 245 | } | 
| @@ -193,6 +193,9 @@ | ||
| 193 | 193 | } | 
| 194 | 194 | } | 
| 195 | 195 | |
| 196 | + /** | |
| 197 | + * @param Point $point | |
| 198 | + */ | |
| 196 | 199 |    public function pointOnVertex($point) { | 
| 197 | 200 |      foreach($this->getPoints() as $vertex) { | 
| 198 | 201 |        if ($point->equals($vertex)) { | 
| @@ -9,70 +9,70 @@ discard block | ||
| 9 | 9 | protected $geom_type = 'Polygon'; | 
| 10 | 10 | |
| 11 | 11 |    public function area($exterior_only = FALSE, $signed = FALSE) { | 
| 12 | - if ($this->isEmpty()) return 0; | |
| 12 | + if ($this->isEmpty()) return 0; | |
| 13 | 13 | |
| 14 | -    if ($this->geos() && $exterior_only == FALSE) { | |
| 15 | - return $this->geos()->area(); | |
| 16 | - } | |
| 14 | +	if ($this->geos() && $exterior_only == FALSE) { | |
| 15 | + return $this->geos()->area(); | |
| 16 | + } | |
| 17 | 17 | |
| 18 | - $exterior_ring = $this->components[0]; | |
| 19 | - $pts = $exterior_ring->getComponents(); | |
| 18 | + $exterior_ring = $this->components[0]; | |
| 19 | + $pts = $exterior_ring->getComponents(); | |
| 20 | 20 | |
| 21 | - $c = count($pts); | |
| 22 | - if((int)$c == '0') return NULL; | |
| 23 | - $a = '0'; | |
| 24 | -    foreach($pts as $k => $p){ | |
| 25 | - $j = ($k + 1) % $c; | |
| 26 | - $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | |
| 27 | - } | |
| 21 | + $c = count($pts); | |
| 22 | + if((int)$c == '0') return NULL; | |
| 23 | + $a = '0'; | |
| 24 | +	foreach($pts as $k => $p){ | |
| 25 | + $j = ($k + 1) % $c; | |
| 26 | + $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | |
| 27 | + } | |
| 28 | 28 | |
| 29 | - if ($signed) $area = ($a / 2); | |
| 30 | - else $area = abs(($a / 2)); | |
| 29 | + if ($signed) $area = ($a / 2); | |
| 30 | + else $area = abs(($a / 2)); | |
| 31 | 31 | |
| 32 | -    if ($exterior_only == TRUE) { | |
| 33 | - return $area; | |
| 34 | - } | |
| 35 | -    foreach ($this->components as $delta => $component) { | |
| 36 | -      if ($delta != 0) { | |
| 37 | - $inner_poly = new Polygon(array($component)); | |
| 38 | - $area -= $inner_poly->area(); | |
| 39 | - } | |
| 40 | - } | |
| 41 | - return $area; | |
| 32 | +	if ($exterior_only == TRUE) { | |
| 33 | + return $area; | |
| 34 | + } | |
| 35 | +	foreach ($this->components as $delta => $component) { | |
| 36 | +	  if ($delta != 0) { | |
| 37 | + $inner_poly = new Polygon(array($component)); | |
| 38 | + $area -= $inner_poly->area(); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + return $area; | |
| 42 | 42 | } | 
| 43 | 43 | |
| 44 | 44 |    public function centroid() { | 
| 45 | - if ($this->isEmpty()) return NULL; | |
| 45 | + if ($this->isEmpty()) return NULL; | |
| 46 | 46 | |
| 47 | -    if ($this->geos()) { | |
| 48 | - return geoPHP::geosToGeometry($this->geos()->centroid()); | |
| 49 | - } | |
| 47 | +	if ($this->geos()) { | |
| 48 | + return geoPHP::geosToGeometry($this->geos()->centroid()); | |
| 49 | + } | |
| 50 | 50 | |
| 51 | - $exterior_ring = $this->components[0]; | |
| 52 | - $pts = $exterior_ring->getComponents(); | |
| 51 | + $exterior_ring = $this->components[0]; | |
| 52 | + $pts = $exterior_ring->getComponents(); | |
| 53 | 53 | |
| 54 | - $c = count($pts); | |
| 55 | - if((int)$c == '0') return NULL; | |
| 56 | -    $cn = array('x' => '0', 'y' => '0'); | |
| 57 | - $a = $this->area(TRUE, TRUE); | |
| 54 | + $c = count($pts); | |
| 55 | + if((int)$c == '0') return NULL; | |
| 56 | +	$cn = array('x' => '0', 'y' => '0'); | |
| 57 | + $a = $this->area(TRUE, TRUE); | |
| 58 | 58 | |
| 59 | - // If this is a polygon with no area. Just return the first point. | |
| 60 | -    if ($a == 0) { | |
| 61 | - return $this->exteriorRing()->pointN(1); | |
| 62 | - } | |
| 59 | + // If this is a polygon with no area. Just return the first point. | |
| 60 | +	if ($a == 0) { | |
| 61 | + return $this->exteriorRing()->pointN(1); | |
| 62 | + } | |
| 63 | 63 | |
| 64 | -    foreach($pts as $k => $p){ | |
| 65 | - $j = ($k + 1) % $c; | |
| 66 | - $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | |
| 67 | - $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; | |
| 68 | - $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; | |
| 69 | - } | |
| 64 | +	foreach($pts as $k => $p){ | |
| 65 | + $j = ($k + 1) % $c; | |
| 66 | + $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | |
| 67 | + $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; | |
| 68 | + $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; | |
| 69 | + } | |
| 70 | 70 | |
| 71 | - $cn['x'] = $cn['x'] / ( 6 * $a); | |
| 72 | - $cn['y'] = $cn['y'] / ( 6 * $a); | |
| 71 | + $cn['x'] = $cn['x'] / ( 6 * $a); | |
| 72 | + $cn['y'] = $cn['y'] / ( 6 * $a); | |
| 73 | 73 | |
| 74 | - $centroid = new Point($cn['x'], $cn['y']); | |
| 75 | - return $centroid; | |
| 74 | + $centroid = new Point($cn['x'], $cn['y']); | |
| 75 | + return $centroid; | |
| 76 | 76 | } | 
| 77 | 77 | |
| 78 | 78 | /** | 
| @@ -98,41 +98,41 @@ discard block | ||
| 98 | 98 | } | 
| 99 | 99 | |
| 100 | 100 |    public function exteriorRing() { | 
| 101 | - if ($this->isEmpty()) return new LineString(); | |
| 102 | - return $this->components[0]; | |
| 101 | + if ($this->isEmpty()) return new LineString(); | |
| 102 | + return $this->components[0]; | |
| 103 | 103 | } | 
| 104 | 104 | |
| 105 | 105 |    public function numInteriorRings() { | 
| 106 | - if ($this->isEmpty()) return 0; | |
| 107 | - return $this->numGeometries()-1; | |
| 106 | + if ($this->isEmpty()) return 0; | |
| 107 | + return $this->numGeometries()-1; | |
| 108 | 108 | } | 
| 109 | 109 | |
| 110 | 110 |    public function interiorRingN($n) { | 
| 111 | - return $this->geometryN($n+1); | |
| 111 | + return $this->geometryN($n+1); | |
| 112 | 112 | } | 
| 113 | 113 | |
| 114 | 114 |    public function dimension() { | 
| 115 | - if ($this->isEmpty()) return 0; | |
| 116 | - return 2; | |
| 115 | + if ($this->isEmpty()) return 0; | |
| 116 | + return 2; | |
| 117 | 117 | } | 
| 118 | 118 | |
| 119 | 119 |    public function isSimple() { | 
| 120 | -    if ($this->geos()) { | |
| 121 | - return $this->geos()->isSimple(); | |
| 122 | - } | |
| 120 | +	if ($this->geos()) { | |
| 121 | + return $this->geos()->isSimple(); | |
| 122 | + } | |
| 123 | 123 | |
| 124 | - $segments = $this->explode(); | |
| 124 | + $segments = $this->explode(); | |
| 125 | 125 | |
| 126 | -    foreach ($segments as $i => $segment) { | |
| 127 | -      foreach ($segments as $j => $check_segment) { | |
| 128 | -        if ($i != $j) { | |
| 129 | -          if ($segment->lineSegmentIntersect($check_segment)) { | |
| 130 | - return FALSE; | |
| 131 | - } | |
| 132 | - } | |
| 133 | - } | |
| 134 | - } | |
| 135 | - return TRUE; | |
| 126 | +	foreach ($segments as $i => $segment) { | |
| 127 | +	  foreach ($segments as $j => $check_segment) { | |
| 128 | +		if ($i != $j) { | |
| 129 | +		  if ($segment->lineSegmentIntersect($check_segment)) { | |
| 130 | + return FALSE; | |
| 131 | + } | |
| 132 | + } | |
| 133 | + } | |
| 134 | + } | |
| 135 | + return TRUE; | |
| 136 | 136 | } | 
| 137 | 137 | |
| 138 | 138 | /** | 
| @@ -146,59 +146,59 @@ discard block | ||
| 146 | 146 | * @return boolean | 
| 147 | 147 | */ | 
| 148 | 148 |    public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex = true) { | 
| 149 | - $vertices = $this->getPoints(); | |
| 149 | + $vertices = $this->getPoints(); | |
| 150 | 150 | |
| 151 | - // Check if the point sits exactly on a vertex | |
| 152 | -    if ($this->pointOnVertex($point, $vertices)) { | |
| 153 | - return $pointOnVertex ? TRUE : FALSE; | |
| 154 | - } | |
| 151 | + // Check if the point sits exactly on a vertex | |
| 152 | +	if ($this->pointOnVertex($point, $vertices)) { | |
| 153 | + return $pointOnVertex ? TRUE : FALSE; | |
| 154 | + } | |
| 155 | 155 | |
| 156 | - // Check if the point is inside the polygon or on the boundary | |
| 157 | - $intersections = 0; | |
| 158 | - $vertices_count = count($vertices); | |
| 156 | + // Check if the point is inside the polygon or on the boundary | |
| 157 | + $intersections = 0; | |
| 158 | + $vertices_count = count($vertices); | |
| 159 | 159 | |
| 160 | -    for ($i=1; $i < $vertices_count; $i++) { | |
| 161 | - $vertex1 = $vertices[$i-1]; | |
| 162 | - $vertex2 = $vertices[$i]; | |
| 163 | - if ($vertex1->y() == $vertex2->y() | |
| 164 | - && $vertex1->y() == $point->y() | |
| 165 | - && $point->x() > min($vertex1->x(), $vertex2->x()) | |
| 166 | -      && $point->x() < max($vertex1->x(), $vertex2->x())) { | |
| 167 | - // Check if point is on an horizontal polygon boundary | |
| 168 | - return $pointOnBoundary ? TRUE : FALSE; | |
| 169 | - } | |
| 170 | - if ($point->y() > min($vertex1->y(), $vertex2->y()) | |
| 171 | - && $point->y() <= max($vertex1->y(), $vertex2->y()) | |
| 172 | - && $point->x() <= max($vertex1->x(), $vertex2->x()) | |
| 173 | -      && $vertex1->y() != $vertex2->y()) { | |
| 174 | - $xinters = | |
| 175 | - ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) | |
| 176 | - / ($vertex2->y() - $vertex1->y()) | |
| 177 | - + $vertex1->x(); | |
| 178 | -        if ($xinters == $point->x()) { | |
| 179 | - // Check if point is on the polygon boundary (other than horizontal) | |
| 180 | - return $pointOnBoundary ? TRUE : FALSE; | |
| 181 | - } | |
| 182 | -        if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { | |
| 183 | - $intersections++; | |
| 184 | - } | |
| 185 | - } | |
| 186 | - } | |
| 187 | - // If the number of edges we passed through is even, then it's in the polygon. | |
| 188 | -    if ($intersections % 2 != 0) { | |
| 189 | - return TRUE; | |
| 190 | - } | |
| 191 | -    else { | |
| 192 | - return FALSE; | |
| 193 | - } | |
| 160 | +	for ($i=1; $i < $vertices_count; $i++) { | |
| 161 | + $vertex1 = $vertices[$i-1]; | |
| 162 | + $vertex2 = $vertices[$i]; | |
| 163 | + if ($vertex1->y() == $vertex2->y() | |
| 164 | + && $vertex1->y() == $point->y() | |
| 165 | + && $point->x() > min($vertex1->x(), $vertex2->x()) | |
| 166 | +	  && $point->x() < max($vertex1->x(), $vertex2->x())) { | |
| 167 | + // Check if point is on an horizontal polygon boundary | |
| 168 | + return $pointOnBoundary ? TRUE : FALSE; | |
| 169 | + } | |
| 170 | + if ($point->y() > min($vertex1->y(), $vertex2->y()) | |
| 171 | + && $point->y() <= max($vertex1->y(), $vertex2->y()) | |
| 172 | + && $point->x() <= max($vertex1->x(), $vertex2->x()) | |
| 173 | +	  && $vertex1->y() != $vertex2->y()) { | |
| 174 | + $xinters = | |
| 175 | + ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) | |
| 176 | + / ($vertex2->y() - $vertex1->y()) | |
| 177 | + + $vertex1->x(); | |
| 178 | +		if ($xinters == $point->x()) { | |
| 179 | + // Check if point is on the polygon boundary (other than horizontal) | |
| 180 | + return $pointOnBoundary ? TRUE : FALSE; | |
| 181 | + } | |
| 182 | +		if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { | |
| 183 | + $intersections++; | |
| 184 | + } | |
| 185 | + } | |
| 186 | + } | |
| 187 | + // If the number of edges we passed through is even, then it's in the polygon. | |
| 188 | +	if ($intersections % 2 != 0) { | |
| 189 | + return TRUE; | |
| 190 | + } | |
| 191 | +	else { | |
| 192 | + return FALSE; | |
| 193 | + } | |
| 194 | 194 | } | 
| 195 | 195 | |
| 196 | 196 |    public function pointOnVertex($point) { | 
| 197 | -    foreach($this->getPoints() as $vertex) { | |
| 198 | -      if ($point->equals($vertex)) { | |
| 199 | - return true; | |
| 200 | - } | |
| 201 | - } | |
| 197 | +	foreach($this->getPoints() as $vertex) { | |
| 198 | +	  if ($point->equals($vertex)) { | |
| 199 | + return true; | |
| 200 | + } | |
| 201 | + } | |
| 202 | 202 | } | 
| 203 | 203 | |
| 204 | 204 | |
| @@ -19,15 +19,15 @@ discard block | ||
| 19 | 19 | $pts = $exterior_ring->getComponents(); | 
| 20 | 20 | |
| 21 | 21 | $c = count($pts); | 
| 22 | - if((int)$c == '0') return NULL; | |
| 22 | + if ((int) $c == '0') return NULL; | |
| 23 | 23 | $a = '0'; | 
| 24 | -    foreach($pts as $k => $p){ | |
| 25 | - $j = ($k + 1) % $c; | |
| 26 | - $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | |
| 24 | +    foreach ($pts as $k => $p) { | |
| 25 | + $j = ($k + 1)%$c; | |
| 26 | + $a = $a + ($p->getX()*$pts[$j]->getY()) - ($p->getY()*$pts[$j]->getX()); | |
| 27 | 27 | } | 
| 28 | 28 | |
| 29 | - if ($signed) $area = ($a / 2); | |
| 30 | - else $area = abs(($a / 2)); | |
| 29 | + if ($signed) $area = ($a/2); | |
| 30 | + else $area = abs(($a/2)); | |
| 31 | 31 | |
| 32 | 32 |      if ($exterior_only == TRUE) { | 
| 33 | 33 | return $area; | 
| @@ -52,7 +52,7 @@ discard block | ||
| 52 | 52 | $pts = $exterior_ring->getComponents(); | 
| 53 | 53 | |
| 54 | 54 | $c = count($pts); | 
| 55 | - if((int)$c == '0') return NULL; | |
| 55 | + if ((int) $c == '0') return NULL; | |
| 56 | 56 |      $cn = array('x' => '0', 'y' => '0'); | 
| 57 | 57 | $a = $this->area(TRUE, TRUE); | 
| 58 | 58 | |
| @@ -61,15 +61,15 @@ discard block | ||
| 61 | 61 | return $this->exteriorRing()->pointN(1); | 
| 62 | 62 | } | 
| 63 | 63 | |
| 64 | -    foreach($pts as $k => $p){ | |
| 65 | - $j = ($k + 1) % $c; | |
| 66 | - $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | |
| 67 | - $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; | |
| 68 | - $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; | |
| 64 | +    foreach ($pts as $k => $p) { | |
| 65 | + $j = ($k + 1)%$c; | |
| 66 | + $P = ($p->getX()*$pts[$j]->getY()) - ($p->getY()*$pts[$j]->getX()); | |
| 67 | + $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX())*$P; | |
| 68 | + $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY())*$P; | |
| 69 | 69 | } | 
| 70 | 70 | |
| 71 | - $cn['x'] = $cn['x'] / ( 6 * $a); | |
| 72 | - $cn['y'] = $cn['y'] / ( 6 * $a); | |
| 71 | + $cn['x'] = $cn['x']/(6*$a); | |
| 72 | + $cn['y'] = $cn['y']/(6*$a); | |
| 73 | 73 | |
| 74 | 74 | $centroid = new Point($cn['x'], $cn['y']); | 
| 75 | 75 | return $centroid; | 
| @@ -85,10 +85,10 @@ discard block | ||
| 85 | 85 | |
| 86 | 86 |  		$max = array('length' => 0, 'point' => null); | 
| 87 | 87 | |
| 88 | -		foreach($this->getPoints() as $point) { | |
| 88 | +		foreach ($this->getPoints() as $point) { | |
| 89 | 89 | $lineString = new LineString(array($centroid, $point)); | 
| 90 | 90 | |
| 91 | -			if($lineString->length() > $max['length']) { | |
| 91 | +			if ($lineString->length() > $max['length']) { | |
| 92 | 92 | $max['length'] = $lineString->length(); | 
| 93 | 93 | $max['point'] = $point; | 
| 94 | 94 | } | 
| @@ -104,11 +104,11 @@ discard block | ||
| 104 | 104 | |
| 105 | 105 |    public function numInteriorRings() { | 
| 106 | 106 | if ($this->isEmpty()) return 0; | 
| 107 | - return $this->numGeometries()-1; | |
| 107 | + return $this->numGeometries() - 1; | |
| 108 | 108 | } | 
| 109 | 109 | |
| 110 | 110 |    public function interiorRingN($n) { | 
| 111 | - return $this->geometryN($n+1); | |
| 111 | + return $this->geometryN($n + 1); | |
| 112 | 112 | } | 
| 113 | 113 | |
| 114 | 114 |    public function dimension() { | 
| @@ -157,8 +157,8 @@ discard block | ||
| 157 | 157 | $intersections = 0; | 
| 158 | 158 | $vertices_count = count($vertices); | 
| 159 | 159 | |
| 160 | -    for ($i=1; $i < $vertices_count; $i++) { | |
| 161 | - $vertex1 = $vertices[$i-1]; | |
| 160 | +    for ($i = 1; $i < $vertices_count; $i++) { | |
| 161 | + $vertex1 = $vertices[$i - 1]; | |
| 162 | 162 | $vertex2 = $vertices[$i]; | 
| 163 | 163 | if ($vertex1->y() == $vertex2->y() | 
| 164 | 164 | && $vertex1->y() == $point->y() | 
| @@ -172,7 +172,7 @@ discard block | ||
| 172 | 172 | && $point->x() <= max($vertex1->x(), $vertex2->x()) | 
| 173 | 173 |        && $vertex1->y() != $vertex2->y()) { | 
| 174 | 174 | $xinters = | 
| 175 | - ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) | |
| 175 | + ($point->y() - $vertex1->y())*($vertex2->x() - $vertex1->x()) | |
| 176 | 176 | / ($vertex2->y() - $vertex1->y()) | 
| 177 | 177 | + $vertex1->x(); | 
| 178 | 178 |          if ($xinters == $point->x()) { | 
| @@ -185,7 +185,7 @@ discard block | ||
| 185 | 185 | } | 
| 186 | 186 | } | 
| 187 | 187 | // If the number of edges we passed through is even, then it's in the polygon. | 
| 188 | -    if ($intersections % 2 != 0) { | |
| 188 | +    if ($intersections%2 != 0) { | |
| 189 | 189 | return TRUE; | 
| 190 | 190 | } | 
| 191 | 191 |      else { | 
| @@ -194,7 +194,7 @@ discard block | ||
| 194 | 194 | } | 
| 195 | 195 | |
| 196 | 196 |    public function pointOnVertex($point) { | 
| 197 | -    foreach($this->getPoints() as $vertex) { | |
| 197 | +    foreach ($this->getPoints() as $vertex) { | |
| 198 | 198 |        if ($point->equals($vertex)) { | 
| 199 | 199 | return true; | 
| 200 | 200 | } | 
| @@ -9,7 +9,9 @@ discard block | ||
| 9 | 9 | protected $geom_type = 'Polygon'; | 
| 10 | 10 | |
| 11 | 11 |    public function area($exterior_only = FALSE, $signed = FALSE) { | 
| 12 | - if ($this->isEmpty()) return 0; | |
| 12 | +    if ($this->isEmpty()) { | |
| 13 | + return 0; | |
| 14 | + } | |
| 13 | 15 | |
| 14 | 16 |      if ($this->geos() && $exterior_only == FALSE) { | 
| 15 | 17 | return $this->geos()->area(); | 
| @@ -19,15 +21,20 @@ discard block | ||
| 19 | 21 | $pts = $exterior_ring->getComponents(); | 
| 20 | 22 | |
| 21 | 23 | $c = count($pts); | 
| 22 | - if((int)$c == '0') return NULL; | |
| 24 | +    if((int)$c == '0') { | |
| 25 | + return NULL; | |
| 26 | + } | |
| 23 | 27 | $a = '0'; | 
| 24 | 28 |      foreach($pts as $k => $p){ | 
| 25 | 29 | $j = ($k + 1) % $c; | 
| 26 | 30 | $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); | 
| 27 | 31 | } | 
| 28 | 32 | |
| 29 | - if ($signed) $area = ($a / 2); | |
| 30 | - else $area = abs(($a / 2)); | |
| 33 | +    if ($signed) { | |
| 34 | + $area = ($a / 2); | |
| 35 | +    } else { | |
| 36 | + $area = abs(($a / 2)); | |
| 37 | + } | |
| 31 | 38 | |
| 32 | 39 |      if ($exterior_only == TRUE) { | 
| 33 | 40 | return $area; | 
| @@ -42,7 +49,9 @@ discard block | ||
| 42 | 49 | } | 
| 43 | 50 | |
| 44 | 51 |    public function centroid() { | 
| 45 | - if ($this->isEmpty()) return NULL; | |
| 52 | +    if ($this->isEmpty()) { | |
| 53 | + return NULL; | |
| 54 | + } | |
| 46 | 55 | |
| 47 | 56 |      if ($this->geos()) { | 
| 48 | 57 | return geoPHP::geosToGeometry($this->geos()->centroid()); | 
| @@ -52,7 +61,9 @@ discard block | ||
| 52 | 61 | $pts = $exterior_ring->getComponents(); | 
| 53 | 62 | |
| 54 | 63 | $c = count($pts); | 
| 55 | - if((int)$c == '0') return NULL; | |
| 64 | +    if((int)$c == '0') { | |
| 65 | + return NULL; | |
| 66 | + } | |
| 56 | 67 |      $cn = array('x' => '0', 'y' => '0'); | 
| 57 | 68 | $a = $this->area(TRUE, TRUE); | 
| 58 | 69 | |
| @@ -98,12 +109,16 @@ discard block | ||
| 98 | 109 | } | 
| 99 | 110 | |
| 100 | 111 |    public function exteriorRing() { | 
| 101 | - if ($this->isEmpty()) return new LineString(); | |
| 112 | +    if ($this->isEmpty()) { | |
| 113 | + return new LineString(); | |
| 114 | + } | |
| 102 | 115 | return $this->components[0]; | 
| 103 | 116 | } | 
| 104 | 117 | |
| 105 | 118 |    public function numInteriorRings() { | 
| 106 | - if ($this->isEmpty()) return 0; | |
| 119 | +    if ($this->isEmpty()) { | |
| 120 | + return 0; | |
| 121 | + } | |
| 107 | 122 | return $this->numGeometries()-1; | 
| 108 | 123 | } | 
| 109 | 124 | |
| @@ -112,7 +127,9 @@ discard block | ||
| 112 | 127 | } | 
| 113 | 128 | |
| 114 | 129 |    public function dimension() { | 
| 115 | - if ($this->isEmpty()) return 0; | |
| 130 | +    if ($this->isEmpty()) { | |
| 131 | + return 0; | |
| 132 | + } | |
| 116 | 133 | return 2; | 
| 117 | 134 | } | 
| 118 | 135 | |
| @@ -187,8 +204,7 @@ discard block | ||
| 187 | 204 | // If the number of edges we passed through is even, then it's in the polygon. | 
| 188 | 205 |      if ($intersections % 2 != 0) { | 
| 189 | 206 | return TRUE; | 
| 190 | - } | |
| 191 | -    else { | |
| 207 | +    } else { | |
| 192 | 208 | return FALSE; | 
| 193 | 209 | } | 
| 194 | 210 | } |