Skip to content

Commit

Permalink
Adding namespace write support
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed May 5, 2012
1 parent aee5fdc commit cb0ad03
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/adapters/GPX.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class GPX extends GeoAdapter
{
private $namespace = FALSE;
private $nss = ''; // Name-space string. eg 'georss:'

/**
* Read KML string into geometry objects
Expand All @@ -31,9 +33,13 @@ public function read($gpx) {
*
* @return string The GPX string representation of the input geometries
*/
public function write(Geometry $geometry) {
public function write(Geometry $geometry, $namespace = FALSE) {
if ($geometry->isEmpty()) return NULL;
return '<gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'</gpx>';
if ($namespace) {
$this->namespace = $namespace;
$this->nss = $namespace.':';
}
return '<'.$this->nss.'gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'</'.$this->nss.'gpx>';
}

public function geomFromText($text) {
Expand Down Expand Up @@ -146,17 +152,17 @@ protected function geometryToGPX($geom) {
}

private function pointToGPX($geom) {
return '<wpt lat="'.$geom->getY().'" lon="'.$geom->getX().'"></wpt>';
return '<'.$this->nss.'wpt lat="'.$geom->getY().'" lon="'.$geom->getX().'" />';
}

private function linestringToGPX($geom) {
$gpx = '<trk><trkseg>';
$gpx = '<'.$this->nss.'trk><'.$this->nss.'trkseg>';

foreach ($geom->getComponents() as $comp) {
$gpx .= '<trkpt lat="'.$comp->getY().'" lon="'.$comp->getX().'"></trkpt>';
$gpx .= '<'.$this->nss.'trkpt lat="'.$comp->getY().'" lon="'.$comp->getX().'" />';
}

$gpx .= '</trkseg></trk>';
$gpx .= '</'.$this->nss.'trkseg></'.$this->nss.'trk>';

return $gpx;
}
Expand Down

0 comments on commit cb0ad03

Please sign in to comment.