Skip to content

Instantly share code, notes, and snippets.

@cimm
Created July 19, 2010 21:20
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 cimm/482030 to your computer and use it in GitHub Desktop.
Save cimm/482030 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'gnuplot'
if ARGV[0]
source = ARGV[0]
else
puts "usage: ./gpx2image source_file"
exit(0)
end
doc = Nokogiri::XML(open(source))
minlat = (doc.xpath('/xmlns:gpx/xmlns:bounds/@minlat').to_s.to_f * 100).round.to_f / 100
maxlat = (doc.xpath('/xmlns:gpx/xmlns:bounds/@maxlat').to_s.to_f * 100).round.to_f / 100
minlon = (doc.xpath('/xmlns:gpx/xmlns:bounds/@minlon').to_s.to_f * 100).round.to_f / 100
maxlon = (doc.xpath('/xmlns:gpx/xmlns:bounds/@maxlon').to_s.to_f * 100).round.to_f / 100
# TODO Negative coordinates
latdiff = maxlat - minlat
londiff = maxlon - minlon
latmid = (minlat + maxlat) / 2
lonmid = (minlon + maxlon) / 2
if latdiff > londiff # portrait
minlon = lonmid - (latdiff / 2)
maxlon = lonmid + (latdiff / 2)
minlat = latmid - (latdiff / 2)
maxlat = latmid + (latdiff / 2)
else # landscape
minlat = latmid - (londiff / 2)
maxlat = latmid + (londiff / 2)
minlon = lonmid - (londiff / 2)
maxlon = lonmid + (londiff / 2)
end
x = doc.xpath('//xmlns:trkpt/@lon').map{|pt| pt.to_s.to_f}
y = doc.xpath('//xmlns:trkpt/@lat').map{|pt| pt.to_s.to_f}
Gnuplot.open do |gp|
Gnuplot::Plot.new(gp) do |plot|
# plot.arbitrary_lines << 'set terminal "png"'
# plot.arbitrary_lines << 'set output "some.png"'
# plot.arbitrary_lines << "unset border"
plot.arbitrary_lines << "set grid"
# plot.arbitrary_lines << "unset tics"
plot.arbitrary_lines << "set size square"
plot.arbitrary_lines << "set yrange [#{minlat}:#{maxlat}]"
plot.arbitrary_lines << "set xrange [#{minlon}:#{maxlon}]"
# plot.arbitrary_lines << 'set term svg'
plot.data << Gnuplot::DataSet.new([x, y]) do |ds|
ds.with = "lines"
ds.notitle
end
end
end
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment