Class: Natour::Report

Inherits:
Object
  • Object
show all
Includes:
Asciinurse
Defined in:
lib/natour/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Asciinurse

#save_adoc, #to_adoc

Constructor Details

#initialize(path, title, images, species_lists, gps_track: nil, map_image: nil, starting_point: nil, arrival_point: nil) ⇒ Report

Returns a new instance of Report.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/natour/report.rb', line 14

def initialize(path,
               title,
               images,
               species_lists,
               gps_track: nil,
               map_image: nil,
               starting_point: nil,
               arrival_point: nil)
  @path = path
  @title = title
  @images = images
  @species_lists = species_lists
  @gps_track = gps_track
  @map_image = map_image
  @starting_point = starting_point
  @arrival_point = arrival_point
end

Instance Attribute Details

#arrival_pointObject (readonly)

Returns the value of attribute arrival_point.



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

def arrival_point
  @arrival_point
end

#gps_trackObject (readonly)

Returns the value of attribute gps_track.



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

def gps_track
  @gps_track
end

#imagesObject (readonly)

Returns the value of attribute images.



7
8
9
# File 'lib/natour/report.rb', line 7

def images
  @images
end

#map_imageObject (readonly)

Returns the value of attribute map_image.



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

def map_image
  @map_image
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/natour/report.rb', line 5

def path
  @path
end

#species_listsObject (readonly)

Returns the value of attribute species_lists.



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

def species_lists
  @species_lists
end

#starting_pointObject (readonly)

Returns the value of attribute starting_point.



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

def starting_point
  @starting_point
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/natour/report.rb', line 6

def title
  @title
end

Class Method Details

.load_directory(dir, track_formats: %i[gpx fit],, create_map: true, overwrite_map: false, map_layers: []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/natour/report.rb', line 32

def self.load_directory(dir, track_formats: %i[gpx fit], create_map: true, overwrite_map: false, map_layers: [])
  Dir.chdir(dir) do
    path = Pathname(dir)
    title = Pathname.pwd.basename.to_s.encode('utf-8')
                    .gsub(/^\d{4}-\d{2}-\d{2}( |_|-)?/, '')
    images = Pathname.glob('**/*.{[j|J][p|P][g|G],[j|J][p|P][e|E][g|G]}')
                     .map { |filename| Image.load_file(filename.to_s) }
                     .sort_by { |image| [image.date_time ? 0 : 1, image.date_time, image.path] }
    species_lists =
      Pathname.glob('**/*.{[c|C][s|S][v|V],[k|K][m|M][l|L]}')
              .map { |filename| SpeciesList.load_file(filename.to_s) }
              .flatten
              .sort_by { |species_list| [species_list.type, species_list.date ? 0 : 1, species_list.date] }
    gps_tracks = if track_formats.empty?
                   []
                 else
                   track_patterns = { gpx: '[g|G][p|P][x|X]', fit: '[f|F][i|I][t|T]' }
                   track_pattern = track_formats.map { |track_format| track_patterns[track_format] }
                                                .compact
                                                .join(',')
                   Pathname.glob("**/*.{#{track_pattern}}")
                           .map { |filename| GPSTrack.load_file(filename.to_s) }
                           .compact
                           .sort_by { |gps_track| [gps_track.date, gps_track.path] }
                 end

    if create_map
      map_images = MapGeoAdmin.open do |map|
        Dir.mktmpdir do |tmp_dir|
          gps_tracks.map do |gps_track|
            track = Pathname(tmp_dir).join(gps_track.path).sub_ext('.gpx')
            gps_track.save_gpx(track, overwrite: true)
            filename = Pathname(gps_track.path).sub_ext('.jpg')
            map.save_image(filename, overwrite: overwrite_map, gps_files: [track], map_layers: map_layers)
            Image.load_file(filename.to_s)
          end
        end
      end
    end

    if gps_tracks.empty?
      [Report.new(path.to_s, title, images, species_lists)]
    else
      gps_tracks.zip(map_images.to_a).map do |gps_track, map_image|
        starting_station = PublicTransport.search_station(
          [gps_track.start_point.latitude, gps_track.start_point.longitude]
        )
        arrival_station = PublicTransport.search_station(
          [gps_track.end_point.latitude, gps_track.end_point.longitude]
        )
        Report.new(
          path.to_s,
          title,
          images.select { |image| !image.date_time || image.date_time.to_date == gps_track.date },
          species_lists.select { |species_list| !species_list.date || species_list.date == gps_track.date },
          gps_track: gps_track.round_effective_km!,
          map_image: map_image,
          starting_point: starting_station&.label,
          arrival_point: arrival_station&.label
        )
      end
    end
  end
end