| @@ -3,6 +3,10 @@ | ||
| 3 | 3 | require_once(dirname(__FILE__).'/../require/class.Connection.php'); | 
| 4 | 4 | |
| 5 | 5 |  class create_db { | 
| 6 | + | |
| 7 | + /** | |
| 8 | + * @param string $filename | |
| 9 | + */ | |
| 6 | 10 |  	public static function import_file($filename) { | 
| 7 | 11 | $filename = filter_var($filename,FILTER_SANITIZE_STRING); | 
| 8 | 12 | $Connection = new Connection(); | 
| @@ -29,9 +29,9 @@ discard block | ||
| 29 | 29 | } | 
| 30 | 30 | fclose($handle); | 
| 31 | 31 | } | 
| 32 | - //Connection::$db->commit(); | |
| 33 | - $Connection->db = null; | |
| 34 | - return ''; | |
| 32 | + //Connection::$db->commit(); | |
| 33 | + $Connection->db = null; | |
| 34 | + return ''; | |
| 35 | 35 | } | 
| 36 | 36 | |
| 37 | 37 |  	public static function import_all_db($directory) { | 
| @@ -40,7 +40,7 @@ discard block | ||
| 40 | 40 | //foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $filename) | 
| 41 | 41 | while(false !== ($filename = readdir($dh))) | 
| 42 | 42 |  		{ | 
| 43 | -		    if (preg_match('/\.sql$/',$filename)) $error .= create_db::import_file($directory.$filename); | |
| 43 | +			if (preg_match('/\.sql$/',$filename)) $error .= create_db::import_file($directory.$filename); | |
| 44 | 44 | } | 
| 45 | 45 | return $error; | 
| 46 | 46 | } | 
| @@ -55,7 +55,7 @@ discard block | ||
| 55 | 55 | $host = filter_var($host,FILTER_SANITIZE_STRING); | 
| 56 | 56 | // Dirty hack | 
| 57 | 57 |  		if ($host != 'localhost' && $host != '127.0.0.1') { | 
| 58 | - $grantright = $_SERVER['SERVER_ADDR']; | |
| 58 | + $grantright = $_SERVER['SERVER_ADDR']; | |
| 59 | 59 | } else $grantright = 'localhost'; | 
| 60 | 60 |  		try { | 
| 61 | 61 | $dbh = new PDO($db_type.':host='.$host,$root,$root_pass); | 
| @@ -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; | 
| @@ -77,7 +77,7 @@ | ||
| 77 | 77 | /** | 
| 78 | 78 | * Returns list of available locales | 
| 79 | 79 | * | 
| 80 | - * @return array | |
| 80 | + * @return string[] | |
| 81 | 81 | */ | 
| 82 | 82 | public function listLocaleDir() | 
| 83 | 83 |  	{ | 
| @@ -1,9 +1,9 @@ discard block | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | 3 |  if (!function_exists("gettext")) { | 
| 4 | -        function _($text) { | |
| 5 | - return $text; | |
| 6 | - } | |
| 4 | +		function _($text) { | |
| 5 | + return $text; | |
| 6 | + } | |
| 7 | 7 |  } else { | 
| 8 | 8 |  	if (isset($_COOKIE['language']) && $_COOKIE['language'] != 'en_GB' && (isset($globalTranslate) && $globalTranslate)) { | 
| 9 | 9 | $Language = new Language(); | 
| @@ -75,9 +75,9 @@ discard block | ||
| 75 | 75 | ); | 
| 76 | 76 | |
| 77 | 77 | /** | 
| 78 | - * Returns list of available locales | |
| 79 | - * | |
| 80 | - * @return array | |
| 78 | + * Returns list of available locales | |
| 79 | + * | |
| 80 | + * @return array | |
| 81 | 81 | */ | 
| 82 | 82 | public function listLocaleDir() | 
| 83 | 83 |  	{ | 
| @@ -103,9 +103,9 @@ discard block | ||
| 103 | 103 | } | 
| 104 | 104 | |
| 105 | 105 | /** | 
| 106 | - * Returns list of available languages | |
| 107 | - * | |
| 108 | - * @return array | |
| 106 | + * Returns list of available languages | |
| 107 | + * | |
| 108 | + * @return array | |
| 109 | 109 | */ | 
| 110 | 110 | public function getLanguages() | 
| 111 | 111 |  	{ | 
| @@ -58,6 +58,10 @@ discard block | ||
| 58 | 58 | } else return $ident; | 
| 59 | 59 | } | 
| 60 | 60 | |
| 61 | + /** | |
| 62 | + * @param string $correct_ident | |
| 63 | + * @param string $source | |
| 64 | + */ | |
| 61 | 65 |         public function addOperator($ident,$correct_ident,$source) { | 
| 62 | 66 | $query = "INSERT INTO translation (Operator,Operator_correct,Source) VALUES (:ident,:correct_ident,:source)"; | 
| 63 | 67 |                  $query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source); | 
| @@ -69,6 +73,10 @@ discard block | ||
| 69 | 73 | } | 
| 70 | 74 | } | 
| 71 | 75 | |
| 76 | + /** | |
| 77 | + * @param string $correct_ident | |
| 78 | + * @param string $source | |
| 79 | + */ | |
| 72 | 80 |         public function updateOperator($ident,$correct_ident,$source) { | 
| 73 | 81 | $query = "UPDATE translation SET Operator_correct = :correct_ident,Source = :source WHERE Operator = :ident"; | 
| 74 | 82 |                  $query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source); | 
| @@ -7,91 +7,91 @@ discard block | ||
| 7 | 7 | |
| 8 | 8 | |
| 9 | 9 |  class Translation { | 
| 10 | - public $db; | |
| 11 | -    public function __construct($dbc = null) { | |
| 12 | - $Connection = new Connection($dbc); | |
| 13 | - $this->db = $Connection->db(); | |
| 14 | - } | |
| 10 | + public $db; | |
| 11 | +	public function __construct($dbc = null) { | |
| 12 | + $Connection = new Connection($dbc); | |
| 13 | + $this->db = $Connection->db(); | |
| 14 | + } | |
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * Change IATA to ICAO value for ident | |
| 18 | - * | |
| 19 | - * @param String $ident ident | |
| 20 | - * @return String the icao | |
| 21 | - */ | |
| 22 | -    public function ident2icao($ident) { | |
| 16 | + /** | |
| 17 | + * Change IATA to ICAO value for ident | |
| 18 | + * | |
| 19 | + * @param String $ident ident | |
| 20 | + * @return String the icao | |
| 21 | + */ | |
| 22 | +	public function ident2icao($ident) { | |
| 23 | 23 | $Spotter = new Spotter(); | 
| 24 | 24 | if (!is_numeric(substr($ident, 0, 3))) | 
| 25 | -        { | |
| 26 | -	    if (is_numeric(substr(substr($ident, 0, 3), -1, 1))) { | |
| 27 | - $airline_icao = substr($ident, 0, 2); | |
| 28 | -            } elseif (is_numeric(substr(substr($ident, 0, 4), -1, 1))) { | |
| 29 | - //$airline_icao = substr($ident, 0, 3); | |
| 30 | - return $ident; | |
| 31 | - } else return $ident; | |
| 32 | - } else return $ident; | |
| 33 | -        if ($airline_icao == 'AF') { | |
| 34 | -            if (filter_var(substr($ident,2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $ident; | |
| 35 | - else $icao = 'AFR'.ltrim(substr($ident,2),'0'); | |
| 36 | -        } else { | |
| 37 | - $identicao = $Spotter->getAllAirlineInfo($airline_icao); | |
| 38 | -            if (isset($identicao[0])) { | |
| 39 | - $icao = $identicao[0]['icao'].ltrim(substr($ident,2),'0'); | |
| 40 | - } else $icao = $ident; | |
| 41 | - } | |
| 42 | - return $icao; | |
| 43 | - } | |
| 25 | +		{ | |
| 26 | +		if (is_numeric(substr(substr($ident, 0, 3), -1, 1))) { | |
| 27 | + $airline_icao = substr($ident, 0, 2); | |
| 28 | +			} elseif (is_numeric(substr(substr($ident, 0, 4), -1, 1))) { | |
| 29 | + //$airline_icao = substr($ident, 0, 3); | |
| 30 | + return $ident; | |
| 31 | + } else return $ident; | |
| 32 | + } else return $ident; | |
| 33 | +		if ($airline_icao == 'AF') { | |
| 34 | +			if (filter_var(substr($ident,2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $ident; | |
| 35 | + else $icao = 'AFR'.ltrim(substr($ident,2),'0'); | |
| 36 | +		} else { | |
| 37 | + $identicao = $Spotter->getAllAirlineInfo($airline_icao); | |
| 38 | +			if (isset($identicao[0])) { | |
| 39 | + $icao = $identicao[0]['icao'].ltrim(substr($ident,2),'0'); | |
| 40 | + } else $icao = $ident; | |
| 41 | + } | |
| 42 | + return $icao; | |
| 43 | + } | |
| 44 | 44 | |
| 45 | 45 | |
| 46 | -       public function getOperator($ident) { | |
| 47 | - $query = "SELECT * FROM translation WHERE Operator = :ident LIMIT 1"; | |
| 48 | -                $query_values = array(':ident' => $ident); | |
| 49 | -                 try { | |
| 50 | - $sth = $this->db->prepare($query); | |
| 51 | - $sth->execute($query_values); | |
| 52 | -                } catch(PDOException $e) { | |
| 53 | - return "error : ".$e->getMessage(); | |
| 54 | - } | |
| 55 | - $row = $sth->fetch(PDO::FETCH_ASSOC); | |
| 56 | - $sth->closeCursor(); | |
| 57 | -                if (count($row) > 0) { | |
| 58 | - return $row['operator_correct']; | |
| 59 | - } else return $ident; | |
| 60 | - } | |
| 46 | +	   public function getOperator($ident) { | |
| 47 | + $query = "SELECT * FROM translation WHERE Operator = :ident LIMIT 1"; | |
| 48 | +				$query_values = array(':ident' => $ident); | |
| 49 | +				 try { | |
| 50 | + $sth = $this->db->prepare($query); | |
| 51 | + $sth->execute($query_values); | |
| 52 | +				} catch(PDOException $e) { | |
| 53 | + return "error : ".$e->getMessage(); | |
| 54 | + } | |
| 55 | + $row = $sth->fetch(PDO::FETCH_ASSOC); | |
| 56 | + $sth->closeCursor(); | |
| 57 | +				if (count($row) > 0) { | |
| 58 | + return $row['operator_correct']; | |
| 59 | + } else return $ident; | |
| 60 | + } | |
| 61 | 61 | |
| 62 | -       public function addOperator($ident,$correct_ident,$source) { | |
| 63 | - $query = "INSERT INTO translation (Operator,Operator_correct,Source) VALUES (:ident,:correct_ident,:source)"; | |
| 64 | -                $query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source); | |
| 65 | -                 try { | |
| 66 | - $sth = $this->db->prepare($query); | |
| 67 | - $sth->execute($query_values); | |
| 68 | -                } catch(PDOException $e) { | |
| 69 | - return "error : ".$e->getMessage(); | |
| 70 | - } | |
| 71 | - } | |
| 62 | +	   public function addOperator($ident,$correct_ident,$source) { | |
| 63 | + $query = "INSERT INTO translation (Operator,Operator_correct,Source) VALUES (:ident,:correct_ident,:source)"; | |
| 64 | +				$query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source); | |
| 65 | +				 try { | |
| 66 | + $sth = $this->db->prepare($query); | |
| 67 | + $sth->execute($query_values); | |
| 68 | +				} catch(PDOException $e) { | |
| 69 | + return "error : ".$e->getMessage(); | |
| 70 | + } | |
| 71 | + } | |
| 72 | 72 | |
| 73 | -       public function updateOperator($ident,$correct_ident,$source) { | |
| 74 | - $query = "UPDATE translation SET Operator_correct = :correct_ident,Source = :source WHERE Operator = :ident"; | |
| 75 | -                $query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source); | |
| 76 | -                 try { | |
| 77 | - $sth = $this->db->prepare($query); | |
| 78 | - $sth->execute($query_values); | |
| 79 | -                } catch(PDOException $e) { | |
| 80 | - return "error : ".$e->getMessage(); | |
| 81 | - } | |
| 82 | - } | |
| 73 | +	   public function updateOperator($ident,$correct_ident,$source) { | |
| 74 | + $query = "UPDATE translation SET Operator_correct = :correct_ident,Source = :source WHERE Operator = :ident"; | |
| 75 | +				$query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source); | |
| 76 | +				 try { | |
| 77 | + $sth = $this->db->prepare($query); | |
| 78 | + $sth->execute($query_values); | |
| 79 | +				} catch(PDOException $e) { | |
| 80 | + return "error : ".$e->getMessage(); | |
| 81 | + } | |
| 82 | + } | |
| 83 | 83 | |
| 84 | -        public function checkTranslation($ident,$web = false) { | |
| 85 | - global $globalTranslationSources, $globalTranslationFetch; | |
| 86 | -    	    //if (!isset($globalTranslationSources)) $globalTranslationSources = array('planefinder'); | |
| 87 | - $globalTranslationSources = array(); | |
| 88 | - if (!isset($globalTranslationFetch)) $globalTranslationFetch = TRUE; | |
| 89 | - //echo "Check Translation for ".$ident."..."; | |
| 90 | - $correct = $this->getOperator($ident); | |
| 91 | -    	    if ($correct != '' && $correct != $ident) { | |
| 92 | - //echo "Found in DB !\n"; | |
| 93 | - return $correct; | |
| 94 | - } /* | |
| 84 | +		public function checkTranslation($ident,$web = false) { | |
| 85 | + global $globalTranslationSources, $globalTranslationFetch; | |
| 86 | +			//if (!isset($globalTranslationSources)) $globalTranslationSources = array('planefinder'); | |
| 87 | + $globalTranslationSources = array(); | |
| 88 | + if (!isset($globalTranslationFetch)) $globalTranslationFetch = TRUE; | |
| 89 | + //echo "Check Translation for ".$ident."..."; | |
| 90 | + $correct = $this->getOperator($ident); | |
| 91 | +			if ($correct != '' && $correct != $ident) { | |
| 92 | + //echo "Found in DB !\n"; | |
| 93 | + return $correct; | |
| 94 | + } /* | |
| 95 | 95 |      	    elseif ($web && $globalTranslationFetch) { | 
| 96 | 96 |      		if (! is_numeric(substr($ident,-4))) { | 
| 97 | 97 |      		    if (count($globalTranslationSources) > 0) { | 
| @@ -108,8 +108,8 @@ discard block | ||
| 108 | 108 | } | 
| 109 | 109 | } | 
| 110 | 110 | */ | 
| 111 | - return $this->ident2icao($ident); | |
| 112 | - } | |
| 111 | + return $this->ident2icao($ident); | |
| 112 | + } | |
| 113 | 113 | |
| 114 | 114 | /* | 
| 115 | 115 |      function fromPlanefinder($icao) { | 
| @@ -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 | } | 
| @@ -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 | |
| @@ -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 | } | 
| @@ -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 | } | 
| @@ -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 | } |