1
0

Tidy up Gpx creation into own class. Make download work properly.

This commit is contained in:
Markus Birth 2018-10-09 01:21:14 +02:00
parent bf3c2a7d5a
commit 3e78595917
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
2 changed files with 80 additions and 35 deletions

View File

@ -9,6 +9,7 @@ require_once 'vendor/autoload.php';
use \OwntracksRecorder\Database\MySql;
use \OwntracksRecorder\Database\SQLite;
use \OwntracksRecorder\RecordType\Location;
use \OwntracksRecorder\Gpx;
use \OwntracksRecorder\Rpc;
$response = array();
@ -27,35 +28,9 @@ $rpc = new Rpc($sql);
$markers = $rpc->getMarkers();
#header("Content-type: application/javascript");
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$gpx = $dom->createElement('gpx');
$gpx->setAttribute('creator', 'php-owntracks-recorder');
$gpx->setAttribute('version', '1.1');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.topografix.com/GPX/1/1');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns2', 'http://www.garmin.com/xmlschemas/GpxExtensions/v3');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns3', 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1');
$gpx->setAttribute('xsi:schemaLocation', 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd');
# METADATA
$meta = $dom->createElement('metadata');
$link = $dom->createElement('link');
$link->setAttribute('href', 'github.com');
$link->appendChild($dom->createElement('text', 'php-owntracks-recorder'));
$meta->appendChild($link);
$meta->appendChild($dom->createElement('time', date('c')));
$gpx->appendChild($meta);
# TRACK INFO
$trk = $dom->createElement('trk');
$trk->appendChild($dom->createElement('name', 'Exported Track'));
$trk->appendChild($dom->createElement('type', 'other'));
# TRACK SEGMENT
$trkseg = $dom->createElement('trkseg');
$gpx = new Gpx();
$gpx->addLink('github.com', 'php-owntracks-recorder');
$gpx->addTrack('Exported Track', 'other');
foreach ($markers['markers'] as $tid => $markerList) {
foreach ($markerList as $marker) {
@ -64,13 +39,11 @@ foreach ($markers['markers'] as $tid => $markerList) {
$trkpt = $lo->getGpxDom();
$trkpti = $dom->importNode($trkpt->documentElement, true);
$trkseg->appendChild($trkpti);
$gpx->addPoint($trkpt->documentElement);
}
}
$trk->appendChild($trkseg);
$gpx->appendChild($trk);
$dom->appendChild($gpx);
header('Content-type: application/gpx+xml');
header('Content-Disposition: attachment; filename=export.gpx');
echo $dom->saveXML();
echo $gpx->getXml();

72
lib/Gpx.php Normal file
View File

@ -0,0 +1,72 @@
<?php
namespace OwntracksRecorder;
class Gpx
{
private $dom;
private $root;
private $meta;
private $trk;
private $trkseg;
public function __construct()
{
$this->dom = new \DOMDocument('1.0', 'utf-8');
$this->dom->formatOutput = true;
$gpx = $this->dom->createElement('gpx');
$gpx->setAttribute('creator', 'php-owntracks-recorder');
$gpx->setAttribute('version', '1.1');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.topografix.com/GPX/1/1');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns2', 'http://www.garmin.com/xmlschemas/GpxExtensions/v3');
$gpx->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns3', 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1');
$gpx->setAttribute('xsi:schemaLocation', 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd');
$this->root = $gpx;
$this->dom->appendChild($this->root);
}
private function addMeta($domNode)
{
if (is_null($this->meta)) {
$this->meta = $this->dom->createElement('metadata');
$this->meta->appendChild($this->dom->createElement('time', date('c')));
$this->root->appendChild($this->meta);
}
$this->meta->appendChild($domNode);
}
public function addLink($url, $title)
{
$link = $this->dom->createElement('link');
$link->setAttribute('href', $url);
$link->appendChild($this->dom->createElement('text', $title));
$this->addMeta($link);
}
public function addTrack($title, $type)
{
$this->trk = $this->dom->createElement('trk');
$this->trk->appendChild($this->dom->createElement('name', $title));
$this->trk->appendChild($this->dom->createElement('type', $type));
$this->root->appendChild($this->trk);
}
public function addPoint(\DOMNode $domNode)
{
if (is_null($this->trk)) {
$this->addTrack('Unknown', 'other');
}
if (is_null($this->trkseg)) {
$this->trkseg = $this->dom->createElement('trkseg');
$this->trk->appendChild($this->trkseg);
}
$trkpti = $this->dom->importNode($domNode, true);
$this->trkseg->appendChild($trkpti);
}
public function getXml()
{
return $this->dom->saveXML();
}
}