Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Created November 26, 2011 19:03
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 otobrglez/1396138 to your computer and use it in GitHub Desktop.
Save otobrglez/1396138 to your computer and use it in GitHub Desktop.
Simple script that I use for mergin GPX tracks.
#!/usr/bin/env ruby
require "nokogiri"
require "pathname"
require "date"
gpx_root_folder = "/"+["Users", ENV["USER"], "mainnav-tracklogs"].join("/")
if ARGV[0].nil? or ARGV[1].nil?
puts "Missing dates!"
end
diff_dates = []
(Date.strptime(ARGV[0],"%y-%m-%d")).upto(Date.strptime(ARGV[1],"%y-%m-%d")) do |date|
diff_dates << date.strftime("%y-%m-%d")
end
files = Dir.glob(gpx_root_folder+"/*/*").select do |v|
m = /track_(\d.*)-(\d.*)-(\d.*)_/ =~ v
diff_dates.include? "#{Regexp.last_match[1]}-#{Regexp.last_match[2]}-#{Regexp.last_match[3]}"
end
files.sort! do |a,b|
p_a = /track_(\d.*)-(\d.*)-(\d.*)_/ =~ a
d_a = Date.strptime("#{Regexp.last_match[1]}-#{Regexp.last_match[2]}-#{Regexp.last_match[3]}","%y-%m-%d")
p_b = /track_(\d.*)-(\d.*)-(\d.*)_/ =~ b
d_b = Date.strptime("#{Regexp.last_match[1]}-#{Regexp.last_match[2]}-#{Regexp.last_match[3]}","%y-%m-%d")
d_a <=> d_b
end
# Merge
f_name = "merged_#{ARGV[0]}_to_#{ARGV[1]}.gpx"
puts "Building #{f_name}"
f_out = File.open(f_name,"w")
f_out.write '<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="Oto Brglez - gpx_joiner" version="1.1"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
'
files.each do |path|
puts "Working with: #{path}"
c_file = File.open(path)
doc = Nokogiri::XML(c_file)
f_out.write(doc.xpath("//n:trk","n" => "http://www.topografix.com/GPX/1/1").to_s)
c_file.close
end
f_out.write("</gpx>");
f_out.close
puts "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment