Skip to content

Instantly share code, notes, and snippets.

@cimm
Created January 16, 2010 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cimm/279091 to your computer and use it in GitHub Desktop.
Save cimm/279091 to your computer and use it in GitHub Desktop.
This AppleScript converts a NMEA input file and converts all real coordinates to Google placemarks in a KML file.
property script_name : "NMEA2KML"
property script_version : "0.3"
property script_description : "This script converts a NMEA input file and converts all real coordinates to Google placemarks in a KML file."
property script_copyright : "Creative Commons Attribution 3.0 License"
property script_author : "Simon Schoeters"
property script_website : "http://www.suffix.be/"
-- Get the file and its path
set filePath to (choose file with prompt "Select a NMEA file:") as Unicode text
set filePath to replace_chars(filePath, ":", "/")
set filePath to text 5 thru -1 of filePath
-- Use only data for fixed coordinates (no generated data)
set fileContents to do shell script "grep GPGGA " & filePath
set fileLines to paragraphs of fileContents
set countCoordinates to count fileLines
set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
-- Get the different fields in the NMEA records
set route to ""
repeat with aLine in fileLines
set points to (every text item of aLine)
set logTime to {item 2 of points} as Unicode text
set logHour to text 1 thru 2 of logTime
set logMinutes to text 3 thru 4 of logTime
set logSeconds to text 5 thru 6 of logTime
set latitude to convert_to_deg({item 3 of points})
set longitude to convert_to_deg({item 5 of points})
set altitude to {item 10 of points}
set route to route & longitude & "," & latitude & "," & altitude & "
"
end repeat
set AppleScript's text item delimiters to oldDels
-- Create output file
set fileName to the text returned of (display dialog "Name your route:" default answer "myroute")
set kml to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<kml xmlns=\"http://earth.google.com/kml/2.0\">
<Placemark>
<description>Generated from NMEA file</description>
<name>" & fileName & "</name>
<LookAt>
<longitude>" & longitude & "</longitude>
<latitude>" & latitude & "</latitude>
<range>4451.842204068102</range>
<tilt>44.61038665812578</tilt>
<heading>-125</heading>
</LookAt>
<visibility>1</visibility>
<open>0</open>
<Style>
<LineStyle>
<color>ff00ffff</color>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>
" & route & "
</coordinates>
</LineString>
</Placemark>
</kml>"
set outputFile to (((path to desktop) as string) & fileName & ".kml") as file specification
my write_to_file(outputFile, kml)
-- Subroutines
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
on write_to_file(outFile, outData)
try
open for access outFile with write permission
set eof of outFile to 0
write (outData) to outFile starting at eof
close access outFile
on error
try
close access outFile
end try
end try
end write_to_file
on convert_to_deg(decimal)
set temp to decimal as number
set deg to round (temp / 100) rounding toward zero
set min to temp - (deg * 100)
return (deg * 1.0) + min / 60.0
end convert_to_deg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment