Created
January 31, 2011 00:55
-
-
Save ptrv/803497 to your computer and use it in GitHub Desktop.
Change track names in GPX files (ruby)
This file contains hidden or 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/env ruby | |
# Script for assigning the date of a track in a GPX file as track name. | |
# | |
# Usage e.g $ ruby ChangeGpxTrackName.rb source.gpx target.gpx" | |
# | |
# Peter Vasil | |
# Date: 2011-01-31 | |
require 'rexml/document' | |
include REXML | |
# unless ARGV.length == 1 || ARGV.length == 2 | |
# puts "You should give a file name to work on!" | |
# exit | |
# end | |
unless ARGV.length == 2 | |
puts "You should give a source file path and destination file path!" | |
exit | |
end | |
# if ARGV.length == 1 | |
# filename = ARGV[0] | |
# new_filename = filename | |
# elsif ARGV.length == 2 | |
# filename = ARGV[0] | |
# new_filename = ARGV[1] | |
# end | |
if ARGV.length == 2 | |
filename = ARGV[0] | |
new_filename = ARGV[1] | |
end | |
gpxfile = File.new(filename) | |
doc = Document.new(gpxfile) | |
gpxfile.close | |
root = doc.root | |
puts "Processing GPX file..." | |
trk_num = 0 | |
root.each_element('trk') do |elem| | |
trk_num += 1 | |
old_name = elem.elements['name'].text | |
new_name = elem.elements['trkseg/trkpt/time'].text | |
puts "-----------------------------------------------" | |
puts "Track " + trk_num.to_s | |
puts "Old name: " + old_name | |
puts "New name: " + new_name | |
elem.elements[1].text = new_name | |
end | |
puts "-----------------------------------------------" | |
new_doc = Document.new | |
new_doc << XMLDecl.new | |
new_doc << root | |
new_gpxfile = File.open(new_filename, "w") | |
new_gpxfile.write new_doc | |
new_gpxfile.close | |
puts "Finished processing!" | |
puts "Wrote file '" + new_filename + "'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment