wp-gpx-maps/wp-gpx-maps_utils.php

612 lines
16 KiB
PHP
Raw Normal View History

2011-12-19 13:27:22 +00:00
<?php
2012-01-08 16:47:40 +00:00
2012-03-17 11:46:00 +00:00
require_once("wp-gpx-maps_utils_nggallery.php");
2012-01-08 16:47:40 +00:00
function sitePath()
{
return substr(substr(__FILE__, 0, strrpos(__FILE__,'wp-content')), 0, -1);
// $uploadsPath = substr($uploadsPath, 0, -1);
}
function gpxFolderPath()
{
$upload_dir = wp_upload_dir();
$uploadsPath = $upload_dir['basedir'];
2012-09-18 16:50:29 +00:00
if ( current_user_can('manage_options') ){
$ret = $uploadsPath.DIRECTORY_SEPARATOR."gpx";
}
else if ( current_user_can('publish_posts') ) {
global $current_user;
get_currentuserinfo();
$ret = $uploadsPath.DIRECTORY_SEPARATOR."gpx".DIRECTORY_SEPARATOR.$current_user->user_login;
}
2012-01-08 16:47:40 +00:00
return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $ret);
}
2012-02-07 13:06:51 +00:00
function gpxCacheFolderPath()
{
$upload_dir = wp_upload_dir();
$uploadsPath = $upload_dir['basedir'];
2012-09-18 16:50:29 +00:00
$ret = $uploadsPath.DIRECTORY_SEPARATOR."gpx".DIRECTORY_SEPARATOR."~cache";
2012-02-07 13:06:51 +00:00
return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $ret);
}
2012-01-08 16:47:40 +00:00
function relativeGpxFolderPath()
{
$sitePath = sitePath();
$realGpxPath = gpxFolderPath();
$ret = str_replace($sitePath,'',$realGpxPath).DIRECTORY_SEPARATOR;
return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $ret);
}
2012-03-17 11:46:00 +00:00
2012-02-07 13:06:51 +00:00
function recursive_remove_directory($directory, $empty=FALSE)
{
if(substr($directory,-1) == '/')
{
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory))
{
return FALSE;
}elseif(is_readable($directory))
{
$handle = opendir($directory);
while (FALSE !== ($item = readdir($handle)))
{
if($item != '.' && $item != '..')
{
$path = $directory.'/'.$item;
if(is_dir($path))
{
recursive_remove_directory($path);
}else{
unlink($path);
}
}
}
closedir($handle);
if($empty == FALSE)
{
if(!rmdir($directory))
{
return FALSE;
}
}
}
return TRUE;
}
2012-01-08 16:47:40 +00:00
function getPoints($gpxPath, $gpxOffset = 10, $donotreducegpx, $distancetype)
2011-12-19 13:27:22 +00:00
{
2012-01-24 17:05:59 +00:00
2011-12-19 13:27:22 +00:00
$points = array();
$dist=0;
$lastLat=0;
$lastLon=0;
$lastEle=0;
$lastOffset=0;
2012-01-03 09:53:36 +00:00
2011-12-30 20:49:06 +00:00
if (file_exists($gpxPath))
2011-12-19 13:27:22 +00:00
{
$points = @parseXml($gpxPath, $gpxOffset, $distancetype);
2011-12-20 15:37:26 +00:00
}
else
{
echo "WP GPX Maps Error: File $gpxPath not found!";
2011-12-20 15:37:26 +00:00
}
2012-01-03 09:53:36 +00:00
// reduce the points to around 200 to speedup
if ( $donotreducegpx != true)
2011-12-20 15:37:26 +00:00
{
2012-04-07 09:11:37 +00:00
$count=sizeof($points->lat);
2012-01-03 09:53:36 +00:00
if ($count>200)
{
$f = round($count/200);
if ($f>1)
for($i=$count;$i>0;$i--)
2012-07-02 12:51:45 +00:00
if ($i % $f != 0 && $points->lat[$i] != null)
2012-04-07 09:11:37 +00:00
{
2012-07-02 12:51:45 +00:00
unset($points->dt[$i]);
2012-04-07 09:11:37 +00:00
unset($points->lat[$i]);
unset($points->lon[$i]);
unset($points->ele[$i]);
unset($points->dist[$i]);
unset($points->speed[$i]);
unset($points->hr[$i]);
unset($points->atemp[$i]);
2012-04-12 14:49:22 +00:00
unset($points->cad[$i]);
unset($points->grade[$i]);
2012-04-07 09:11:37 +00:00
}
2012-01-03 09:53:36 +00:00
}
2011-12-20 15:37:26 +00:00
}
return $points;
}
function parseXml($filePath, $gpxOffset, $distancetype)
2011-12-20 15:37:26 +00:00
{
2012-04-07 09:11:37 +00:00
$points = null;
2012-07-02 12:51:45 +00:00
$points->dt = array();
2012-04-07 09:11:37 +00:00
$points->lat = array();
$points->lon = array();
$points->ele = array();
$points->dist = array();
$points->speed = array();
$points->hr = array();
$points->atemp = array();
2012-04-12 14:49:22 +00:00
$points->cad = array();
$points->grade = array();
2012-07-31 14:34:27 +00:00
$points->maxTime = 0;
$points->minTime = 0;
2012-06-25 10:22:56 +00:00
$points->maxEle = 0;
$points->minEle = 0;
$points->totalEleUp = 0;
$points->totalEleDown = 0;
$points->avgSpeed = 0;
$points->totalLength = 0;
2012-04-07 09:11:37 +00:00
2011-12-20 15:37:26 +00:00
$gpx = simplexml_load_file($filePath);
2012-01-08 16:47:40 +00:00
2012-02-15 16:04:18 +00:00
if($gpx === FALSE)
2012-01-08 16:47:40 +00:00
return;
2011-12-20 15:37:26 +00:00
$gpx->registerXPathNamespace('10', 'http://www.topografix.com/GPX/1/0');
2012-07-02 12:51:45 +00:00
$gpx->registerXPathNamespace('11', 'http://www.topografix.com/GPX/1/1');
2012-04-07 09:11:37 +00:00
$gpx->registerXPathNamespace('gpxtpx', 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1');
2011-12-20 15:37:26 +00:00
2012-07-02 12:51:45 +00:00
$nodes = $gpx->xpath('//trk | //10:trk | //11:trk');
//normal gpx
2011-12-20 15:37:26 +00:00
if ( count($nodes) > 0 )
{
2012-01-08 16:47:40 +00:00
2012-07-02 12:51:45 +00:00
foreach($nodes as $_trk)
{
$trk = simplexml_load_string($_trk->asXML());
2012-04-07 09:11:37 +00:00
2012-07-02 12:51:45 +00:00
$trk->registerXPathNamespace('10', 'http://www.topografix.com/GPX/1/0');
$trk->registerXPathNamespace('11', 'http://www.topografix.com/GPX/1/1');
2012-10-18 13:10:05 +00:00
$trk->registerXPathNamespace('gpxtpx', 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1');
2012-07-02 12:51:45 +00:00
$trkpts = $trk->xpath('//trkpt | //10:trkpt | //11:trkpt');
$lastLat = 0;
$lastLon = 0;
$lastEle = 0;
$lastTime = 0;
//$dist = 0;
$lastOffset = 0;
$speedBuffer = array();
foreach($trkpts as $trkpt)
2011-12-19 13:27:22 +00:00
{
2012-07-02 12:51:45 +00:00
$lat = $trkpt['lat'];
$lon = $trkpt['lon'];
$ele = $trkpt->ele;
$time = $trkpt->time;
$speed = (float)$trkpt->speed;
$hr = 0;
$atemp = 0;
2012-07-02 12:51:45 +00:00
$cad = 0;
$grade = 0;
2012-10-18 13:10:05 +00:00
2012-07-02 12:51:45 +00:00
if (isset($trkpt->extensions))
{
2014-07-14 07:48:30 +00:00
$arr = json_decode( json_encode($trkpt->extensions) , 1);
if (isset($arr['gpxtpx:TrackPointExtension']))
{
$tpe = $arr['gpxtpx:TrackPointExtension'];
$hr = @$tpe["gpxtpx:hr"];
$atemp = @$tpe["gpxtpx:atemp"];
$cad = @$tpe["gpxtpx:cad"];
}
else if (isset($arr['TrackPointExtension']))
{
2014-07-14 07:48:30 +00:00
$tpe = $arr['TrackPointExtension'];
$hr = @$tpe["hr"];
$atemp = @$tpe["atemp"];
$cad = @$tpe["cad"];
}
2012-06-25 10:22:56 +00:00
}
2012-07-02 12:51:45 +00:00
if ($lastLat == 0 && $lastLon == 0)
2011-12-19 13:27:22 +00:00
{
2012-07-02 12:51:45 +00:00
//Base Case
array_push($points->dt, strtotime($time));
array_push($points->lat, (float)$lat);
array_push($points->lon, (float)$lon);
array_push($points->ele, (float)round($ele,2));
array_push($points->dist, (float)round($dist,2));
array_push($points->speed, 0);
array_push($points->hr, (float)$hr);
array_push($points->atemp, (float)$atemp);
array_push($points->cad, (float)$cad);
array_push($points->grade, $grade);
2012-02-29 14:01:40 +00:00
2012-07-02 12:51:45 +00:00
$lastLat=$lat;
$lastLon=$lon;
$lastEle=$ele;
$lastTime=$time;
2012-07-02 12:51:45 +00:00
}
else
{
//Normal Case
$offset = calculateDistance((float)$lat, (float)$lon, (float)$ele, (float)$lastLat, (float)$lastLon, (float)$lastEle, $distancetype);
2012-07-02 12:51:45 +00:00
$dist = $dist + $offset;
2012-02-29 14:01:40 +00:00
2012-07-02 12:51:45 +00:00
$points->totalLength = $dist;
2012-02-29 14:01:40 +00:00
2012-07-02 12:51:45 +00:00
if ($speed == 0)
{
$datediff = (float)my_date_diff($lastTime,$time);
if ($datediff>0)
{
$speed = $offset / $datediff;
}
}
2012-04-07 09:11:37 +00:00
2012-07-02 12:51:45 +00:00
if ($ele != 0 && $lastEle != 0)
{
$deltaEle = (float)($ele - $lastEle);
2012-07-02 12:51:45 +00:00
if ((float)$ele > (float)$lastEle)
{
$points->totalEleUp += $deltaEle;
2012-07-02 12:51:45 +00:00
}
else
{
$points->totalEleDown += $deltaEle;
2012-07-02 12:51:45 +00:00
}
$grade = $deltaEle / $offset * 100;
2012-07-02 12:51:45 +00:00
}
2012-04-07 09:11:37 +00:00
2012-07-02 12:51:45 +00:00
array_push($speedBuffer, $speed);
if (((float) $offset + (float) $lastOffset) > $gpxOffset)
{
//Bigger Offset -> write coordinate
$avgSpeed = 0;
foreach($speedBuffer as $s)
{
$avgSpeed += $s;
}
$avgSpeed = $avgSpeed / count($speedBuffer);
$speedBuffer = array();
$lastOffset=0;
array_push($points->dt, strtotime($time));
array_push($points->lat, (float)$lat );
array_push($points->lon, (float)$lon );
array_push($points->ele, (float)round($ele, 2) );
array_push($points->dist, (float)round($dist, 2) );
array_push($points->speed, (float)round($avgSpeed, 1) );
array_push($points->hr, $hr);
array_push($points->atemp, $atemp);
2012-07-02 12:51:45 +00:00
array_push($points->cad, $cad);
array_push($points->grade, (float)round($grade, 2) );
2012-07-02 12:51:45 +00:00
}
else
{
//Smoller Offset -> continue..
$lastOffset = (float) $lastOffset + (float) $offset ;
}
2011-12-19 13:27:22 +00:00
}
2012-07-02 12:51:45 +00:00
$lastLat=$lat;
$lastLon=$lon;
$lastEle=$ele;
$lastTime=$time;
2011-12-19 13:27:22 +00:00
}
2012-07-02 12:51:45 +00:00
array_push($points->dt, null);
array_push($points->lat, null);
array_push($points->lon, null);
array_push($points->ele, null);
array_push($points->dist, null);
array_push($points->speed, null);
array_push($points->hr, null);
array_push($points->atemp, null);
array_push($points->cad, null);
array_push($points->grade, null);
2012-07-02 12:51:45 +00:00
unset($trkpts);
2011-12-19 13:27:22 +00:00
}
2012-07-02 12:51:45 +00:00
2012-01-08 16:47:40 +00:00
unset($nodes);
2012-06-25 10:22:56 +00:00
try {
2012-07-31 14:34:27 +00:00
array_pop($points->dt, null);
array_pop($points->lat, null);
array_pop($points->lon, null);
array_pop($points->ele, null);
array_pop($points->dist, null);
array_pop($points->speed, null);
array_pop($points->hr, null);
array_pop($points->atemp, null);
array_pop($points->cad, null);
array_pop($points->grade, null);
2012-07-31 14:34:27 +00:00
$_time = array_filter($points->dt);
2012-07-04 12:06:44 +00:00
$_ele = array_filter($points->ele);
$_dist = array_filter($points->dist);
$_speed = array_filter($points->speed);
$points->maxEle = max($_ele);
$points->minEle = min($_ele);
$points->totalLength = max($_dist);
2012-07-31 14:34:27 +00:00
$points->maxTime = max($_time);
$points->minTime = min($_time);
2012-07-04 12:06:44 +00:00
$points->avgSpeed = array_sum($_speed) / count($_speed);
2012-06-25 10:22:56 +00:00
} catch (Exception $e) { }
2011-12-20 15:37:26 +00:00
2011-12-19 13:27:22 +00:00
}
else
{
2011-12-20 15:37:26 +00:00
2012-07-02 12:51:45 +00:00
// gpx garmin case
$gpx->registerXPathNamespace('gpxx', 'http://www.garmin.com/xmlschemas/GpxExtensions/v3');
2011-12-20 15:37:26 +00:00
$nodes = $gpx->xpath('//gpxx:rpt');
if ( count($nodes) > 0 )
{
2012-01-08 16:47:40 +00:00
$lastLat = 0;
$lastLon = 0;
$lastEle = 0;
$dist = 0;
$lastOffset = 0;
2011-12-20 15:37:26 +00:00
// Garmin case
foreach($nodes as $rpt)
{
$lat = $rpt['lat'];
$lon = $rpt['lon'];
if ($lastLat == 0 && $lastLon == 0)
{
//Base Case
2012-04-07 09:11:37 +00:00
array_push($points->lat, (float)$lat );
array_push($points->lon, (float)$lon );
array_push($points->ele, 0 );
array_push($points->dist, 0 );
array_push($points->speed, 0 );
2012-04-12 14:49:22 +00:00
array_push($points->hr, 0 );
array_push($points->atemp, 0 );
2012-04-12 14:49:22 +00:00
array_push($points->cad, 0 );
array_push($points->grade, 0 );
2011-12-20 15:37:26 +00:00
$lastLat=$lat;
$lastLon=$lon;
}
else
{
//Normal Case
$offset = calculateDistance($lat, $lon, 0,$lastLat, $lastLon, 0, $distancetype);
2011-12-20 15:37:26 +00:00
$dist = $dist + $offset;
if (((float) $offset + (float) $lastOffset) > $gpxOffset)
{
//Bigger Offset -> write coordinate
$lastOffset=0;
2012-04-07 09:11:37 +00:00
array_push($points->lat, (float)$lat );
array_push($points->lon, (float)$lon );
array_push($points->ele, 0 );
array_push($points->dist, 0 );
2012-04-12 14:49:22 +00:00
array_push($points->speed, 0 );
array_push($points->hr, 0 );
array_push($points->atemp, 0 );
2012-04-12 14:49:22 +00:00
array_push($points->cad, 0 );
array_push($points->grade, 0 );
2011-12-20 15:37:26 +00:00
}
else
{
//Smoller Offset -> continue..
$lastOffset= (float) $lastOffset + (float) $offset;
}
}
$lastLat=$lat;
$lastLon=$lon;
}
2012-01-08 16:47:40 +00:00
unset($nodes);
2012-07-02 12:51:45 +00:00
2011-12-20 15:37:26 +00:00
}
else
2012-07-02 12:51:45 +00:00
{
//gpx strange case
$nodes = $gpx->xpath('//rtept | //10:rtept | //11:rtept');
if ( count($nodes) > 0 )
{
$lastLat = 0;
$lastLon = 0;
$lastEle = 0;
$dist = 0;
$lastOffset = 0;
// Garmin case
foreach($nodes as $rtept)
{
$lat = $rtept['lat'];
$lon = $rtept['lon'];
if ($lastLat == 0 && $lastLon == 0)
{
//Base Case
array_push($points->lat, (float)$lat );
array_push($points->lon, (float)$lon );
array_push($points->ele, 0 );
array_push($points->dist, 0 );
array_push($points->speed, 0 );
array_push($points->hr, 0 );
array_push($points->atemp, 0 );
2012-07-02 12:51:45 +00:00
array_push($points->cad, 0 );
array_push($points->grade, 0 );
2012-07-02 12:51:45 +00:00
$lastLat=$lat;
$lastLon=$lon;
}
else
{
//Normal Case
$offset = calculateDistance($lat, $lon, 0,$lastLat, $lastLon, 0, $distancetype);
2012-07-02 12:51:45 +00:00
$dist = $dist + $offset;
if (((float) $offset + (float) $lastOffset) > $gpxOffset)
{
//Bigger Offset -> write coordinate
$lastOffset=0;
array_push($points->lat, (float)$lat );
array_push($points->lon, (float)$lon );
array_push($points->ele, 0 );
array_push($points->dist, 0 );
array_push($points->speed, 0 );
array_push($points->hr, 0 );
array_push($points->atemp, 0 );
2012-07-02 12:51:45 +00:00
array_push($points->cad, 0 );
array_push($points->grade, 0 );
2012-07-02 12:51:45 +00:00
}
else
{
//Smoller Offset -> continue..
$lastOffset= (float) $lastOffset + (float) $offset;
}
}
$lastLat=$lat;
$lastLon=$lon;
}
unset($nodes);
}
2011-12-20 15:37:26 +00:00
}
2012-07-02 12:51:45 +00:00
2011-12-19 13:27:22 +00:00
}
2012-07-02 12:51:45 +00:00
2012-01-08 16:47:40 +00:00
unset($gpx);
2011-12-19 13:27:22 +00:00
return $points;
2011-12-20 15:37:26 +00:00
}
2011-12-24 14:37:05 +00:00
function getWayPoints($gpxPath)
{
$points = array();
2011-12-30 20:49:06 +00:00
if (file_exists($gpxPath))
2011-12-24 14:37:05 +00:00
{
try {
$gpx = simplexml_load_file($gpxPath);
} catch (Exception $e) {
echo "WP GPX Maps Error: Cant parse xml file " . $gpxPath;
return $points;
}
2011-12-24 14:37:05 +00:00
$gpx->registerXPathNamespace('10', 'http://www.topografix.com/GPX/1/0');
$gpx->registerXPathNamespace('11', 'http://www.topografix.com/GPX/1/1');
$nodes = $gpx->xpath('//wpt | //10:wpt | //11:wpt');
if ( count($nodes) > 0 )
{
// normal case
foreach($nodes as $wpt)
{
$lat = $wpt['lat'];
$lon = $wpt['lon'];
$ele = $wpt->ele;
$time = $wpt->time;
$name = $wpt->name;
$desc = $wpt->desc;
$sym = $wpt->sym;
$type = $wpt->type;
array_push($points, array((float)$lat,(float)$lon,(float)$ele,$time,$name,$desc,$sym,$type));
}
}
}
return $points;
}
2011-12-19 13:27:22 +00:00
function toRadians($degrees)
{
2012-01-28 11:56:52 +00:00
return (float)($degrees * 3.1415926535897932385 / 180);
2011-12-20 15:37:26 +00:00
}
2011-12-19 13:27:22 +00:00
function calculateDistance($lat1,$lon1,$ele1,$lat2,$lon2,$ele2,$distancetype)
2011-12-19 13:27:22 +00:00
{
if ($distancetype == '2') // climb
{
return (float)$ele1 - (float)$ele2;
}
else if ($distancetype == '1') // flat
{
$alpha = (float)sin((float)toRadians((float) $lat2 - (float) $lat1) / 2);
$beta = (float)sin((float)toRadians((float) $lon2 - (float) $lon1) / 2);
//Distance in meters
$a = (float) ( (float)$alpha * (float)$alpha) + (float) ( (float)cos( (float)toRadians($lat1)) * (float)cos( (float)toRadians($lat2)) * (float)$beta * (float)$beta );
$dist = 2 * 6369628.75 * (float)atan2((float)sqrt((float)$a), (float)sqrt(1 - (float) $a));
return (float)sqrt((float)pow((float)$dist, 2) + pow((float) $lat1 - (float)$lat2, 2));
}
else // normal
{
$alpha = (float)sin((float)toRadians((float) $lat2 - (float) $lat1) / 2);
$beta = (float)sin((float)toRadians((float) $lon2 - (float) $lon1) / 2);
//Distance in meters
$a = (float) ( (float)$alpha * (float)$alpha) + (float) ( (float)cos( (float)toRadians($lat1)) * (float)cos( (float)toRadians($lat2)) * (float)$beta * (float)$beta );
$dist = 2 * 6369628.75 * (float)atan2((float)sqrt((float)$a), (float)sqrt(1 - (float) $a));
$d = (float)sqrt((float)pow((float)$dist, 2) + pow((float) $lat1 - (float)$lat2, 2));
return sqrt((float)pow((float)$ele1-(float)$ele2,2)+(float)pow((float)$d,2));
}
2011-12-19 13:27:22 +00:00
}
2012-02-29 14:01:40 +00:00
2012-02-29 17:39:25 +00:00
function my_date_diff($old_date, $new_date) {
2012-02-29 14:01:40 +00:00
$t1 = strtotime($new_date);
$t2 = strtotime($old_date);
// milliceconds fix
$t1 += date_getDecimals($new_date);
$t2 += date_getDecimals($old_date);
$offset = (float)($t1 - $t2);
//echo "$offset = $new_date - $old_date; ".strtotime($new_date)." ".strtotime($old_date)." <br />";
return $offset;
}
function date_getDecimals($date)
{
if (preg_match('(\.([0-9]{2})Z?)', $date, $matches))
{
return (float)((float)$matches[1] / 100);
}
else
{
return 0;
}
}
2011-12-20 15:37:26 +00:00
2011-12-14 10:35:25 +00:00
?>