/*
?list = lists all saved tracks
?pos = add a new position
?kml = layer for google maps
?robots = list for robot
*/
$filename_postfix = "_coordinates.log";
// https://code.google.com/apis/console/b/0/?noredirect#project:1033363279592:access
// https://console.developers.google.com/project/1033363279592?authuser=0
$GOOGLE_MAPS_API_KEY = "AIzaSyCnY6pNSLY1ga9bXGPQtzcV_Gpfdf6E8b0";
// minimum accuracy required for painting on map - in Meter (usually 40min is minium)
$MIN_ACCURACY = 60;
// get a list of stored tracks
if(isset($_GET['list'])) {
$oFiles = new RegexIterator(
new IteratorIterator(
new DirectoryIterator('.')
),
'/[0-9]+'. $filename_postfix .'/',
RegexIterator::MATCH
);
if(empty($oFiles)) {
header("HTTP/1.0 404 Not Found");
echo 'no stored tracks';
exit(0);
}
foreach ($oFiles as $key => $value) {
$trackid = substr("".$value,0, strpos("".$value, '_'));
$startdate = date("d.m.Y H:i:s", $trackid/1000);
$time = filemtime("".$value);
$aFiles[$trackid] = array(
'start' => $trackid,
'end' => $time,
'file' => "".$value,
'startdate' => $startdate);
$links[$trackid] = ''.$startdate.' (map) '.
''.$startdate.' (kml) '.
''.$startdate.' (robot)';
}
ksort($aFiles);
ksort($links);
if(isset($_GET['js'])) {
//header("Content-type: application/json; charset=utf-8");
header("Content-type: text/javascript; charset=utf-8");
header('Content-Disposition: inline"');
echo json_encode($aFiles);
} else {
echo implode($links, '
');
}
exit(0);
}
// delivering KML history file
if(isset($_GET['kml']) || isset($_GET['kmz']) || isset($_GET['robot'])) {
if(empty($_GET['trackid'])) {
header("HTTP/1.0 406 Not Acceptable");
echo 'empty trackid';
exit(0);
}
$trackid = intval($_GET['trackid']);
if(empty($trackid)) {
header("HTTP/1.0 406 Not Acceptable");
echo 'invalid trackid';
exit(0);
}
//$coordinates = file_get_contents("coordinates.log");
$coordinates = file($trackid.$filename_postfix);
$lastupdate = filemtime($trackid.$filename_postfix);
if(isset($_GET['robot'])) {
header("Content-type: text/plain; charset=utf-8");
foreach ($coordinates as $idx => $coord) {
// TODO: change from lat/long/time to direction/speed
// http://www.movable-type.co.uk/scripts/latlong.html
list($long, $lat, $alt, $accuracy, $altitudeAccuracy, $heading, $speed, $time, $tracking) = explode("\t", $coord);
// cut out inaccuracies
if($accuracy > $MIN_ACCURACY) {
continue;
}
$speed = floatval($speed);
$heading = floatval($heading);
$lat = floatval($lat);
$long = floatval($long);
if(isset($last)) {
$time -= $starttime;
$time /= 1000.0;
$lat1 = deg2rad($last['lat']);
$lat2 = deg2rad($lat);
$deltaLat = $lat2 - $lat1;
$deltaLong = deg2rad($long - $last['long']);
$a = sin($deltaLat/2) * sin($deltaLat/2) + cos($lat1)*cos($lat2)*sin($deltaLong/2)*sin($deltaLong/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$distance = 6371000 * $c;
$deltaTime = ($time - $last['time']);
if($deltaTime < 0.5) continue;
$cspeed = $distance / $deltaTime; // m/s
$lat2 = deg2rad($lat);
$lat1 = deg2rad($last['lat']);
$y = sin($deltaLong) * cos($lat2);
$x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($deltaLong);
$cheading = rad2deg(atan2($y, $x));
if($cheading < 0) $cheading += 360;
} else {
$cheading = 0;
$cspeed = 0;
$starttime = $time;
$time = 0;
}
if($cspeed < 0.1) {
// $cspeed = 0; $cheading = 0;
}
$last = array('lat' => $lat, 'long' => $long, 'time' => $time);
$tracking = intval($tracking);
//$speed *= 3.6; // km/h
$output[] = "$time\t$tracking\t$heading\t$speed\t$cheading\t$cspeed";
}
} else if(isset($_GET['kml']) || isset($_GET['kmz'])) {
$output = [];
foreach ($coordinates as $idx => $coord) {
list($long, $lat, $alt, $accuracy, $altitudeAccuracy, $heading, $speed, $time, $tracking) = explode("\t", $coord);
// cut out inaccuracies
if($accuracy > $MIN_ACCURACY) {
continue;
}
if(!intval($tracking)) {
continue;
}
$lat = floatval($lat);
$long = floatval($long);
$alt = floatval($alt);
$line = trim("$long,$lat,$alt");
// cut out duplicates
if(count($output) > 0 && trim($output[count($output) - 1]) == $line) {
continue;
}
$output[] = $line;
}
}
if(empty($output)) {
header("HTTP/1.0 404 Not Found");
echo 'no coordinates for this track';
exit(0);
}
if(isset($_GET['robot'])) {
$output = implode("\n", $output);
header("Content-type: text/plain; charset=utf-8");
header('Content-Disposition: inline"');
print $output;
exit(0);
}
else if(isset($_GET['kml']) || isset($_GET['kmz'])) {
// see: https://developers.google.com/kml/documentation/kmlreference
$output =
'