Class: Geotagger::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/geotagger/exif_editor.rb

Overview

Wrapper class for path to image and the EXIF data read (and written) to the image.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor, path, time_offset = 0) ⇒ Image

Returns a new instance of Image.



14
15
16
17
18
19
20
21
# File 'lib/geotagger/exif_editor.rb', line 14

def initialize(editor, path, time_offset = 0)
  @editor = editor
  @photo = MiniExiftool.new path
  @attr = {
   :path => path,
   :time => (time = @photo['DateTimeOriginal']) && (time + time_offset + editor.global_time_offset)
  }
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



12
13
14
# File 'lib/geotagger/exif_editor.rb', line 12

def attr
  @attr
end

#editorObject (readonly)

Returns the value of attribute editor.



12
13
14
# File 'lib/geotagger/exif_editor.rb', line 12

def editor
  @editor
end

#photoObject (readonly)

Returns the value of attribute photo.



12
13
14
# File 'lib/geotagger/exif_editor.rb', line 12

def photo
  @photo
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/geotagger/exif_editor.rb', line 31

def [](key)
  @attr[key]
end

#[]=(key, value) ⇒ Object



35
36
37
# File 'lib/geotagger/exif_editor.rb', line 35

def []=(key,value)
  @attr[key] = value
end

#get_photo_time(offset = 0) ⇒ Object



39
40
41
# File 'lib/geotagger/exif_editor.rb', line 39

def get_photo_time(offset=0)
  (time = photo['DateTimeOriginal']) && (time + offset)
end

#pathObject



23
24
25
# File 'lib/geotagger/exif_editor.rb', line 23

def path
  self[:path]
end

#save!Object



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
# File 'lib/geotagger/exif_editor.rb', line 43

def save!
  # http://en.wikipedia.org/wiki/Geotagging#JPEG_photos

  photo['ProcessingSoftware'] = 'gpx2exif'

  photo['GPSVersionID'] = '2 2 0 0'
  photo['DateTimeOriginal'] = self.time

  photo['GPSLatitude'] = @attr[:coord][:lat]
  photo['GPSLongitude'] = @attr[:coord][:lon]

  lat_ref = (@attr[:coord][:lat] < 0.0) ? "S" : "N"
  lon_ref = (@attr[:coord][:lon] < 0.0) ? "W" : "E"

  photo['GPSLatitudeRef'] = lat_ref
  photo['GPSLongitudeRef'] = lon_ref

  photo['GPSAltitude'] = @attr[:coord][:alt]
  photo['Orientation'] = 1	# 1 means normal upright landscape mode
  photo['GPSImgDirectionRef'] = 'T'	# T=true north (as opposed to M=magnetic)
  photo['GPSImgDirection'] = @attr[:coord][:direction]	# calculated in TrackImporter
  photo['GPSMapDatum'] = 'WGS-84'	# We assume all GPS data is WGS-84

  (editor.options[:exif_tags]||{}).each do |key,value|
    photo[key] = value
  end

  unless photo.save
    puts "Failed to save exif data to '#{path}': #{photo.errors.inspect}"
  end

  photo2 = MiniExiftool.new path
  puts " - coord saved lat #{photo2['GPSLatitude']} lon #{photo2['GPSLongitude']}" if editor.verbose

  # exiftool -GPSMapDatum="WGS-84" -gps:GPSLatitude="34,57,57"
  # -gps:GPSLatitudeRef="N" -gps:GPSLongitude="83,17,59" -gps:GPSLongitudeRef="W"
  # -gps:GPSAltitudeRef="0" -GPSAltitude=1426 -gps:GPSMeasureMode=2 -City="RabunBald"
  # -State="North Carolina" -Country="USA" ~/Desktop/RabunBaldSummit_NC.jpg
end

#timeObject



27
28
29
# File 'lib/geotagger/exif_editor.rb', line 27

def time
  self[:time]
end

#to_sObject



83
84
85
# File 'lib/geotagger/exif_editor.rb', line 83

def to_s
  "Image[#{path}] at '#{time}'"
end