refs #1 add ability to load kml and csv (unicsv) files, conversion with gpsbabel

merge-requests/1/head
Julien Veyssier 2016-12-05 16:58:59 +01:00
parent 1b65594f53
commit 40a2350228
5 changed files with 74 additions and 12 deletions

View File

@ -5,3 +5,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [Unreleased]
### Added
- ability to load kml and csv (unicsv format) files
[#1](https://gitlab.com/eneiluj/gpxedit-oc/issues/1) @eneiluj

View File

@ -181,6 +181,53 @@ class PageController extends Controller {
return $response; return $response;
} }
/**
* convert the given file (csv or kml) to gpx and return its content
*/
public function toGpx($file){
$gpsbabel_path = getProgramPath('gpsbabel');
$data_folder = $this->userAbsoluteDataPath;
$tempdir = $data_folder.'/../cache/'.rand();
mkdir($tempdir);
$filename = $file->getName();
$filecontent = $file->getContent();
$file_clear_path = $tempdir.'/'.$filename;
$gpx_target_clear_path = $tempdir.'/'.$filename.'.gpx';
file_put_contents($file_clear_path, $filecontent);
if (endswith($file->getName(), '.KML') or endswith($file->getName(), '.kml')){
$fmt = 'kml';
}
else if (endswith($file->getName(), '.csv') or endswith($file->getName(), '.CSV')){
$fmt = 'unicsv';
}
$args = Array('-i', $fmt, '-f', $file_clear_path, '-o',
'gpx', '-F', $gpx_target_clear_path);
$cmdparams = '';
foreach($args as $arg){
$shella = escapeshellarg($arg);
$cmdparams .= " $shella";
}
exec(
escapeshellcmd(
$gpsbabel_path.' '.$cmdparams
),
$output, $returnvar
);
if (file_exists($gpx_target_clear_path)){
$gpx_clear_content = file_get_contents($gpx_target_clear_path);
}
else{
$gpx_clear_content = '';
}
delTree($tempdir);
return $gpx_clear_content;
}
/** /**
* *
* @NoAdminRequired * @NoAdminRequired
@ -192,20 +239,22 @@ class PageController extends Controller {
$gpxContent = ''; $gpxContent = '';
if ($userFolder->nodeExists($cleanpath)){ if ($userFolder->nodeExists($cleanpath)){
$file = $userFolder->get($cleanpath); $file = $userFolder->get($cleanpath);
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE and if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE){
(endswith($file->getName(), '.GPX') or endswith($file->getName(), '.gpx')) if (endswith($file->getName(), '.GPX') or endswith($file->getName(), '.gpx')){
){ $gpxContent = $file->getContent();
// all ok }
else if (getProgramPath('gpsbabel') !== null and
(endswith($file->getName(), '.KML') or endswith($file->getName(), '.kml') or
endswith($file->getName(), '.CSV') or endswith($file->getName(), '.csv'))
){
$gpxContent = $this->toGpx($file);
}
} }
else{ else{
$file = null; $file = null;
} }
} }
if ($file !== null){
$gpxContent = $file->getContent();
}
$response = new DataResponse( $response = new DataResponse(
[ [
'gpx'=>$gpxContent 'gpx'=>$gpxContent
@ -285,6 +334,7 @@ class PageController extends Controller {
public function getdircontent($dir) { public function getdircontent($dir) {
$userFolder = \OC::$server->getUserFolder(); $userFolder = \OC::$server->getUserFolder();
$userfolder_path = $userFolder->getPath(); $userfolder_path = $userFolder->getPath();
$gpsbabelpath = getProgramPath('gpsbabel');
$responseTxt = '<ul class="jqueryFileTree">'; $responseTxt = '<ul class="jqueryFileTree">';
//error_log('DIR : '.$dir); //error_log('DIR : '.$dir);
@ -297,9 +347,18 @@ class PageController extends Controller {
if ($elem->getType() === \OCP\Files\FileInfo::TYPE_FOLDER){ if ($elem->getType() === \OCP\Files\FileInfo::TYPE_FOLDER){
$responseTxt .= '<li class="directory collapsed"><a href="#" rel="'.$elempath.'">'.$elem->getName().'</a></li>'; $responseTxt .= '<li class="directory collapsed"><a href="#" rel="'.$elempath.'">'.$elem->getName().'</a></li>';
} }
else if ($elem->getType() === \OCP\Files\FileInfo::TYPE_FILE and else if ($elem->getType() === \OCP\Files\FileInfo::TYPE_FILE){
(endswith($elempath, '.gpx') or endswith($elempath, '.GPX'))){ if (endswith($elempath, '.gpx') or endswith($elempath, '.GPX')){
$responseTxt .= '<li class="gpx ext_gpx"><a href="#" rel="'.$elempath.'">'.$elem->getName().'</a></li>'; $responseTxt .= '<li class="gpx ext_gpx"><a href="#" rel="'.$elempath.'">'.$elem->getName().'</a></li>';
}
else if ($gpsbabelpath !== null and
(endswith($elempath, '.csv') or endswith($elempath, '.CSV'))){
$responseTxt .= '<li class="csv ext_csv"><a href="#" rel="'.$elempath.'">'.$elem->getName().'</a></li>';
}
else if ($gpsbabelpath !== null and
(endswith($elempath, '.kml') or endswith($elempath, '.KML'))){
$responseTxt .= '<li class="kml ext_kml"><a href="#" rel="'.$elempath.'">'.$elem->getName().'</a></li>';
}
} }
} }
} }

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 440 B