class GPX::Gpx
Docu
Attributes
used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx
used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx
used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx
used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx
access in readonly to the quantity of points/routes/tracks in the gpx
used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx
access in readonly to the quantity of points/routes/tracks in the gpx
used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx
access in readonly to the quantity of points/routes/tracks in the gpx
Public Class Methods
# File lib/gpx_kml/gpx.rb, line 12 def initialize(file_path) return unless correct_path?(file_path) && (File.size(file_path) < 10_000_000) @gpx = Nokogiri::XML(File.open(file_path)) return unless valid? @file_name = File.basename(file_path) @name = _name @author = @gpx.xpath('/xmlns:gpx/xmlns:metadata/xmlns:author/xmlns:name/text()').to_s @link = @gpx.xpath('/xmlns:gpx/xmlns:metadata/xmlns:link/@href').to_s @tracks = _tracks if tracks? @routes = _routes if routes? @points = _points if points? @points_length = _points_length @routes_length = _routes_length @tracks_length = _tracks_length end
Public Instance Methods
# File lib/gpx_kml/gpx.rb, line 63 def _name if valid? name = @gpx.xpath('/xmlns:gpx/xmlns:metadata/xmlns:name/text()').to_s return alt_name if name.empty? return name end puts 'return empty string' '' end
# File lib/gpx_kml/gpx.rb, line 74 def description return @gpx.xpath('//xmlns:metadata/xmlns:desc/text()').to_s if valid? '' end
# File lib/gpx_kml/gpx.rb, line 36 def gpx? !@gpx.nil? && !@gpx.xpath('/xmlns:gpx').empty? end
# File lib/gpx_kml/gpx.rb, line 57 def points? return true unless @gpx.xpath('//xmlns:wpt').empty? false end
# File lib/gpx_kml/gpx.rb, line 45 def routes? return true unless @gpx.xpath('//xmlns:rte').empty? false end
# File lib/gpx_kml/gpx.rb, line 51 def tracks? return true unless @gpx.xpath('//xmlns:trk').empty? false end
For a gpx file to be valid it must only have a waypoint, a route or a track
# File lib/gpx_kml/gpx.rb, line 41 def valid? gpx? && (tracks? || routes? || points?) end
Private Instance Methods
# File lib/gpx_kml/gpx.rb, line 98 def _points p = [] @gpx.xpath('xmlns:gpx/xmlns:wpt').each_with_index do |wpt, i| p[i] = GPX::Point.new wpt, self end p end
# File lib/gpx_kml/gpx.rb, line 118 def _points_length return 0 if @points.nil? @points.length end
# File lib/gpx_kml/gpx.rb, line 90 def _routes r = [] @gpx.xpath('xmlns:gpx/xmlns:rte').each_with_index do |rte, i| r[i] = GPX::Route.new rte end r end
# File lib/gpx_kml/gpx.rb, line 112 def _routes_length return 0 if @routes.nil? @routes.length end
# File lib/gpx_kml/gpx.rb, line 82 def _tracks t = [] @gpx.xpath('xmlns:gpx/xmlns:trk').each_with_index do |trk, i| t[i] = GPX::Track.new trk end t end
# File lib/gpx_kml/gpx.rb, line 106 def _tracks_length return 0 if @tracks.nil? @tracks.length end
# File lib/gpx_kml/gpx.rb, line 128 def alt_name node = nil node = @gpx.xpath('//xmlns:wpt')[0] if points? node = @gpx.xpath('//xmlns:trk')[0] if tracks? node = @gpx.xpath('//xmlns:rte')[0] if routes? return node.xpath('./xmlns:name/text()').to_s unless node.nil? '' end
# File lib/gpx_kml/gpx.rb, line 124 def correct_path?(path) path.instance_of?(String) && (path.end_with?('.gpx') || path.end_with?('.xml') || !path.include?('.')) end