Skip to content

Instantly share code, notes, and snippets.

@GBH
Last active March 25, 2016 14:42
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 GBH/93bcdacacd4b4d702e1d to your computer and use it in GitHub Desktop.
Save GBH/93bcdacacd4b4d702e1d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Small script to join and clean-up ACA gpx files.
# Setup:
# - make sure you got nokogiri installed (gem install nokogiri)
# - throw this merger.rb in some directory
# - inside that directory create input/ folder
# - copy ACA .gpx files into that folder. Generally {direction}Main files plus Services files
# Usage:
# - from terminal run ./merger.rb
# - output.gpx will be created in the directory where this script is at
# - now you can import it into Google maps
require 'nokogiri'
point_data = Hash.new { |hash, key| hash[key] = [] }
wp_data = Hash.new { |hash, key| hash[key] = [] }
Dir[File.join(File.dirname(__FILE__), 'input', '**', "*.gpx")].each do |file|
puts file.inspect
doc = File.open(file){|f| Nokogiri::XML(f)}
points = doc.css('trkpt')
points.each do |point|
point_data[File.basename(file)] << [point['lat'], point['lon']]
end
waypoints = doc.css('wpt')
waypoints.each do |wp|
# Ignoring all Waypoints except campgrounds
if wp.css('sym').text == 'Campground'
wp_data[File.basename(file)] << [wp['lat'], wp['lon'], wp.css('name').text]
end
end
end
builder = Nokogiri::XML::Builder.new do |xml|
xml.gpx(
:creator => 'merger.rb',
:version => "0.0",
: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"
) do
point_data.each do |segment, points|
xml.trk do
xml.name segment
xml.trkseg do
points.each do |lat, lon|
xml.trkpt(:lat => lat, :lon => lon)
end
end
end
end
wp_data.each do |segment, waypoints|
waypoints.each do |lat, lon, name|
xml.wpt(:lat => lat, :lon => lon) do
xml.name name
end
end
end
end
end
File.open(File.join(File.dirname(__FILE__), 'output.gpx'), 'w'){|file| file.write(builder.to_xml)}
puts 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment