Skip to content

Commit

Permalink
Adding GPX adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Apr 13, 2011
1 parent 7e0da7a commit 39d06b5
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 3 deletions.
13 changes: 10 additions & 3 deletions geoPHP.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
* file that was distributed with this source code.
*/

// Geometry Loader (Depreciated)
include_once("lib/GeometryLoader.class.php");
include_once("lib/adapters/GeoAdapter.class.php");

// Adapters
include_once("lib/adapters/GeoAdapter.class.php"); // Abtract class
include_once("lib/adapters/GeoJSON.class.php");
include_once("lib/adapters/WKT.class.php");
include_once("lib/adapters/KML.class.php");
include_once("lib/adapters/GPX.class.php");
include_once("lib/adapters/GoogleGeocode.class.php");
include_once("lib/geometry/Geometry.class.php");

// Geometries
include_once("lib/geometry/Geometry.class.php"); // Abtract class
include_once("lib/geometry/Point.class.php");
include_once("lib/geometry/Collection.class.php");
include_once("lib/geometry/Collection.class.php"); // Abtract class
include_once("lib/geometry/LineString.class.php");
include_once("lib/geometry/MultiPoint.class.php");
include_once("lib/geometry/LinearRing.class.php");
Expand Down Expand Up @@ -61,6 +67,7 @@ class geoPHP {
'wkt' => 'WKT',
'json' => 'GeoJSON',
'kml' => 'KML',
'gpx' => 'GPX',
'google_geocode' => 'GoogleGeocode',
);
}
Expand Down
176 changes: 176 additions & 0 deletions lib/adapters/GPX.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
/*
* Copyright (c) Patrick Hayes
*
* This code is open-source and licenced under the Modified BSD License.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* PHP Geometry/GPX encoder/decoder
*/
class GPX extends GeoAdapter
{

/**
* Read KML string into geometry objects
*
* @param string $kml A KML string
*
* @return Geometry|GeometryCollection
*/
public function read($gpx)
{
return $this->geomFromText($gpx);
}

/**
* Serialize geometries into a KML string.
*
* @param Geometry $geometry
*
* @return string The KML string representation of the input geometries
*/
public function write(Geometry $geometry)
{
return $this->geometryToGPX($geometry);
}

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();
} catch(InvalidText $e) {
throw new Exception("Cannot Read Geometry From GPX: ". $text);
} catch(Exception $e) {
throw $e;
}

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);
}

protected function childElements($xml, $nodename = '') {
$children = array();
foreach ($xml->childNodes as $child) {
if ($child->nodeName == $nodename) {
$children[] = $child;
}
}
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);
}
return $points;
}

protected function parseTracks() {
$lines = array();
$trk_elements = $this->xmlobj->getElementsByTagName('trk');
foreach ($trk_elements as $trk) {
$components = array();
foreach ($this->childElements($trk, 'trkseg') as $trkseg) {
foreach ($this->childElements($trkseg, 'trkpt') as $trkpt) {
$lat = $trkpt->attributes->getNamedItem("lat")->nodeValue;
$lon = $trkpt->attributes->getNamedItem("lon")->nodeValue;
$components[] = new Point($lon, $lat);
}
}
$lines[] = new LineString($components);
}
return $lines;
}

protected function parseRoutes() {
$lines = array();
$rte_elements = $this->xmlobj->getElementsByTagName('rte');
foreach ($rte_elements as $rte) {
$components = array();
foreach ($this->childElements($rte, 'rtept') as $rtept) {
$lat = $rtept->attributes->getNamedItem("lat")->nodeValue;
$lon = $rtept->attributes->getNamedItem("lon")->nodeValue;
$components[] = new Point($lon, $lat);
}
$lines[] = new LineString($components);
}
return $lines;
}

private function geometryToGPX($geom) {
$type = strtolower($geom->getGeomType());
switch ($type) {
case 'point':
return $this->pointToGPX($geom);
break;
case 'linestring':
case 'linearring':
return $this->linestringToGPX($geom);
break;
case 'polygon':
case 'multipoint':
case 'multilinestring':
case 'multipolygon':
case 'geometrycollection':
return $this->collectionToGPX($geom);
break;
}
}

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

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

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

$gpx .= '</trkseg></trk>';

return $gpx;
}

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

return $gpx;
}

}

0 comments on commit 39d06b5

Please sign in to comment.