Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #3 from rb2k/master

Make tests and code ruby 1.9 compatible
  • Loading branch information...
commit 4bca65aeeca65fbf97c6c73f2da6138173ae2047 2 parents 7cf19bf + 0d5e31f
Doug Fales dougfales authored
4 gpx.gemspec
... ... @@ -1,5 +1,4 @@
1   -require 'xml/libxml'
2   -require 'lib/gpx/gpx' # load this and xml/libxml just to get GPX::VERSION
  1 +require './lib/gpx/gpx' # load this just to get GPX::VERSION
3 2 require 'rake' # For FileList
4 3 Gem::Specification.new do |s|
5 4 s.name = 'gpx'
@@ -13,4 +12,5 @@ Gem::Specification.new do |s|
13 12 s.email = "doug.fales@gmail.com"
14 13 s.homepage = "http://dougfales.github.com/gpx/"
15 14 s.rubyforge_project = "gpx"
  15 + s.add_dependency('hpricot')
16 16 end
2  lib/gpx.rb
@@ -22,7 +22,7 @@
22 22 #++
23 23 $:.unshift(File.dirname(__FILE__))
24 24 require 'rubygems'
25   -require 'xml/libxml'
  25 +require 'hpricot'
26 26 require 'date'
27 27 require 'time'
28 28 require 'csv'
11 lib/gpx/gpx.rb
@@ -21,12 +21,11 @@
21 21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 22 #++
23 23 module GPX
24   - VERSION = "0.6"
  24 + VERSION = "0.7"
25 25
26 26 # A common base class which provides a useful initializer method to many
27 27 # class in the GPX library.
28 28 class Base
29   - include XML
30 29
31 30 # This initializer can take an XML::Node and scrape out any text
32 31 # elements with the names given in the "text_elements" array. Each
@@ -35,11 +34,11 @@ class Base
35 34 # have to pick out individual text elements in each initializer of each
36 35 # class (Route, TrackPoint, Track, etc). Just pass an array of possible
37 36 # attributes to this method.
38   - def instantiate_with_text_elements(parent, text_elements, ns)
  37 + def instantiate_with_text_elements(parent, text_elements)
39 38 text_elements.each do |el|
40   - child_xpath = "gpx:#{el}"
41   - unless parent.find(child_xpath, ns).empty?
42   - val = parent.find(child_xpath, ns).first.content
  39 + child_xpath = "//#{el}"
  40 + unless parent.at(child_xpath).nil?
  41 + val = parent.at(child_xpath).inner_text
43 42 self.send("#{el}=", val)
44 43 end
45 44 end
31 lib/gpx/gpx_file.rb
@@ -52,20 +52,19 @@ def initialize(opts = {})
52 52 # gpx_file = File.open(gpx_file)
53 53 #end
54 54 gpx_file = gpx_file.name if gpx_file.is_a?(File)
55   - @xml = XML::Document.file(gpx_file)
  55 + @xml = Hpricot(File.open(gpx_file))
56 56 else
57   - parser = XML::Parser.string(opts[:gpx_data])
58   - @xml = parser.parse
  57 + @xml = Hpricot(opts[:gpx_data])
59 58 end
60 59 # set XML namespace for XML find
61   - if @xml.root.namespaces.namespace
62   - @ns = 'gpx:' + @xml.root.namespaces.namespace.href
63   - else
64   - @ns = 'gpx:http://www.topografix.com/GPX/1/1' # default to GPX 1.1
65   - end
  60 + #if @xml.root.namespaces.namespace
  61 + # @ns = 'gpx:' + @xml.root.namespaces.namespace.href
  62 + #else
  63 + # @ns = 'gpx:http://www.topografix.com/GPX/1/1' # default to GPX 1.1
  64 + #end
66 65
67 66 reset_meta_data
68   - bounds_element = (@xml.find("//gpx:gpx/gpx:metadata/gpx:bounds", @ns).to_a.first rescue nil)
  67 + bounds_element = (@xml.at("//metadata/bounds") rescue nil)
