GPX to GeoJson converter
This commit is contained in:
File diff suppressed because one or more lines are too long
77027
geo/hrp.gpx
Normal file
77027
geo/hrp.gpx
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
153
geo/te_araroa.geojson.bk
Normal file
153
geo/te_araroa.geojson.bk
Normal file
File diff suppressed because one or more lines are too long
159414
geo/te_araroa.gpx
Normal file
159414
geo/te_araroa.gpx
Normal file
File diff suppressed because it is too large
Load Diff
130
inc/converter.php
Normal file
130
inc/converter.php
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Converter extends PhpObject {
|
||||||
|
|
||||||
|
const GPX_EXT = '.gpx';
|
||||||
|
const GEO_EXT = '.geojson';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project to convert
|
||||||
|
* @var Project
|
||||||
|
*/
|
||||||
|
private $oProject;
|
||||||
|
|
||||||
|
public function __construct(&$oProject) {
|
||||||
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||||
|
$this->oProject = &$oProject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function convertToGeoJson() {
|
||||||
|
$sFileName = pathinfo($this->oProject->getGeoFile(), PATHINFO_FILENAME);
|
||||||
|
$sGpxFileName = $sFileName.self::GPX_EXT;
|
||||||
|
$sGeoJsonFileName = $sFileName.self::GEO_EXT;
|
||||||
|
|
||||||
|
$oGpx = new Gpx($sGpxFileName);
|
||||||
|
$oGeoJson = new GeoJson($sGeoJsonFileName);
|
||||||
|
|
||||||
|
$oGeoJson->setTracks($oGpx->getTracks());
|
||||||
|
$oGeoJson->saveFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Geo extends PhpObject {
|
||||||
|
|
||||||
|
const GEO_FOLDER = 'geo/';
|
||||||
|
|
||||||
|
protected $asTracks;
|
||||||
|
protected $sFilePath;
|
||||||
|
|
||||||
|
public function __construct($sFileName) {
|
||||||
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||||
|
$this->sFilePath = self::GEO_FOLDER.$sFileName;
|
||||||
|
$this->asTracks = array();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Gpx extends Geo {
|
||||||
|
|
||||||
|
public function __construct($sFileName) {
|
||||||
|
parent::__construct($sFileName);
|
||||||
|
$this->parseFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTracks() {
|
||||||
|
return $this->asTracks;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseFile() {
|
||||||
|
$oXml = simplexml_load_file($this->sFilePath);
|
||||||
|
foreach($oXml->trk as $aoTrack) {
|
||||||
|
$asTrack = array(
|
||||||
|
'name' => (string) $aoTrack->name,
|
||||||
|
'desc' => str_replace("\n", '', ToolBox::fixEOL((strip_tags($aoTrack->desc)))),
|
||||||
|
'color' => (string) $aoTrack->extensions->children('gpxx', true)->TrackExtension->DisplayColor,
|
||||||
|
'points'=> array()
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($aoTrack->trkseg as $asSegment) {
|
||||||
|
foreach($asSegment as $asPoint) {
|
||||||
|
$asTrack['points'][] = array(
|
||||||
|
'lon' => (float) $asPoint['lon'],
|
||||||
|
'lat' => (float) $asPoint['lat'],
|
||||||
|
//'ele' => (int) $asPoint->ele
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->asTracks[] = $asTrack;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class GeoJson extends Geo {
|
||||||
|
|
||||||
|
public function __construct($sFileName) {
|
||||||
|
parent::__construct($sFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTracks($asTracks) {
|
||||||
|
$this->asTracks = $asTracks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveFile() {
|
||||||
|
$sContent = json_encode($this->getGeoJson());
|
||||||
|
//$sContent = str_replace('{"type":"Feature"', "\n\t{\"type\":\"Feature\"", $sContent);
|
||||||
|
file_put_contents($this->sFilePath, $sContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getGeoJson() {
|
||||||
|
$asTracks = array();
|
||||||
|
foreach($this->asTracks as $asTrackProps) {
|
||||||
|
|
||||||
|
switch($asTrackProps['color']) {
|
||||||
|
case 'LightGray': continue 2;
|
||||||
|
case 'Magenta': $sType = 'off-track'; break;
|
||||||
|
case 'Red': $sType = 'hitchhiking'; break;
|
||||||
|
default: $sType = 'main'; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$asTrack = array(
|
||||||
|
'type' => 'Feature',
|
||||||
|
'properties' => array(
|
||||||
|
'name' => $asTrackProps['name'],
|
||||||
|
'type' => $sType,
|
||||||
|
'description' => $asTrackProps['desc']
|
||||||
|
),
|
||||||
|
'geometry' => array(
|
||||||
|
'type' => 'MultiLineString',
|
||||||
|
'coordinates' => array()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($asTrackProps['points'] as $asPoint) {
|
||||||
|
$asTrack['geometry']['coordinates'][0][] = array_values($asPoint);
|
||||||
|
}
|
||||||
|
$asTracks[] = $asTrack;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $asTracks;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,6 +72,10 @@ class Project extends PhpObject {
|
|||||||
return $this->asGeo['timezone'];
|
return $this->asGeo['timezone'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getGeoFile() {
|
||||||
|
return $this->asGeo['file'];
|
||||||
|
}
|
||||||
|
|
||||||
public function getProjects($iProjectId=0) {
|
public function getProjects($iProjectId=0) {
|
||||||
$bSpecificProj = ($iProjectId > 0);
|
$bSpecificProj = ($iProjectId > 0);
|
||||||
$asInfo = array(
|
$asInfo = array(
|
||||||
|
|||||||
@@ -454,6 +454,12 @@ class Spot extends Main
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function convertGpxToGeoJson() {
|
||||||
|
$this->oClassManagement->incClass('converter', true);
|
||||||
|
$oConverter = new Converter($this->oProject);
|
||||||
|
return $oConverter->convertToGeoJson();
|
||||||
|
}
|
||||||
|
|
||||||
public static function DecToDMS($dValue, $sType='lat') {
|
public static function DecToDMS($dValue, $sType='lat') {
|
||||||
if($sType=='lat') $sDirection = ($dValue >= 0)?'N':'S';
|
if($sType=='lat') $sDirection = ($dValue >= 0)?'N':'S';
|
||||||
else $sDirection = ($dValue >= 0)?'E':'W';
|
else $sDirection = ($dValue >= 0)?'E':'W';
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ if($sAction!='')
|
|||||||
case 'sync_pics':
|
case 'sync_pics':
|
||||||
$sResult = $oSpot->syncPics();
|
$sResult = $oSpot->syncPics();
|
||||||
break;
|
break;
|
||||||
|
case 'convert':
|
||||||
|
$sResult = $oSpot->convertGpxToGeoJson();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
$sResult = Spot::getJsonResult(false, Spot::NOT_FOUND);
|
$sResult = Spot::getJsonResult(false, Spot::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user