automatic geojson file generation
This commit is contained in:
1
files/db/update_v4_to_v5.sql
Normal file
1
files/db/update_v4_to_v5.sql
Normal file
@@ -0,0 +1 @@
|
||||
UPDATE projects SET geofile = REPLACE(geofile, '.geojson', '');
|
||||
@@ -3,21 +3,17 @@
|
||||
/**
|
||||
* To convert a gpx file:
|
||||
* 1. Add file <file_name>.gpx to geo/ folder
|
||||
* 2. Assign file to project: UPDATE projects SET geofile = '<file_name>.geojson'
|
||||
* 2. Assign file to project: UPDATE projects SET geofile = '<file_name>' WHERE id_project = <id_project>;
|
||||
*/
|
||||
class Converter extends PhpObject {
|
||||
|
||||
const GPX_EXT = '.gpx';
|
||||
const GEO_EXT = '.geojson';
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||
}
|
||||
|
||||
public function convertToGeoJson($sGeoFile) {
|
||||
$sFileName = pathinfo($sGeoFile, PATHINFO_FILENAME);
|
||||
$sGpxFileName = $sFileName.self::GPX_EXT;
|
||||
$sGeoJsonFileName = $sFileName.self::GEO_EXT;
|
||||
public function convertToGeoJson($sFileName) {
|
||||
$sGpxFileName = $sFileName.Gpx::EXT;
|
||||
$sGeoJsonFileName = $sFileName.GeoJson::EXT;
|
||||
|
||||
$oGpx = new Gpx($sGpxFileName);
|
||||
$oGeoJson = new GeoJson($sGeoJsonFileName);
|
||||
@@ -26,6 +22,14 @@ class Converter extends PhpObject {
|
||||
$oGeoJson->saveFile();
|
||||
}
|
||||
|
||||
public static function isGeoJsonValid($sFileName) {
|
||||
$bResult = false;
|
||||
$sGeoJsonFilePath = Geo::getFilePath($sFileName);
|
||||
if(file_exists($sGeoJsonFilePath)) {
|
||||
if(filemtime($sGeoJsonFilePath) > filemtime(Geo::getFilePath($sFileName, Gpx::EXT))) $bResult = true;
|
||||
}
|
||||
return $bResult;
|
||||
}
|
||||
}
|
||||
|
||||
class Geo extends PhpObject {
|
||||
@@ -40,10 +44,16 @@ class Geo extends PhpObject {
|
||||
$this->sFilePath = self::GEO_FOLDER.$sFileName;
|
||||
$this->asTracks = array();
|
||||
}
|
||||
|
||||
public static function getFilePath($sFileName, $sExt=GeoJson::EXT) {
|
||||
return self::GEO_FOLDER.$sFileName.$sExt;
|
||||
}
|
||||
}
|
||||
|
||||
class Gpx extends Geo {
|
||||
|
||||
const EXT = '.gpx';
|
||||
|
||||
public function __construct($sFileName) {
|
||||
parent::__construct($sFileName);
|
||||
$this->parseFile();
|
||||
@@ -79,6 +89,8 @@ class Gpx extends Geo {
|
||||
|
||||
class GeoJson extends Geo {
|
||||
|
||||
const EXT = '.geojson';
|
||||
|
||||
public function __construct($sFileName) {
|
||||
parent::__construct($sFileName);
|
||||
}
|
||||
@@ -89,7 +101,6 @@ class GeoJson extends Geo {
|
||||
|
||||
public function saveFile() {
|
||||
$sContent = json_encode($this->getGeoJson());
|
||||
//$sContent = str_replace('{"type":"Feature"', "\n\t{\"type\":\"Feature\"", $sContent);
|
||||
file_put_contents($this->sFilePath, $sContent);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,6 @@ class Project extends PhpObject {
|
||||
const MODE_HISTO = 'H';
|
||||
const MODES = array('previz'=>self::MODE_PREVIZ, 'blog'=>self::MODE_BLOG, 'histo'=>self::MODE_HISTO);
|
||||
|
||||
//Folders
|
||||
const GEO_FOLDER = 'geo/';
|
||||
|
||||
//DB Tables
|
||||
const PROJ_TABLE = 'projects';
|
||||
|
||||
@@ -101,12 +98,11 @@ class Project extends PhpObject {
|
||||
case 2: $asProject['mode'] = self::MODE_HISTO; break;
|
||||
}
|
||||
|
||||
$sGeoFile = self::GEO_FOLDER.$asProject['geofile'];
|
||||
if(!file_exists($sGeoFile)) {
|
||||
if(!Converter::isGeoJsonValid($asProject['geofile'])) {
|
||||
(new Converter())->convertToGeoJson($asProject['geofile']);
|
||||
}
|
||||
|
||||
$asProject['geofile'] = Spot::addTimestampToFilePath($sGeoFile);
|
||||
$asProject['geofile'] = Spot::addTimestampToFilePath(Geo::getFilePath($asProject['geofile']));
|
||||
$asProject['codename'] = $sCodeName;
|
||||
$asProject['timezone_desc'] = Spot::getTimeZoneDesc($asProject['timezone']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user