69 68 if bounds_element
70 69 @bounds.min_lat = get_bounds_attr_value(bounds_element, %w{ min_lat minlat minLat })
71 70 @bounds.min_lon = get_bounds_attr_value(bounds_element, %w{ min_lon minlon minLon})
@@ -75,20 +74,18 @@ def initialize(opts = {})
75 74 get_bounds = true
76 75 end
77 76
78   - @time = Time.parse(@xml.find("//gpx:gpx/gpx:metadata/gpx:time", @ns).first.content) rescue nil
79   - @name = @xml.find("//gpx:gpx/gpx:metadata/gpx:name", @ns).first.content rescue nil
80   -
  77 + @time = Time.parse(@xml.at("//metadata/time").inner_text) rescue nil
  78 + @name = @xml.at("//metadata/name").inner_text rescue nil
81 79 @tracks = []
82   - @xml.find("//gpx:gpx/gpx:trk", @ns).each do |trk|
  80 + @xml.search("//trk").each do |trk|
83 81 trk = Track.new(:element => trk, :gpx_file => self)
84 82 update_meta_data(trk, get_bounds)
85 83 @tracks << trk
86 84 end
87 85 @waypoints = []
88   - @xml.find("//gpx:gpx/gpx:wpt", @ns).each { |wpt| @waypoints << Waypoint.new(:element => wpt, :gpx_file => self) }
  86 + @xml.search("//wpt").each { |wpt| @waypoints << Waypoint.new(:element => wpt, :gpx_file => self) }
89 87 @routes = []
90   - @xml.find("//gpx:gpx/gpx:rte", @ns).each { |rte| @routes << Route.new(:element => rte, :gpx_file => self) }
91   -
  88 + @xml.search("//rte").each { |rte| @routes << Route.new(:element => rte, :gpx_file => self) }
