Created
May 21, 2012 15:56
-
-
Save stoermerjp/2763012 to your computer and use it in GitHub Desktop.
DroneMapper.com: GPX File Generation from UAV Flight Controller Logs
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
#!/usr/bin/php | |
<?php | |
/* | |
* Change date_default_timezone_set to your flight timezone, you will also | |
* need to add a -geosync offset for exiftool on the command line if the | |
* time (hour) field doesn't match. | |
*/ | |
date_default_timezone_set('Europe/Berlin'); | |
/* | |
* Input/Output files | |
*/ | |
$incomingfile = "./MAVinci.csvm"; | |
$outputfile = "./MAVinci.gpx"; | |
function getGps($exifCoord, $hemi) { | |
$degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0; | |
$minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0; | |
$seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0; | |
$flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1; | |
return $flip * ($degrees + $minutes / 60 + $seconds / 3600); | |
} | |
function gps2Num($coordPart) { | |
$parts = explode('/', $coordPart); | |
if (count($parts) <= 0) | |
return 0; | |
if (count($parts) == 1) | |
return $parts[0]; | |
return floatval($parts[0]) / floatval($parts[1]); | |
} | |
/* | |
* Build GPX | |
*/ | |
$mygpx = ''; | |
$mygpx .= '<?xml version="1.0"?>'; | |
$mygpx .= "\n"; | |
$mygpx .= '<gpx creator="http://dronemapper.com" version="1.0" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">'; | |
$mygpx .= "\n"; | |
$mygpx .= '<trk>'; | |
$mygpx .= "\n"; | |
$mygpx .= "\t".'<number>1</number>'; | |
$mygpx .= "\n"; | |
$mygpx .= "\t".'<name>DroneMapper UAV</name>'; | |
$mygpx .= "\n"; | |
$mygpx .= "\t\t<trkseg>\n"; | |
$row = ''; | |
if (($handle = fopen($incomingfile, "r")) !== FALSE) { | |
/* | |
* Use fgetcsv with field delimited of " " | |
* You can change this to "," or "\t" for different formats. | |
*/ | |
while (($data = fgetcsv($handle, 1000, " ")) !== FALSE) { | |
$num = count($data); | |
$row++; | |
/* Skip headers */ | |
if(!preg_match("/JPG/i", $data[2])) { | |
continue; | |
} | |
/* | |
* You can change the $data[X] to obtain different log fields. | |
* This is useful if you are processing a UAV log from a different | |
* company or you generate your own. | |
*/ | |
print "\tGPS Timestamp: " . $data[3] . "\n"; | |
print "\tGPS Lat: " . $data[5] . "\n"; | |
print "\tGPS Lon: " . $data[4] . "\n"; | |
print "\tGPS Altitude: " . $data[13] . "\n"; | |
$mygpx .= "\t\t\t<trkpt lat=\"".$data[5]."\" lon=\"".$data[4]."\">\n"; | |
$mygpx .= "\t\t\t\t"; | |
$mygpx .= '<ele>'.$data[13].'</ele>'."\n"; | |
$mygpx .= "\t\t\t\t"; | |
$mygpx .= '<time>'.date("Y-m-d\TH:i:s\Z", $data[22]).'</time>'."\n"; | |
$mygpx .= "\t\t\t"; | |
$mygpx .= '</trkpt>'."\n"; | |
} | |
fclose($handle); | |
} | |
$mygpx .= "\n\t\t"; | |
$mygpx .= '</trkseg>'; | |
$mygpx .= "\n\t"; | |
$mygpx .= '</trk>'; | |
$mygpx .= "\n"; | |
$mygpx .= '</gpx>'; | |
/* Write GPX */ | |
file_put_contents($outputfile, $mygpx); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment