Class: Natour::GPSTrack

Inherits:
Object
  • Object
show all
Defined in:
lib/natour/gps_track.rb

Direct Known Subclasses

FITFile, GPXFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, date, ascent, descent, distance, duration, start_point, end_point) ⇒ GPSTrack

Returns a new instance of GPSTrack.



17
18
19
20
21
22
23
24
25
26
# File 'lib/natour/gps_track.rb', line 17

def initialize(path, date, ascent, descent, distance, duration, start_point, end_point)
  @path = path
  @date = date
  @ascent = ascent
  @descent = descent
  @distance = distance
  @duration = duration
  @start_point = start_point
  @end_point = end_point
end

Instance Attribute Details

#ascentObject (readonly)

Returns the value of attribute ascent.



10
11
12
# File 'lib/natour/gps_track.rb', line 10

def ascent
  @ascent
end

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/natour/gps_track.rb', line 9

def date
  @date
end

#descentObject (readonly)

Returns the value of attribute descent.



11
12
13
# File 'lib/natour/gps_track.rb', line 11

def descent
  @descent
end

#distanceObject (readonly)

Returns the value of attribute distance.



12
13
14
# File 'lib/natour/gps_track.rb', line 12

def distance
  @distance
end

#durationObject (readonly)

Returns the value of attribute duration.



13
14
15
# File 'lib/natour/gps_track.rb', line 13

def duration
  @duration
end

#end_pointObject (readonly)

Returns the value of attribute end_point.



15
16
17
# File 'lib/natour/gps_track.rb', line 15

def end_point
  @end_point
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/natour/gps_track.rb', line 8

def path
  @path
end

#start_pointObject (readonly)

Returns the value of attribute start_point.



14
15
16
# File 'lib/natour/gps_track.rb', line 14

def start_point
  @start_point
end

Class Method Details

.load_file(filename, format: :auto) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/natour/gps_track.rb', line 28

def self.load_file(filename, format: :auto)
  format = Pathname(filename).extname.to_s.delete_prefix('.').to_sym if format == :auto
  case format
  when :gpx
    gpx_file = GPXFile.new(filename)
    gpx_file if gpx_file.types.include?(:track)
  when :fit
    FITFile.new(filename)
  end
end

Instance Method Details

#round_effective_km!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/natour/gps_track.rb', line 39

def round_effective_km!
  @ascent = @ascent&.round(-2)
  @descent = @descent&.round(-2)
  @distance = @distance&.round(-3)
  @duration = Duration.new(round_duration(@duration, minutes: 15)) if @duration
  @start_point = GPSTrackPoint.new(
    @start_point.latitude,
    @start_point.longitude,
    @start_point.elevation,
    round_time(@start_point.time, minutes: 5)
  )
  @end_point = GPSTrackPoint.new(
    @end_point.latitude,
    @end_point.longitude,
    @end_point.elevation,
    round_time(@end_point.time, minutes: 5)
  )
  self
end

#save_gpx(filename, overwrite: false) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/natour/gps_track.rb', line 59

def save_gpx(filename, overwrite: false)
  FileUtils.mkdir_p(Pathname(filename).dirname)
  mode = File::WRONLY | File::CREAT | File::TRUNC
  mode |= File::EXCL unless overwrite
  File.open(filename, mode) do |file|
    file.write(to_gpx)
  end
end