From 931a10cd17ecff20ac137c3aee8f8905bc52cf8e Mon Sep 17 00:00:00 2001 From: Doug Fales Date: Tue, 28 Nov 2006 15:35:25 +0000 Subject: [PATCH] A couple of fixes to make the library comply with the different attribute names possible on the bounds element. Formerly, a bounds element with a minlat or minLat attribute (instead of min_lat) would not be properly parsed. Now all three styles of attribute name are accepted. --- lib/gpx/bounds.rb | 8 ++++---- lib/gpx/gpx_file.rb | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/gpx/bounds.rb b/lib/gpx/bounds.rb index 8e6eee7..33bf8a4 100644 --- a/lib/gpx/bounds.rb +++ b/lib/gpx/bounds.rb @@ -45,10 +45,10 @@ module GPX def to_xml bnd = REXML::Element.new('bounds') - bnd.attributes['min_lat'] = min_lat - bnd.attributes['min_lon'] = min_lon - bnd.attributes['max_lat'] = max_lat - bnd.attributes['max_lon'] = max_lon + bnd.attributes['minlat'] = min_lat + bnd.attributes['minlon'] = min_lon + bnd.attributes['maxlat'] = max_lat + bnd.attributes['maxlon'] = max_lon bnd end diff --git a/lib/gpx/gpx_file.rb b/lib/gpx/gpx_file.rb index 3caf234..983174e 100644 --- a/lib/gpx/gpx_file.rb +++ b/lib/gpx/gpx_file.rb @@ -53,10 +53,10 @@ module GPX bounds_element = (XPath.match(@xml, "/gpx/metadata/bounds").first rescue nil) if bounds_element - @bounds.min_lat = bounds_element.attributes["min_lat"].to_f - @bounds.min_lon = bounds_element.attributes["min_lon"].to_f - @bounds.max_lat = bounds_element.attributes["max_lat"].to_f - @bounds.max_lon = bounds_element.attributes["max_lon"].to_f + @bounds.min_lat = get_bounds_attr_value(bounds_element, %w{ min_lat minlat minLat }) + @bounds.min_lon = get_bounds_attr_value(bounds_element, %w{ min_lon minlon minLon}) + @bounds.max_lat = get_bounds_attr_value(bounds_element, %w{ max_lat maxlat maxLat}) + @bounds.max_lon = get_bounds_attr_value(bounds_element, %w{ max_lon maxlon maxLon}) else get_bounds = true end @@ -82,6 +82,15 @@ module GPX end end + def get_bounds_attr_value(el, possible_names) + result = nil + possible_names.each do |name| + result = el.attributes[name] + break unless result.nil? + end + return (result.to_f rescue nil) + end + # Returns the distance, in kilometers, meters, or miles, of all of the # tracks and segments contained in this GPXFile. def distance(opts = { :units => 'kilometers' })