Skip to content

Instantly share code, notes, and snippets.

@ejain
Created November 15, 2013 06:03
Show Gist options
  • Save ejain/7479848 to your computer and use it in GitHub Desktop.
Save ejain/7479848 to your computer and use it in GitHub Desktop.
Converts a directory of GPX files into a single KML file for import into Google Fusion Tables; includes some metadata based on the file name.
import groovy.xml.MarkupBuilder
new MarkupBuilder().kml(xmlns : 'http://www.opengis.net/kml/2.2') {
new File(args?.size() ? args[0] : '.').eachFileMatch(~/.*\.gpx/) { file ->
new XmlParser().parse(file).trk.each { trk ->
trk.trkseg.each { trkseg ->
def geo = ""
trkseg.trkpt.each { trkpt ->
geo += "${trkpt.'@lon'},${trkpt.'@lat'}\n"
}
if (geo) {
Placemark() {
name(file.getName().substring(0, 8))
LineString {
coordinates(geo);
}
ExtendedData {
Data(name : 'mode') {
value(file.getName() =~ /car/ ? 'drive' : 'hike')
}
Data(name : 'mode_color') {
value(file.getName() =~ /car/ ? 'FF000080' : 'FFFF00FF')
}
Data(name : 'multiday') {
value(file.getName().contains('-'))
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment