Paperclip Location

Pulling geo-location from photos with Rails has never been easier.

Paperclip is a file attachment gem from the good folks at Thoughtbot. Paperclip-Location is a Paperclip::Processor that extracts an image's latitude and longitude from EXIF metadata.

The Setup

The processor requires works with ActiveRecord models, and requires 3 fields:

  • lat - a decimal representing the latitude
  • lng - a decimal representing the longitude
  • location_locked - a boolean flag to determine if the location has been manually set. During image processing, if the location_locked field is true, the record's current location will remain and EXIF processing will be skipped.

First, add them to the table with a migration.

Then, include :location in the array of Paperclip::Processors within the has_attached_file macro.

Finally, paperclip-location expects that processed files are images with EXIF metadata. To require this, add a Paperclip validation on :content_type.

The Payoff

Now, whenever an image with GPS information in its EXIF headers is attached to a Place, its lat and lng fields will automatically be updated.

Further Reading

  • View the source code on GitHub
  • A working code example can be found here.
  • For a real example from the wild, checkout the source for Chief, an open-source skate spot catalog.