Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support to metadata on waypoints from a GPX file. #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions lib/adapters/GPX.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).'</'.$this->nss.'gpx>';
}

public function geomFromText($text) {
// Change to lower-case and strip all CDATA
$text = strtolower($text);
$text = preg_replace('/<!\[cdata\[(.*?)\]\]>/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();
Expand All @@ -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) {
Expand All @@ -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');
Expand All @@ -116,7 +125,7 @@ protected function parseTracks() {
}
return $lines;
}

protected function parseRoutes() {
$lines = array();
$rte_elements = $this->xmlobj->getElementsByTagName('rte');
Expand All @@ -131,7 +140,7 @@ protected function parseRoutes() {
}
return $lines;
}

protected function geometryToGPX($geom) {
$type = strtolower($geom->getGeomType());
switch ($type) {
Expand All @@ -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 .= '</'.$this->nss.'trkseg></'.$this->nss.'trk>';

return $gpx;
}

public function collectionToGPX($geom) {
$gpx = '';
$components = $geom->getComponents();
foreach ($geom->getComponents() as $comp) {
$gpx .= $this->geometryToGPX($comp);
}

return $gpx;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/geometry/Point.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -43,6 +43,7 @@ public function __construct($x, $y, $z = NULL) {
if ($this->dimention == 3) {
$this->coords = array($x, $y, $z);
}
$this->metadata = $metadata;
}

/**
Expand Down Expand Up @@ -152,4 +153,3 @@ public function interiorRingN($n) { return NULL; }
public function pointOnSurface() { return NULL; }
public function explode() { return NULL; }
}