92 89 @tracks.delete_if { |t| t.empty? }
93 90
94 91 calculate_duration
@@ -222,7 +219,7 @@ def generate_xml_doc
222 219 doc = Document.new
223 220 doc.root = Node.new('gpx')
224 221 gpx_elem = doc.root
225   - gpx_elem['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
  222 + gpx_elem['xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
226 223 @version = '1.1' if (@version.nil? || !(['1.0', '1.1'].include?(@version))) # default to version 1.1 of the schema (only version 1.0 and 1.1 of the schema exist)
227 224 version_dir = @version.gsub('.','/')
228 225 gpx_elem['xmlns'] = @ns || "http://www.topografix.com/GPX/#{version_dir}"
4 lib/gpx/magellan_track_log.rb
@@ -60,11 +60,11 @@ class << self
60 60
61 61 # Takes the name of a magellan file, converts the contents to GPX, and
62 62 # writes the result to gpx_filename.
63   - def convert_to_gpx(magellan_filename, gpx_filename)
  63 + def convert_to_gpx(magellan_filename, gpx_filename)
64 64
65 65 segment = Segment.new
66 66
67   - CSV.open(magellan_filename, "r") do |row|
  67 + CSV.open(magellan_filename, "r").each do |row|
68 68 next if(row.size < 10 or row[INVALID_FLAG] == 'V')
69 69
70 70 lat_deg = row[LAT][0..1]
6 lib/gpx/point.rb
@@ -38,9 +38,9 @@ def initialize(opts = {:lat => 0.0, :lon => 0.0, :elevation => 0.0, :time => Tim
38 38 @lat, @lon = elem["lat"].to_f, elem["lon"].to_f
39 39 @latr, @lonr = (D_TO_R * @lat), (D_TO_R * @lon)
40 40 #'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
41   - @time = (Time.xmlschema(elem.find("gpx:time", @gpx_file.ns).first.content) rescue nil)
42   - @elevation = elem.find("gpx:ele", @gpx_file.ns).first.content.to_f unless elem.find("gpx:ele", @gpx_file.ns).empty?
43   - @speed = elem.find("gpx:speed", @gpx_file.ns).first.content.to_f unless elem.find("gpx:speed", @gpx_file.ns).empty?
  41 + @time = (Time.xmlschema(elem.at("time").inner_text) rescue nil)
  42 + @elevation = elem.at("ele").inner_text.to_f unless elem.at("ele").nil?
  43 + @speed = elem.at("speed").inner_text.to_f unless elem.at("speed").nil?
44 44 else
45 45 @lat = opts[:lat]
46 46 @lon = opts[:lon]
4 lib/gpx/route.rb
@@ -34,9 +34,9 @@ def initialize(opts = {})
34 34 if(opts[:gpx_file] and opts[:element])
35 35 rte_element = opts[:element]
36 36 @gpx_file = opts[:gpx_file]
37   - @name = rte_element.find("child::gpx:name", @gpx_file.ns).first.content
  37 + @name = rte_element.at("//name").inner_text
38 38 @points = []
39   - rte_element.find("child::gpx:rtept", @gpx_file.ns).each do |point|
  39 + rte_element.search("//rtept").each do |point|
40 40 @points << Point.new(:element => point, :gpx_file => @gpx_file)
41 41 end
42 42 else
4 lib/gpx/segment.rb
@@ -46,8 +46,8 @@ def initialize(opts = {})
46 46 if(opts[:element])
47 47 segment_element = opts[:element]
48 48 last_pt = nil
49   - if segment_element.is_a?(XML::Node)
50   - segment_element.find("child::gpx:trkpt", @gpx_file.ns).each do |trkpt|
  49 + if segment_element.is_a?(Hpricot::Elem)
  50 + segment_element.search("//trkpt").each do |trkpt|
51 51 pt = TrackPoint.new(:element => trkpt, :segment => self, :gpx_file => @gpx_file)
52 52 unless pt.time.nil?
53 53 @earliest_point = pt if(@earliest_point.nil? or pt.time < @earliest_point.time)
9 lib/gpx/track.rb
@@ -41,8 +41,8 @@ def initialize(opts = {})
41 41 reset_meta_data
42 42 if(opts[:element])
43 43 trk_element = opts[:element]
44   - @name = (trk_element.find("child::gpx:name", @gpx_file.ns).first.content rescue "")
45   - trk_element.find("child::gpx:trkseg", @gpx_file.ns).each do |seg_element|
  44 + @name = (trk_element.at("//name").inner_text rescue "")
  45 + trk_element.search("//trkseg").each do |seg_element|
46 46 seg = Segment.new(:element => seg_element, :track => self, :gpx_file => @gpx_file)
47 47 update_meta_data(seg)
48 48 @segments << seg
@@ -69,8 +69,8 @@ def contains_time?(time)
69 69 # correlating things like pictures, video, and other events, if you are
70 70 # working with a timestamp.
71 71 def closest_point(time)
72   - segment = segments.find { |s| s.contains_time?(time) }
73   - segment.closest_point(time)
  72 + segment = segments.select { |s| s.contains_time?(time) }
  73 + segment.first
74 74 end
75 75
76 76 # Removes all points outside of a given area and updates the meta data.
@@ -114,6 +114,7 @@ def to_xml
114 114
115 115 # Prints out a friendly summary of this track (sans points). Useful for
116 116 # debugging and sanity checks.
  117 +
117 118 def to_s
118 119 result = "Track \n"
119 120 result << "\tName: #{name}\n"
2  lib/gpx/waypoint.rb
@@ -46,7 +46,7 @@ def initialize(opts = {})
46 46 wpt_elem = opts[:element]
47 47 @gpx_file = opts[:gpx_file]
48 48 super(:element => wpt_elem, :gpx_file => @gpx_file)
49   - instantiate_with_text_elements(wpt_elem, SUB_ELEMENTS, @gpx_file.ns)
  49 + instantiate_with_text_elements(wpt_elem, SUB_ELEMENTS)
50 50 else
51 51 opts.each do |key, value|
52 52 assignment_method = "#{key}="
18 tests/segment_test.rb
@@ -12,11 +12,11 @@ def setup
12 12
13 13 def test_segment_read
14 14 assert_equal(189, @segment.points.size)
15   - assert_equal("Fri Apr 07 18:12:05 UTC 2006", @segment.earliest_point.time.to_s)
16   - assert_equal("Fri Apr 07 19:26:31 UTC 2006", @segment.latest_point.time.to_s)
  15 + assert_equal(1144433525, @segment.earliest_point.time.to_i)
  16 + assert_equal(1144437991, @segment.latest_point.time.to_i)
17 17 assert_equal(1334.447, @segment.lowest_point.elevation)
18 18 assert_equal(1480.087, @segment.highest_point.elevation)
19   - assert_equal("6.98803359528853", @segment.distance.to_s)
  19 + assert_in_delta(6.98803359528853, @segment.distance, 0.001)
20 20 end
21 21
22 22 def test_segment_crop
@@ -27,9 +27,9 @@ def test_segment_crop
27 27 @segment.crop(crop_rectangle)
28 28
29 29 assert_equal(106, @segment.points.size)
30   - assert_equal("4.11422061733046", @segment.distance.to_s)
31   - assert_equal("Fri Apr 07 18:37:21 UTC 2006", @segment.earliest_point.time.to_s)
32   - assert_equal("Fri Apr 07 19:22:32 UTC 2006", @segment.latest_point.time.to_s)
  30 + assert_in_delta(4.11422061733046, @segment.distance, 0.001)
  31 + assert_equal(1144435041, @segment.earliest_point.time.to_i)
  32 + assert_equal(1144437752, @segment.latest_point.time.to_i)
33 33 assert_equal(1407.027, @segment.lowest_point.elevation)
34 34 assert_equal(1480.087, @segment.highest_point.elevation)
35 35 assert_equal(39.173834, @segment.bounds.min_lat)
@@ -45,9 +45,9 @@ def test_segment_delete
45 45 :max_lon=> -108.999000)
46 46 @segment.delete_area(delete_rectangle)
47 47 assert_equal(83, @segment.points.size)
48   - assert_equal("3.35967118153605", @segment.distance.to_s)
49   - assert_equal("Fri Apr 07 18:12:05 UTC 2006", @segment.earliest_point.time.to_s)
50   - assert_equal("Fri Apr 07 19:26:31 UTC 2006", @segment.latest_point.time.to_s)
  48 + assert_in_delta(3.35967118153605, @segment.distance, 0.001)
  49 + assert_equal(1144433525, @segment.earliest_point.time.to_i)
  50 + assert_equal(1144437991, @segment.latest_point.time.to_i)
51 51 assert_equal(1334.447, @segment.lowest_point.elevation)
52 52 assert_equal(1428.176, @segment.highest_point.elevation)
53 53 assert_equal(39.180572, @segment.bounds.min_lat)
4 tests/track_test.rb
@@ -13,7 +13,7 @@ def test_track_read
13 13 assert_equal("ACTIVE LOG", @track.name)
14 14 assert_equal( 182, @track.points.size)
15 15 assert_equal(8, @track.segments.size)
16   - assert_equal("3.07249668492626", @track.distance.to_s)
  16 + assert_in_delta(3.07249668492626, @track.distance, 0.001)
17 17 assert_equal(1267.155, @track.lowest_point.elevation)
18 18 assert_equal(1594.003, @track.highest_point.elevation)
19 19 assert_equal(38.681488, @track.bounds.min_lat)
@@ -32,7 +32,7 @@ def test_track_crop
32 32 assert_equal("ACTIVE LOG", @track.name)
33 33 assert_equal( 111, @track.points.size)
34 34 assert_equal(4, @track.segments.size)
35   - assert_equal("1.62136024923607", @track.distance.to_s)
  35 + assert_in_delta(1.62136024923607, @track.distance, 0.001)
36 36 assert_equal(1557.954, @track.lowest_point.elevation)
37 37 assert_equal(1582.468, @track.highest_point.elevation)
38 38 assert_equal(38.782511, @track.bounds.min_lat)

0 comments on commit 4bca65a

Please sign in to comment.
Something went wrong with that request. Please try again.