Created
June 17, 2019 19:57
-
-
Save willpearse/b7ba4efba53cddf926c6388516ae6dd8 to your computer and use it in GitHub Desktop.
Make Garmin Waypoints (GPX files) from CSVs of site locations
This file contains 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/ruby | |
require 'optparse' | |
require 'csv' | |
options = {} | |
OptionParser.new do |opts| | |
#Defaults | |
options[:output] = nil | |
options[:input] = nil | |
options[:lat] = "lat" | |
options[:long] = "long" | |
options[:name] = "name" | |
opts.banner = "csv_to_gpx: Convert CSV to GPX for Garmin GPS\nUsage: csv_to_gpx.rb -i INPUT -o OUTPUT [options]" | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
opts.on("-o FILE", "--output FILE", "Filename/location for output GPX") {|x| options[:output] = x.to_s} | |
opts.on("-i FILE", "--input FILE", "Filename/location for input CSV") {|x| options[:input] = x.to_s} | |
opts.on("-x COLUMN", "--longitude COLUMN", "Column name for longitude in input (default: long)") {|x| options[:long] = x.to_s} | |
opts.on("-y COLUMN", "--latitude COLUMN", "Column name for latitude in input (default: lat)") {|x| options[:lat] = x.to_s} | |
opts.on("-n COLUMN", "--name COLUMN", "Column name for site name in input (default: name)") {|x| options[:name] = x.to_s} | |
opts.on("-") | |
end.parse! | |
# Argument handling | |
if options[:output].nil? or options[:input].nil? or not File.exist? options[:input] | |
puts "Must specify (valid) output and input filenames; exiting" | |
puts "Run ./csv_to_gpx.rb --help (or similar) to get options" | |
exit(false) | |
end | |
# Do work | |
File.open(options[:output], "wb") do |output| | |
output << '<?xml version="1.0" encoding="UTF-8" standalone="no" ?> | |
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="Oregon 500t" version="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 http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"> | |
<metadata> | |
<link href="http://pearselab.com"> | |
<text>Pearse Lab - RHF sites</text> | |
</link> | |
</metadata> | |
' | |
CSV.foreach(options[:input], headers: true) do |row| | |
output << " <wpt lat=\"#{row[options[:lat]]}\" lon=\"#{row[options[:long]]}\"> | |
<name>#{row[options[:name]]}</name> | |
<sym>Building</sym> | |
</wpt> | |
" | |
end | |
output << " | |
</gpx> | |
" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment