From cc5aa666bcfb5b8d353e67769fb875ae686be454 Mon Sep 17 00:00:00 2001 From: Doug Fales Date: Tue, 7 Jul 2009 02:41:33 +0000 Subject: [PATCH] Patches from Tom Verbeure (mtbguru.com) to work with libxml-ruby 1.x. --- lib/gpx/gpx_file.rb | 13 +++++------ tests/gpx_file_test.rb | 7 ++++++ tests/gpx_files/with_or_without_elev.gpx | 29 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 tests/gpx_files/with_or_without_elev.gpx diff --git a/lib/gpx/gpx_file.rb b/lib/gpx/gpx_file.rb index 03bfd99..6fe8b74 100644 --- a/lib/gpx/gpx_file.rb +++ b/lib/gpx/gpx_file.rb @@ -54,13 +54,12 @@ module GPX gpx_file = gpx_file.name if gpx_file.is_a?(File) @xml = XML::Document.file(gpx_file) else - parser = XML::Parser.new - parser.string = opts[:gpx_data] + parser = XML::Parser.string(opts[:gpx_data]) @xml = parser.parse end # set XML namespace for XML find - if @xml.root.namespace_node - @ns = 'gpx:' + @xml.root.namespace_node.href + if @xml.root.namespaces.namespace + @ns = 'gpx:' + @xml.root.namespaces.namespace.href else @ns = 'gpx:http://www.topografix.com/GPX/1/1' # default to GPX 1.1 end @@ -197,8 +196,8 @@ module GPX # you modify the GPX data (i.e. by adding or deleting points) and you # want the meta data to accurately reflect the new data. def update_meta_data(trk, get_bounds = true) - @lowest_point = trk.lowest_point if(@lowest_point.nil? or trk.lowest_point.elevation < @lowest_point.elevation) - @highest_point = trk.highest_point if(@highest_point.nil? or trk.highest_point.elevation > @highest_point.elevation) + @lowest_point = trk.lowest_point if(@lowest_point.nil? or (!trk.lowest_point.nil? and trk.lowest_point.elevation < @lowest_point.elevation)) + @highest_point = trk.highest_point if(@highest_point.nil? or (!trk.highest_point.nil? and trk.highest_point.elevation > @highest_point.elevation)) @bounds.add(trk.bounds) if get_bounds @distance += trk.distance end @@ -236,7 +235,7 @@ module GPX waypoints.each { |w| gpx_elem << w.to_xml } unless waypoints.nil? routes.each { |r| gpx_elem << r.to_xml } unless routes.nil? - doc.save(filename, true) + doc.save(filename, :indent => true) end private diff --git a/tests/gpx_file_test.rb b/tests/gpx_file_test.rb index 0f7f542..a604b11 100644 --- a/tests/gpx_file_test.rb +++ b/tests/gpx_file_test.rb @@ -4,6 +4,7 @@ require File.dirname(__FILE__) + '/../lib/gpx' class GPXFileTest < Test::Unit::TestCase ONE_TRACK_FILE = File.join(File.dirname(__FILE__), "gpx_files/one_track.gpx") + WITH_OR_WITHOUT_ELEV_FILE = File.join(File.dirname(__FILE__), "gpx_files/with_or_without_elev.gpx") BIG_FILE = File.join(File.dirname(__FILE__), "gpx_files/big.gpx") def test_load_data_from_string @@ -38,4 +39,10 @@ class GPXFileTest < Test::Unit::TestCase assert_equal(7968, gpx_file.tracks.first.points.size) end + def test_with_or_with_elev + gpx_file = GPX::GPXFile.new(:gpx_file => WITH_OR_WITHOUT_ELEV_FILE) + assert_equal(2, gpx_file.tracks.size) + #assert_equal(7968, gpx_file.tracks.first.points.size) + end + end diff --git a/tests/gpx_files/with_or_without_elev.gpx b/tests/gpx_files/with_or_without_elev.gpx new file mode 100644 index 0000000..ce516a4 --- /dev/null +++ b/tests/gpx_files/with_or_without_elev.gpx @@ -0,0 +1,29 @@ + + + + + + Garmin International + + + + + + + ACTIVE LOG + + + 50.606567 + + + + + + + LINE-13 + + + + + +