Patches from Tom Verbeure (mtbguru.com) to work with libxml-ruby 1.x.

master
Doug Fales 2009-07-07 02:41:33 +00:00
parent 73ea9e6309
commit cc5aa666bc
3 changed files with 42 additions and 7 deletions

View File

@ -54,13 +54,12 @@ module GPX
gpx_file = gpx_file.name if gpx_file.is_a?(File) gpx_file = gpx_file.name if gpx_file.is_a?(File)
@xml = XML::Document.file(gpx_file) @xml = XML::Document.file(gpx_file)
else else
parser = XML::Parser.new parser = XML::Parser.string(opts[:gpx_data])
parser.string = opts[:gpx_data]
@xml = parser.parse @xml = parser.parse
end end
# set XML namespace for XML find # set XML namespace for XML find
if @xml.root.namespace_node if @xml.root.namespaces.namespace
@ns = 'gpx:' + @xml.root.namespace_node.href @ns = 'gpx:' + @xml.root.namespaces.namespace.href
else else
@ns = 'gpx:http://www.topografix.com/GPX/1/1' # default to GPX 1.1 @ns = 'gpx:http://www.topografix.com/GPX/1/1' # default to GPX 1.1
end end
@ -197,8 +196,8 @@ module GPX
# you modify the GPX data (i.e. by adding or deleting points) and you # you modify the GPX data (i.e. by adding or deleting points) and you
# want the meta data to accurately reflect the new data. # want the meta data to accurately reflect the new data.
def update_meta_data(trk, get_bounds = true) 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) @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.elevation > @highest_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 @bounds.add(trk.bounds) if get_bounds
@distance += trk.distance @distance += trk.distance
end end
@ -236,7 +235,7 @@ module GPX
waypoints.each { |w| gpx_elem << w.to_xml } unless waypoints.nil? waypoints.each { |w| gpx_elem << w.to_xml } unless waypoints.nil?
routes.each { |r| gpx_elem << r.to_xml } unless routes.nil? routes.each { |r| gpx_elem << r.to_xml } unless routes.nil?
doc.save(filename, true) doc.save(filename, :indent => true)
end end
private private

View File

@ -4,6 +4,7 @@ require File.dirname(__FILE__) + '/../lib/gpx'
class GPXFileTest < Test::Unit::TestCase class GPXFileTest < Test::Unit::TestCase
ONE_TRACK_FILE = File.join(File.dirname(__FILE__), "gpx_files/one_track.gpx") 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") BIG_FILE = File.join(File.dirname(__FILE__), "gpx_files/big.gpx")
def test_load_data_from_string def test_load_data_from_string
@ -38,4 +39,10 @@ class GPXFileTest < Test::Unit::TestCase
assert_equal(7968, gpx_file.tracks.first.points.size) assert_equal(7968, gpx_file.tracks.first.points.size)
end 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 end

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.5" 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">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2007-04-16T18:11:47Z</time>
<bounds maxlat="40.207429" maxlon="116.670578" minlat="39.876895" minlon="101.636217"/>
</metadata>
<trk>
<name>ACTIVE LOG</name>
<trkseg>
<trkpt lat="40.079434" lon="116.295948">
<ele>50.606567</ele>
<time>2007-03-25T05:17:34Z</time>
</trkpt>
</trkseg>
</trk>
<trk>
<name>LINE-13</name>
<trkseg>
<trkpt lat="39.949744" lon="116.427398"/>
</trkseg>
</trk>
</gpx>