diff --git a/lib/gpx/gpx.rb b/lib/gpx/gpx.rb index db97cb8..b07536c 100644 --- a/lib/gpx/gpx.rb +++ b/lib/gpx/gpx.rb @@ -35,15 +35,12 @@ module GPX # have to pick out individual text elements in each initializer of each # class (Route, TrackPoint, Track, etc). Just pass an array of possible # attributes to this method. - def instantiate_with_text_elements(parent, text_elements) + def instantiate_with_text_elements(parent, text_elements, ns) text_elements.each do |el| - unless parent.find(el).empty? - val = parent.find(el).first.content - code = <<-code - attr_accessor #{ el } - #{el}=#{val} - code - class_eval code + child_xpath = "gpx:#{el}" + unless parent.find(child_xpath, ns).empty? + val = parent.find(child_xpath, ns).first.content + self.send("#{el}=", val) end end diff --git a/lib/gpx/point.rb b/lib/gpx/point.rb index 9173fea..6eccbf1 100644 --- a/lib/gpx/point.rb +++ b/lib/gpx/point.rb @@ -25,7 +25,7 @@ module GPX # The base class for all points. Trackpoint and Waypoint both descend from this base class. class Point < Base D_TO_R = PI/180.0; - attr_accessor :lat, :lon, :time, :elevation + attr_accessor :lat, :lon, :time, :elevation, :gpx_file # When you need to manipulate individual points, you can create a Point # object with a latitude, a longitude, an elevation, and a time. In diff --git a/lib/gpx/route.rb b/lib/gpx/route.rb index 54d0541..d8524f0 100644 --- a/lib/gpx/route.rb +++ b/lib/gpx/route.rb @@ -33,10 +33,10 @@ module GPX def initialize(opts = {}) rte_element = opts[:element] @gpx_file = opts[:gpx_file] - @name = rte_element.find("child::gpx:name", @ns).first.content + @name = rte_element.find("child::gpx:name", @gpx_file.ns).first.content @points = [] - rte_element.find("child::gpx:rtept", @ns).each do |point| - @points << Point.new(:element => point) + rte_element.find("child::gpx:rtept", @gpx_file.ns).each do |point| + @points << Point.new(:element => point, :gpx_file => @gpx_file) end end diff --git a/lib/gpx/waypoint.rb b/lib/gpx/waypoint.rb index 56010bb..f9a7a52 100644 --- a/lib/gpx/waypoint.rb +++ b/lib/gpx/waypoint.rb @@ -30,6 +30,7 @@ module GPX SUB_ELEMENTS = %w{ magvar geoidheight name cmt desc src link sym type fix sat hdop vdop pdop ageofdgpsdata dgpsid extensions } attr_reader :gpx_file + SUB_ELEMENTS.each { |sub_el| attr_accessor sub_el.to_sym } # Not implemented def crop(area) @@ -44,7 +45,23 @@ module GPX wpt_elem = opts[:element] @gpx_file = opts[:gpx_file] super(:element => wpt_elem, :gpx_file => @gpx_file) - instantiate_with_text_elements(wpt_elem, SUB_ELEMENTS) + instantiate_with_text_elements(wpt_elem, SUB_ELEMENTS, @gpx_file.ns) + end + + # Prints out a friendly summary of this track (sans points). Useful for + # debugging and sanity checks. + def to_s + result = "Waypoint \n" + result << "\tName: #{name}\n" + result << "\tLatitude: #{lat} \n" + result << "\tLongitude: #{lon} \n" + result << "\tElevation: #{elevation}\n " + result << "\tTime: #{time}\n" + SUB_ELEMENTS.each do |sub_element_attribute| + val = self.send(sub_element_attribute) + result << "\t#{sub_element_attribute}: #{val}\n" unless val.nil? + end + result end # Converts a waypoint to a XML::Node.