Skip to content

Commit

Permalink
Patches from Tom Verbeure (mtbguru.com) to work with libxml-ruby 1.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
dougfales committed Jul 7, 2009
1 parent 73ea9e6 commit cc5aa66
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/gpx/gpx_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ def initialize(opts = {})
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
Expand Down Expand Up @@ -197,8 +196,8 @@ def reset_meta_data
# 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
Expand Down Expand Up @@ -236,7 +235,7 @@ def write(filename, update_time = true)
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
Expand Down
7 changes: 7 additions & 0 deletions tests/gpx_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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
Expand Down Expand Up @@ -38,4 +39,10 @@ def test_big_file
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
29 changes: 29 additions & 0 deletions tests/gpx_files/with_or_without_elev.gpx
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit cc5aa66

Please sign in to comment.