From 49c4416c8c83fb513a5f482cde73869c2e5b5267 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Sat, 5 May 2012 13:03:21 -0700 Subject: [PATCH] Adding namespace suppor to KML --- lib/adapters/KML.class.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/adapters/KML.class.php b/lib/adapters/KML.class.php index 2130f638..4d76d767 100644 --- a/lib/adapters/KML.class.php +++ b/lib/adapters/KML.class.php @@ -20,7 +20,9 @@ */ class KML extends GeoAdapter { - + private $namespace = FALSE; + private $nss = ''; // Name-space string. eg 'georss:' + /** * Read KML string into geometry objects * @@ -39,7 +41,11 @@ public function read($kml) { * * @return string The KML string representation of the input geometries */ - public function write(Geometry $geometry) { + public function write(Geometry $geometry, $namespace = FALSE) { + if ($namespace) { + $this->namespace = $namespace; + $this->nss = $namespace.':'; + } return $this->geometryToKML($geometry); } @@ -199,7 +205,7 @@ private function geometryToKML($geom) { } private function pointToKML($geom) { - return "".$geom->getX().",".$geom->getY().""; + return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'nss.'coordinates>nss.'Point>'; } private function linestringToKML($geom, $type = FALSE) { @@ -207,7 +213,7 @@ private function linestringToKML($geom, $type = FALSE) { $type = $geom->getGeomType(); } - $str = '<'. $type .'>'; + $str = '<'.$this->nss . $type .'><'.$this->nss.'coordinates>'; $i=0; foreach ($geom->getComponents() as $comp) { if ($i != 0) $str .= ' '; @@ -215,28 +221,28 @@ private function linestringToKML($geom, $type = FALSE) { $i++; } - return $str .''; + return $str .'nss.'coordinates>nss . $type .'>'; } public function polygonToKML($geom) { $components = $geom->getComponents(); - $str = '' . $this->linestringToKML($components[0], 'LinearRing') . ''; + $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . 'nss.'outerBoundaryIs>'; foreach (array_slice($components, 1) as $comp) { - $str .= '' . $this->linestringToKML($comp) . ''; + $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . 'nss.'innerBoundaryIs>'; } - return ''. $str .''; + return '<'.$this->nss.'Polygon>'. $str .'nss.'Polygon>'; } public function collectionToKML($geom) { $components = $geom->getComponents(); - $str = ''; + $str = '<'.$this->nss.'MultiGeometry>'; foreach ($geom->getComponents() as $comp) { $sub_adapter = new KML(); $str .= $sub_adapter->write($comp); } - return $str .''; + return $str .'nss.'MultiGeometry>'; } }