Created
January 24, 2015 14:15
-
-
Save vojkny/ab9e7208e8a9d3016551 to your computer and use it in GitHub Desktop.
GPX interleave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$inputFile = 'file.gpx'; | |
$start = '2015-01-24 08:46:05'; | |
$speed = '19'; | |
function dist( | |
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) { | |
// convert from degrees to radians | |
$latFrom = deg2rad($latitudeFrom); | |
$lonFrom = deg2rad($longitudeFrom); | |
$latTo = deg2rad($latitudeTo); | |
$lonTo = deg2rad($longitudeTo); | |
$lonDelta = $lonTo - $lonFrom; | |
$a = pow(cos($latTo) * sin($lonDelta), 2) + | |
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); | |
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); | |
$angle = atan2(sqrt($a), $b); | |
return $angle * $earthRadius; | |
} | |
$gps = file_get_contents($inputFile); | |
$start = @strtotime($start); | |
$speed = $speed / 3.6; // m/s | |
$last = false; | |
foreach (preg_split("#\s*\n\s*#", $gps) as $line) { | |
$regs = array(); | |
if (preg_match('#<trkpt\s*lat="(.*?)"\s*lon="(.*?)"/>#', $line, $regs)) { | |
if ($lastLat) { | |
$dist = dist($lastLat, $lastLon, $regs[1], $regs[2]); | |
$diff = $dist / ($speed * rand(8,12) / 10); | |
$start += round($diff); | |
echo "<trkpt lat=\"$regs[1]\" lon=\"$regs[2]\"><time>".@date('c', $start)."</time></trkpt>"; | |
} else { | |
echo $regs[0]; | |
} | |
$lastLat = $regs[1]; | |
$lastLon = $regs[2]; | |
echo "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment