13

I have a question about Paperclip. Many modern cameras and phones add GPS metadata to a photo when taken:

geotagging

Users of my Rails app can upload photos and manually add the location data to them. However, this sucks as users don't want to enter that: they want it to be there automatically.

Can, and how can I get GPS metadata from a photo (always JPEG) with Paperclip and Ruby on Rails? Can it be done with Paperclip or do I need another gem?

4 Answers4

18

What you need is being able to extract EXIF information from the picture.

There is a gem that aims to do just that: exifr

You can install it automatically with the following bundler line:

gem 'exifr'

EDIT:

I have personally switched to this fork of the same gem, which contains GPS helpers:

gem 'exifr', :git => 'git://github.com/picuous/exifr.git'

Use example:

EXIFR::JPEG.new('IMG_6841.JPG').gps?                # => true
EXIFR::JPEG.new('IMG_6841.JPG').gps                 # => [37.294112,-122.789422]
EXIFR::JPEG.new('IMG_6841.JPG').gps_lat             # => 37.294112
EXIFR::JPEG.new('IMG_6841.JPG').gps_lng             # =>-122.789422
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
1

Just an update to the accepted answer above, which greatly helped me to get EXIFR working but has few issues:

  1. To check if an image has GPS, I had to use EXIFR::JPEG.new('IMG_6841.JPG').exif?

  2. To pull the latitude I used EXIFR::JPEG.new('IMG_6841.JPG').gps_latitude

  3. And longitude is EXIFR::JPEG.new('IMG_6841.JPG').gps_longitude

Small issues but they had me spinning for a bit and might save someone time down the road.

Also, once I got my GPS coordinates from my photo I found this blog entry to help convert them from an array to Google Map friendly coordinates (i.e. 34.360826, -110.239368): http://www.cloudspace.com/blog/2009/02/16/decoding-gps-latitude-and-longitude-from-exif-in-ruby/

jaywards
  • 21
  • 3
1

you can't do it with paperclip. rmagick will help you neither. The only way I see is:

raw_props = %x[identify -format '%[exif:*]' #{path_to_image}].split("\n").collect{|a| a.split("=")}
properties = Hash[*raw_props.collect { |v| [v, v*2]}.flatten]

(pre: imagemagick installed and in the path, check with 'which identify')

Beffa
  • 915
  • 1
  • 8
  • 18
0

If you want use "higher level" solution (no exif directly), there is a gem https://github.com/aufi/photo_geoloader which loads latitude, longitude and altitude and can place it into rails model (in callback)

class Photo < ActiveRecord::Base
  before_create do
    PhotoGeoloader.new(photo.path).place_attributes self
  end
  ...