Adding the ability to write GPX to a string in addition to a file. Thanks to Douglas Robertson for the patch.
parent
9dfdfd8360
commit
287467de59
|
@ -205,6 +205,20 @@ module GPX
|
||||||
# Serialize the current GPXFile to a gpx file named <filename>.
|
# Serialize the current GPXFile to a gpx file named <filename>.
|
||||||
# If the file does not exist, it is created. If it does exist, it is overwritten.
|
# If the file does not exist, it is created. If it does exist, it is overwritten.
|
||||||
def write(filename, update_time = true)
|
def write(filename, update_time = true)
|
||||||
|
@time = Time.now if(@time.nil? or update_time)
|
||||||
|
@name ||= File.basename(filename)
|
||||||
|
doc = generate_xml_doc
|
||||||
|
doc.save(filename, :indent => true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s(update_time = true)
|
||||||
|
@time = Time.now if(@time.nil? or update_time)
|
||||||
|
doc = generate_xml_doc
|
||||||
|
doc.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def generate_xml_doc
|
||||||
doc = Document.new
|
doc = Document.new
|
||||||
doc.root = Node.new('gpx')
|
doc.root = Node.new('gpx')
|
||||||
gpx_elem = doc.root
|
gpx_elem = doc.root
|
||||||
|
@ -218,10 +232,9 @@ module GPX
|
||||||
|
|
||||||
# setup the metadata elements
|
# setup the metadata elements
|
||||||
name_elem = Node.new('name')
|
name_elem = Node.new('name')
|
||||||
name_elem << File.basename(filename)
|
name_elem << @name
|
||||||
time_elem = Node.new('time')
|
time_elem = Node.new('time')
|
||||||
@time = Time.now if(@time.nil? or update_time)
|
time_elem << @time.xmlschema
|
||||||
time_elem << @time.xmlschema
|
|
||||||
|
|
||||||
# version 1.0 of the schema doesn't support the metadata element, so push them straight to the root 'gpx' element
|
# version 1.0 of the schema doesn't support the metadata element, so push them straight to the root 'gpx' element
|
||||||
if (@version == '1.0') then
|
if (@version == '1.0') then
|
||||||
|
@ -240,11 +253,9 @@ module GPX
|
||||||
waypoints.each { |w| gpx_elem << w.to_xml } unless waypoints.nil?
|
waypoints.each { |w| gpx_elem << w.to_xml } unless waypoints.nil?
|
||||||
routes.each { |r| gpx_elem << r.to_xml } unless routes.nil?
|
routes.each { |r| gpx_elem << r.to_xml } unless routes.nil?
|
||||||
|
|
||||||
doc.save(filename, :indent => true)
|
return doc
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# Calculates and sets the duration attribute by subtracting the time on
|
# Calculates and sets the duration attribute by subtracting the time on
|
||||||
# the very first point from the time on the very last point.
|
# the very first point from the time on the very last point.
|
||||||
def calculate_duration
|
def calculate_duration
|
||||||
|
|
|
@ -74,8 +74,8 @@ module GPX
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts a waypoint to a XML::Node.
|
# Converts a waypoint to a XML::Node.
|
||||||
def to_xml
|
def to_xml(elem_name = 'wpt')
|
||||||
wpt = Node.new('wpt')
|
wpt = Node.new(elem_name)
|
||||||
wpt['lat'] = lat.to_s
|
wpt['lat'] = lat.to_s
|
||||||
wpt['lon'] = lon.to_s
|
wpt['lon'] = lon.to_s
|
||||||
SUB_ELEMENTS.each do |sub_element_name|
|
SUB_ELEMENTS.each do |sub_element_name|
|
||||||
|
|
Loading…
Reference in New Issue