class GPX::Gpx

Docu

Attributes

author[R]

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx

file_name[R]

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx

name[R]

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx

points[R]

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx

points_length[R]

access in readonly to the quantity of points/routes/tracks in the gpx

routes[R]

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx

routes_length[R]

access in readonly to the quantity of points/routes/tracks in the gpx

tracks[R]

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx

tracks_length[R]

access in readonly to the quantity of points/routes/tracks in the gpx

Public Class Methods

new(file_path) click to toggle source
# 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

_name() click to toggle source
# 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
description() click to toggle source
# File lib/gpx_kml/gpx.rb, line 74
def description
  return @gpx.xpath('//xmlns:metadata/xmlns:desc/text()').to_s if valid?

  ''
end
gpx?() click to toggle source
# File lib/gpx_kml/gpx.rb, line 36
def gpx?
  !@gpx.nil? && !@gpx.xpath('/xmlns:gpx').empty?
end
points?() click to toggle source
# File lib/gpx_kml/gpx.rb, line 57
def points?
  return true unless @gpx.xpath('//xmlns:wpt').empty?

  false
end
routes?() click to toggle source
# File lib/gpx_kml/gpx.rb, line 45
def routes?
  return true unless @gpx.xpath('//xmlns:rte').empty?

  false
end
tracks?() click to toggle source
# File lib/gpx_kml/gpx.rb, line 51
def tracks?
  return true unless @gpx.xpath('//xmlns:trk').empty?

  false
end
valid?() click to toggle source

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

_points() click to toggle source
# 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
_points_length() click to toggle source
# File lib/gpx_kml/gpx.rb, line 118
def _points_length
  return 0 if @points.nil?

  @points.length
end
_routes() click to toggle source
# 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
_routes_length() click to toggle source
# File lib/gpx_kml/gpx.rb, line 112
def _routes_length
  return 0 if @routes.nil?

  @routes.length
end
_tracks() click to toggle source
# 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
_tracks_length() click to toggle source
# File lib/gpx_kml/gpx.rb, line 106
def _tracks_length
  return 0 if @tracks.nil?

  @tracks.length
end
alt_name() click to toggle source
# 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
correct_path?(path) click to toggle source
# 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