Add PCT tracks and update converter to include GPX simplication
This commit is contained in:
107136
geo/pct.gpx
Normal file
107136
geo/pct.gpx
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* GPX to GeoJSON Converter
|
||||||
|
*
|
||||||
* To convert a gpx file:
|
* To convert a gpx file:
|
||||||
* 1. Add file <file_name>.gpx to geo/ folder
|
* 1. Add file <file_name>.gpx to geo/ folder
|
||||||
* 2. Assign file to project: UPDATE projects SET geofile = '<file_name>' WHERE id_project = <id_project>;
|
* 2. Assign file to project: UPDATE projects SET geofile = '<file_name>' WHERE id_project = <id_project>;
|
||||||
|
* 3. Load any page
|
||||||
|
*
|
||||||
|
* To force gpx rebuild:
|
||||||
|
* ?a=build_geojson&name=<gpx_file_name>
|
||||||
*/
|
*/
|
||||||
class Converter extends PhpObject {
|
class Converter extends PhpObject {
|
||||||
|
|
||||||
@@ -11,20 +17,23 @@ class Converter extends PhpObject {
|
|||||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function convertToGeoJson($sFileName) {
|
public static function convertToGeoJson($sFileName) {
|
||||||
|
$sFileName = basename($sFileName, Gpx::EXT);
|
||||||
|
|
||||||
$oGpx = new Gpx($sFileName);
|
$oGpx = new Gpx($sFileName);
|
||||||
$oGeoJson = new GeoJson($sFileName);
|
$oGeoJson = new GeoJson($sFileName);
|
||||||
|
|
||||||
$oGeoJson->setTracks($oGpx->getTracks());
|
$oGeoJson->buildTracks($oGpx->getTracks());
|
||||||
|
if($oGeoJson->isSimplicationRequired()) $oGeoJson->buildTracks($oGpx->getTracks(), true);
|
||||||
$oGeoJson->saveFile();
|
$oGeoJson->saveFile();
|
||||||
|
|
||||||
|
return $oGpx->getLog().'<br />'.$oGeoJson->getLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isGeoJsonValid($sFileName) {
|
public static function isGeoJsonValid($sFileName) {
|
||||||
$bResult = false;
|
$bResult = false;
|
||||||
$sGeoJsonFilePath = Geo::getFilePath($sFileName, GeoJson::EXT);
|
$sGeoJsonFilePath = Geo::getFilePath($sFileName, GeoJson::EXT);
|
||||||
if(file_exists($sGeoJsonFilePath)) {
|
if(file_exists($sGeoJsonFilePath) && filemtime($sGeoJsonFilePath) > filemtime(Geo::getFilePath($sFileName, Gpx::EXT))) $bResult = true;
|
||||||
if(filemtime($sGeoJsonFilePath) > filemtime(Geo::getFilePath($sFileName, Gpx::EXT))) $bResult = true;
|
|
||||||
}
|
|
||||||
return $bResult;
|
return $bResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,7 +46,7 @@ class Geo extends PhpObject {
|
|||||||
protected $sFilePath;
|
protected $sFilePath;
|
||||||
|
|
||||||
public function __construct($sFileName) {
|
public function __construct($sFileName) {
|
||||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
parent::__construct(get_class($this), Settings::DEBUG, PhpObject::MODE_HTML);
|
||||||
$this->sFilePath = self::getFilePath($sFileName, static::EXT);
|
$this->sFilePath = self::getFilePath($sFileName, static::EXT);
|
||||||
$this->asTracks = array();
|
$this->asTracks = array();
|
||||||
}
|
}
|
||||||
@@ -45,6 +54,10 @@ class Geo extends PhpObject {
|
|||||||
public static function getFilePath($sFileName, $sExt) {
|
public static function getFilePath($sFileName, $sExt) {
|
||||||
return self::GEO_FOLDER.$sFileName.$sExt;
|
return self::GEO_FOLDER.$sFileName.$sExt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLog() {
|
||||||
|
return $this->getCleanMessageStack(PhpObject::NOTICE_TAB);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Gpx extends Geo {
|
class Gpx extends Geo {
|
||||||
@@ -61,7 +74,11 @@ class Gpx extends Geo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function parseFile() {
|
private function parseFile() {
|
||||||
|
$this->addNotice('Parsing: '.$this->sFilePath);
|
||||||
$oXml = simplexml_load_file($this->sFilePath);
|
$oXml = simplexml_load_file($this->sFilePath);
|
||||||
|
|
||||||
|
//Tracks
|
||||||
|
$this->addNotice('Converting '.count($oXml->trk).' tracks');
|
||||||
foreach($oXml->trk as $aoTrack) {
|
foreach($oXml->trk as $aoTrack) {
|
||||||
$asTrack = array(
|
$asTrack = array(
|
||||||
'name' => (string) $aoTrack->name,
|
'name' => (string) $aoTrack->name,
|
||||||
@@ -81,36 +98,67 @@ class Gpx extends Geo {
|
|||||||
}
|
}
|
||||||
$this->asTracks[] = $asTrack;
|
$this->asTracks[] = $asTrack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Waypoints
|
||||||
|
$this->addNotice('Ignoring '.count($oXml->wpt).' waypoints');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GeoJson extends Geo {
|
class GeoJson extends Geo {
|
||||||
|
|
||||||
const EXT = '.geojson';
|
const EXT = '.geojson';
|
||||||
|
const MAX_FILESIZE = 2; //MB
|
||||||
|
const MAX_DEVIATION = 0.1; //10%
|
||||||
|
|
||||||
public function __construct($sFileName) {
|
public function __construct($sFileName) {
|
||||||
parent::__construct($sFileName);
|
parent::__construct($sFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTracks($asTracks) {
|
public function saveFile() {
|
||||||
$this->asTracks = $asTracks;
|
$this->addNotice('Saving '.$this->sFilePath);
|
||||||
|
file_put_contents($this->sFilePath, $this->buildGeoJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function saveFile() {
|
public function isSimplicationRequired() {
|
||||||
$sContent = json_encode($this->getGeoJson());
|
//Size in bytes
|
||||||
file_put_contents($this->sFilePath, $sContent);
|
$iFileSize = strlen($this->buildGeoJson());
|
||||||
|
|
||||||
|
//Convert to MB
|
||||||
|
$iFileSize = round($iFileSize / pow(1024, 2), 2);
|
||||||
|
|
||||||
|
//Compare with max allowed size
|
||||||
|
$bFileTooLarge = ($iFileSize > self::MAX_FILESIZE);
|
||||||
|
if($bFileTooLarge) $this->addNotice('Output file is too large ('.$iFileSize.'MB > '.self::MAX_FILESIZE.'MB)');
|
||||||
|
|
||||||
|
return $bFileTooLarge;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getGeoJson() {
|
public function buildTracks($asTracks, $bSimplify=false) {
|
||||||
$asTracks = array('features'=>array());
|
$this->addNotice('Creating '.($bSimplify?'Simplified ':'').'GeoJson Tracks');
|
||||||
foreach($this->asTracks as $asTrackProps) {
|
|
||||||
|
$this->asTracks = array();
|
||||||
|
foreach($asTracks as $asTrackProps) {
|
||||||
|
|
||||||
//Color mapping
|
//Color mapping
|
||||||
switch($asTrackProps['color']) {
|
switch($asTrackProps['color']) {
|
||||||
case 'DarkBlue': $sType = 'main'; break;
|
case 'DarkBlue':
|
||||||
case 'Magenta': $sType = 'off-track'; break;
|
$sType = 'main';
|
||||||
case 'Red': $sType = 'hitchhiking'; break;
|
break;
|
||||||
default: continue 2; //discard tracks
|
case 'Magenta':
|
||||||
|
if($bSimplify) {
|
||||||
|
$this->addNotice('Ignoring Track "'.$asTrackProps['name'].' (off-track)');
|
||||||
|
continue 2; //discard tracks
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$sType = 'off-track';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Red':
|
||||||
|
$sType = 'hitchhiking';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->addNotice('Ignoring Track "'.$asTrackProps['name'].' (unknown color "'.$asTrackProps['color'].'")');
|
||||||
|
continue 2; //discard tracks
|
||||||
}
|
}
|
||||||
|
|
||||||
$asTrack = array(
|
$asTrack = array(
|
||||||
@@ -126,12 +174,47 @@ class GeoJson extends Geo {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach($asTrackProps['points'] as $asPoint) {
|
//Track points
|
||||||
|
$asTrackPoints = $asTrackProps['points'];
|
||||||
|
$iPointCount = count($asTrackPoints);
|
||||||
|
$iInvalidPointCount = 0;
|
||||||
|
foreach($asTrackPoints as $iIndex=>$asPoint) {
|
||||||
|
if($bSimplify && $iIndex > 0 && $iIndex < ($iPointCount - 1)) {
|
||||||
|
if(!$this->isPointValid($asTrackPoints[$iIndex - 1], $asPoint, $asTrackPoints[$iIndex + 1])) {
|
||||||
|
$iInvalidPointCount++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
$asTrack['geometry']['coordinates'][] = array_values($asPoint);
|
$asTrack['geometry']['coordinates'][] = array_values($asPoint);
|
||||||
}
|
}
|
||||||
$asTracks['features'][] = $asTrack;
|
$this->asTracks[] = $asTrack;
|
||||||
|
|
||||||
|
if($iInvalidPointCount > 0) $this->addNotice('Removing '.$iInvalidPointCount.'/'.$iPointCount.' points ('.round($iInvalidPointCount / $iPointCount * 100, 1).'%) from '.$asTrackProps['name']);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isPointValid($asPointA, $asPointO, $asPointB) {
|
||||||
|
/* A----O Calculate angle AO^OB
|
||||||
|
\ If angle is within [Pi - 10%; Pi + 10%], O can be discarded
|
||||||
|
\ O is valid otherwise
|
||||||
|
B
|
||||||
|
-> -> -> ->
|
||||||
|
Law of Cosines: angle = arccos(OA.OB / ||OA||.||OB||)
|
||||||
|
*/
|
||||||
|
$fVectorOA = array('lon'=>($asPointA['lon'] - $asPointO['lon']), 'lat'=> ($asPointA['lat'] - $asPointO['lat']));
|
||||||
|
$fVectorOB = array('lon'=>($asPointB['lon'] - $asPointO['lon']), 'lat'=> ($asPointB['lat'] - $asPointO['lat']));
|
||||||
|
|
||||||
return $asTracks;
|
$fVectorOAxOB = $fVectorOA['lon'] * $fVectorOB['lon'] + $fVectorOA['lat'] * $fVectorOB['lat'];
|
||||||
|
|
||||||
|
$fLengthOA = sqrt(pow($asPointA['lon'] - $asPointO['lon'], 2) + pow($asPointA['lat'] - $asPointO['lat'], 2));
|
||||||
|
$fLengthOB = sqrt(pow($asPointO['lon'] - $asPointB['lon'], 2) + pow($asPointO['lat'] - $asPointB['lat'], 2));
|
||||||
|
|
||||||
|
$fAngleAOB = acos($fVectorOAxOB/($fLengthOA * $fLengthOB));
|
||||||
|
|
||||||
|
return ($fAngleAOB <= (1 - self::MAX_DEVIATION) * M_PI || $fAngleAOB >= (1 + self::MAX_DEVIATION) * M_PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildGeoJson() {
|
||||||
|
return json_encode(array('features'=>$this->asTracks));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,9 +124,7 @@ class Project extends PhpObject {
|
|||||||
case 2: $asProject['mode'] = self::MODE_HISTO; break;
|
case 2: $asProject['mode'] = self::MODE_HISTO; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Converter::isGeoJsonValid($asProject['geofile'])) {
|
if(!Converter::isGeoJsonValid($asProject['geofile'])) Converter::convertToGeoJson($asProject['geofile']);
|
||||||
(new Converter())->convertToGeoJson($asProject['geofile']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$asProject['geofile'] = Spot::addTimestampToFilePath(Geo::getFilePath($asProject['geofile'], GeoJson::EXT));
|
$asProject['geofile'] = Spot::addTimestampToFilePath(Geo::getFilePath($asProject['geofile'], GeoJson::EXT));
|
||||||
$asProject['codename'] = $sCodeName;
|
$asProject['codename'] = $sCodeName;
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class Spot extends Main
|
|||||||
array('name'=>'feed', 'project'=>true),
|
array('name'=>'feed', 'project'=>true),
|
||||||
array('name'=>'project', 'project'=>true),
|
array('name'=>'project', 'project'=>true),
|
||||||
array('name'=>'media', 'project'=>true),
|
array('name'=>'media', 'project'=>true),
|
||||||
array('name'=>'converter', 'project'=>true)
|
array('name'=>'converter', 'project'=>true)
|
||||||
);
|
);
|
||||||
parent::__construct($oClassManagement, $sProcessPage, $asClasses, true, __FILE__, $sTimezone);
|
parent::__construct($oClassManagement, $sProcessPage, $asClasses, true, __FILE__, $sTimezone);
|
||||||
|
|
||||||
@@ -315,7 +315,7 @@ class Spot extends Main
|
|||||||
Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId(),
|
Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId(),
|
||||||
'name' => mb_strtolower(trim($sName)),
|
'name' => mb_strtolower(trim($sName)),
|
||||||
'content' => trim($sPost),
|
'content' => trim($sPost),
|
||||||
'site_time' => date(Db::TIMESTAMP_FORMAT) //site time (Settings::TIMEZONE)
|
'site_time' => date(Db::TIMESTAMP_FORMAT) //site time (Settings::TIMEZONE)
|
||||||
);
|
);
|
||||||
$iPostId = $this->oDb->insertRow(self::POST_TABLE, $asData);
|
$iPostId = $this->oDb->insertRow(self::POST_TABLE, $asData);
|
||||||
return self::getJsonResult(($iPostId > 0), '');
|
return self::getJsonResult(($iPostId > 0), '');
|
||||||
@@ -361,6 +361,10 @@ class Spot extends Main
|
|||||||
|
|
||||||
return self::getJsonResult($bSuccess, $sDesc, array('values'=>$this->oProject->getProject()));
|
return self::getJsonResult($bSuccess, $sDesc, array('values'=>$this->oProject->getProject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function convertGpxToGeojson($sGeoFileName) {
|
||||||
|
return Converter::convertToGeoJson($sGeoFileName);
|
||||||
|
}
|
||||||
|
|
||||||
public static function decToDms($dValue, $sType) {
|
public static function decToDms($dValue, $sType) {
|
||||||
if($sType=='lat') $sDirection = ($dValue >= 0)?'N':'S'; //Latitude
|
if($sType=='lat') $sDirection = ($dValue >= 0)?'N':'S'; //Latitude
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ if($sAction!='')
|
|||||||
case 'set_admin':
|
case 'set_admin':
|
||||||
$sResult = $oSpot->setAdminSettings($sField, $oValue);
|
$sResult = $oSpot->setAdminSettings($sField, $oValue);
|
||||||
break;
|
break;
|
||||||
|
case 'build_geojson':
|
||||||
|
$sResult = $oSpot->convertGpxToGeojson($sName);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
$sResult = Main::getJsonResult(false, Main::NOT_FOUND);
|
$sResult = Main::getJsonResult(false, Main::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -444,6 +444,8 @@ function updateFeed(bFirstChunk, bDiscrete) {
|
|||||||
self.tmp('$PostList').append($Posts.children());
|
self.tmp('$PostList').append($Posts.children());
|
||||||
|
|
||||||
self.tmp('updatable', true);
|
self.tmp('updatable', true);
|
||||||
|
|
||||||
|
if(bFirstChunk && !asData.length) toggleFeedPanel(false);
|
||||||
}, {
|
}, {
|
||||||
'project_id': self.vars(['project', 'id']),
|
'project_id': self.vars(['project', 'id']),
|
||||||
'chunk': self.tmp('news_chunk')
|
'chunk': self.tmp('news_chunk')
|
||||||
|
|||||||
Reference in New Issue
Block a user