Skip to content

Commit

Permalink
Adding namespace suppor to KML
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed May 5, 2012
1 parent cb0ad03 commit 49c4416
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/adapters/KML.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
*/
class KML extends GeoAdapter
{

private $namespace = FALSE;
private $nss = ''; // Name-space string. eg 'georss:'

/**
* Read KML string into geometry objects
*
Expand All @@ -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);
}

Expand Down Expand Up @@ -199,44 +205,44 @@ private function geometryToKML($geom) {
}

private function pointToKML($geom) {
return "<Point><coordinates>".$geom->getX().",".$geom->getY()."</coordinates></Point>";
return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>';
}

private function linestringToKML($geom, $type = FALSE) {
if (!$type) {
$type = $geom->getGeomType();
}

$str = '<'. $type .'><coordinates>';
$str = '<'.$this->nss . $type .'><'.$this->nss.'coordinates>';
$i=0;
foreach ($geom->getComponents() as $comp) {
if ($i != 0) $str .= ' ';
$str .= $comp->getX() .','. $comp->getY();
$i++;
}

return $str .'</coordinates></'. $type .'>';
return $str .'</'.$this->nss.'coordinates></'. $this->nss . $type .'>';
}

public function polygonToKML($geom) {
$components = $geom->getComponents();
$str = '<outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</outerBoundaryIs>';
$str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>';
foreach (array_slice($components, 1) as $comp) {
$str .= '<innerBoundaryIs>' . $this->linestringToKML($comp) . '</innerBoundaryIs>';
$str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>';
}

return '<Polygon>'. $str .'</Polygon>';
return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>';
}

public function collectionToKML($geom) {
$components = $geom->getComponents();
$str = '<MultiGeometry>';
$str = '<'.$this->nss.'MultiGeometry>';
foreach ($geom->getComponents() as $comp) {
$sub_adapter = new KML();
$str .= $sub_adapter->write($comp);
}

return $str .'</MultiGeometry>';
return $str .'</'.$this->nss.'MultiGeometry>';
}

}

0 comments on commit 49c4416

Please sign in to comment.