Thanks to Mike Gauland for discovering some route- and waypoint-related bugs. I've fixed them and also added #to_s on Waypoint so it's easier to debug.

master
Doug Fales 2008-02-08 23:26:14 +00:00
parent 59aa3e5b9b
commit 8df3cb1ea3
4 changed files with 27 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.