Class: GpxUtils::TrackImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx_utils/track_importer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrackImporter

Returns a new instance of TrackImporter.



10
11
12
# File 'lib/gpx_utils/track_importer.rb', line 10

def initialize
  @coords = Array.new
end

Instance Attribute Details

#coordsObject (readonly)

Returns the value of attribute coords.



14
15
16
# File 'lib/gpx_utils/track_importer.rb', line 14

def coords
  @coords
end

Class Method Details

.coord_valid?(lat, lon, elevation, time) ⇒ Boolean

Only import valid coords

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/gpx_utils/track_importer.rb', line 47

def self.coord_valid?(lat, lon, elevation, time)
  return true if lat and lon
  return false
end

.proc_time(ts) ⇒ Object



52
53
54
55
56
# File 'lib/gpx_utils/track_importer.rb', line 52

def self.proc_time(ts)
  if ts =~ /(\d{4})-(\d{2})-(\d{2})T(\d{1,2}):(\d{2}):(\d{2})Z/
    return Time.gm($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i).localtime
  end
end

Instance Method Details

#add_file(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gpx_utils/track_importer.rb', line 16

def add_file(path)
  f = File.new(path)
  doc = Nokogiri::XML(f)
  doc.remove_namespaces!
  a = Array.new
  error_count = 0

  trackpoints = doc.xpath('//gpx/trk/trkseg/trkpt')
  trackpoints.each do |wpt|
    w = {
      :lat => wpt.xpath('@lat').to_s.to_f,
      :lon => wpt.xpath('@lon').to_s.to_f,
      :time => proc_time(wpt.xpath('time').children.first.to_s),
      :alt => wpt.xpath('ele').children.first.to_s.to_f
    }

    if self.class.coord_valid?(w[:lat], w[:lon], w[:alt], w[:time])
      a << w
    else
      error_count += 1
    end

  end

  f.close

  @coords += a
  @coords = @coords.sort { |b, c| b[:time] <=> c[:time] }
end

#proc_time(ts) ⇒ Object



58
59
60
# File 'lib/gpx_utils/track_importer.rb', line 58

def proc_time(ts)
  self.class.proc_time(ts)
end