Created
June 3, 2011 20:59
-
-
Save jjhageman/1007160 to your computer and use it in GitHub Desktop.
Extracting Geolocation Image Data with Carrierwave and RMagick on Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemImage < ActiveRecord::Base | |
belongs_to :item | |
mount_uploader :image, ImageUploader | |
before_save :extract_geolocation | |
def extract_geolocation | |
img = Magick::Image.read(image)[0] rescue nil | |
return unless img | |
img_lat = img.get_exif_by_entry('GPSLatitude')[0][1].split(', ') rescue nil | |
img_lng = img.get_exif_by_entry('GPSLongitude')[0][1].split(', ') rescue nil | |
lat_ref = img.get_exif_by_entry('GPSLatitudeRef')[0][1] rescue nil | |
lng_ref = img.get_exif_by_entry('GPSLongitudeRef')[0][1] rescue nil | |
return unless img_lat && img_lng && lat_ref && lng_ref | |
latitude = to_frac(img_lat[0]) + (to_frac(img_lat[1])/60) + (to_frac(img_lat[2])/3600) | |
longitude = to_frac(img_lng[0]) + (to_frac(img_lng[1])/60) + (to_frac(img_lng[2])/3600) | |
latitude = latitude * -1 if lat_ref == 'S' # (N is +, S is -) | |
longitude = longitude * -1 if lng_ref == 'W' # (W is -, E is +) | |
self.lat = latitude | |
self.lng = longitude | |
if geo = Geocoder.search("#{latitude},#{longitude}").first | |
self.city = geo.city | |
self.state = geo.state | |
self.zipcode = geo.postal_code | |
end | |
end | |
def to_frac(strng) | |
numerator, denominator = strng.split('/').map(&:to_f) | |
denominator ||= 1 | |
numerator/denominator | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment