diff --git a/lib/adapters/GPX.class.php b/lib/adapters/GPX.class.php index 9ca18b8e..06250d2b 100644 --- a/lib/adapters/GPX.class.php +++ b/lib/adapters/GPX.class.php @@ -37,23 +37,23 @@ public function write(Geometry $geometry, $namespace = FALSE) { if ($geometry->isEmpty()) return NULL; if ($namespace) { $this->namespace = $namespace; - $this->nss = $namespace.':'; + $this->nss = $namespace.':'; } return '<'.$this->nss.'gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'nss.'gpx>'; } - + public function geomFromText($text) { // Change to lower-case and strip all CDATA $text = strtolower($text); $text = preg_replace('//s','',$text); - + // Load into DOMDocument $xmlobj = new DOMDocument(); @$xmlobj->loadXML($text); if ($xmlobj === false) { throw new Exception("Invalid GPX: ". $text); } - + $this->xmlobj = $xmlobj; try { $geom = $this->geomFromXML(); @@ -65,20 +65,28 @@ public function geomFromText($text) { return $geom; } - + protected function geomFromXML() { $geometries = array(); $geometries = array_merge($geometries, $this->parseWaypoints()); $geometries = array_merge($geometries, $this->parseTracks()); $geometries = array_merge($geometries, $this->parseRoutes()); - + if (empty($geometries)) { throw new Exception("Invalid / Empty GPX"); } - - return geoPHP::geometryReduce($geometries); + + return geoPHP::geometryReduce($geometries); } - + + protected function getElementMetadata($element) { + $metadata = array(); + foreach ($element->childNodes as $child) { + $metadata[$child->nodeName] = $child->nodeValue; + } + return $metadata; + } + protected function childElements($xml, $nodename = '') { $children = array(); foreach ($xml->childNodes as $child) { @@ -88,18 +96,19 @@ protected function childElements($xml, $nodename = '') { } return $children; } - + protected function parseWaypoints() { $points = array(); $wpt_elements = $this->xmlobj->getElementsByTagName('wpt'); foreach ($wpt_elements as $wpt) { $lat = $wpt->attributes->getNamedItem("lat")->nodeValue; $lon = $wpt->attributes->getNamedItem("lon")->nodeValue; - $points[] = new Point($lon, $lat); + $metadata = $this->getElementMetadata($wpt); + $points[] = new Point($lon, $lat, null, $metadata); } return $points; } - + protected function parseTracks() { $lines = array(); $trk_elements = $this->xmlobj->getElementsByTagName('trk'); @@ -116,7 +125,7 @@ protected function parseTracks() { } return $lines; } - + protected function parseRoutes() { $lines = array(); $rte_elements = $this->xmlobj->getElementsByTagName('rte'); @@ -131,7 +140,7 @@ protected function parseRoutes() { } return $lines; } - + protected function geometryToGPX($geom) { $type = strtolower($geom->getGeomType()); switch ($type) { @@ -150,30 +159,30 @@ protected function geometryToGPX($geom) { break; } } - + private function pointToGPX($geom) { return '<'.$this->nss.'wpt lat="'.$geom->getY().'" lon="'.$geom->getX().'" />'; } - + private function linestringToGPX($geom) { $gpx = '<'.$this->nss.'trk><'.$this->nss.'trkseg>'; - + foreach ($geom->getComponents() as $comp) { $gpx .= '<'.$this->nss.'trkpt lat="'.$comp->getY().'" lon="'.$comp->getX().'" />'; } - + $gpx .= 'nss.'trkseg>nss.'trk>'; - + return $gpx; } - + public function collectionToGPX($geom) { $gpx = ''; $components = $geom->getComponents(); foreach ($geom->getComponents() as $comp) { $gpx .= $this->geometryToGPX($comp); } - + return $gpx; } diff --git a/lib/geometry/Point.class.php b/lib/geometry/Point.class.php index f49771e7..25d33fa4 100644 --- a/lib/geometry/Point.class.php +++ b/lib/geometry/Point.class.php @@ -17,7 +17,7 @@ class Point extends Geometry * @param numeric $y The y coordinate (or latitude) * @param numeric $z The z coordinate (or altitude) - optional */ - public function __construct($x, $y, $z = NULL) { + public function __construct($x, $y, $z = NULL, $metadata = array()) { // Basic validation on x and y if (!is_numeric($x) || !is_numeric($y)) { throw new Exception("Cannot construct Point. x and y should be numeric"); @@ -43,6 +43,7 @@ public function __construct($x, $y, $z = NULL) { if ($this->dimention == 3) { $this->coords = array($x, $y, $z); } + $this->metadata = $metadata; } /** @@ -152,4 +153,3 @@ public function interiorRingN($n) { return NULL; } public function pointOnSurface() { return NULL; } public function explode() { return NULL; } } -