Saturday, October 26, 2013

Scraping Geotag GPS Metadata from Photos - EXIFR Gem

One of the main goals of my first project for WDI_Array was to scrape GPS metadata from uploaded photos.  In order to do this I used the Paperclip Gem for photo uploads and I discovered the EXIFR Gem for acquisition of metadata from photos: https://github.com/remvee/exifr.  EXIFR is an amazing Gem put just as many Gems it is poorly documented.  Questions/answers about it in StackOverflow are also minimal so it took us a little while to figure it out.

  def load_exif
   exif_img = EXIFR::JPEG.new(image.queued_for_write[:custom].path)
    if !exif_img.gps.nil?
     self.latitude = exif_img.gps.latitude
     self.longitude = exif_img.gps.longitude
    end
  end


For my project I have a Post model correlating to the table "posts" in the database.  In my Post model I defined a method that uses the EXIFR Gem to access the image metadata.
In this case the path to the image (in this case "image" is the name of the database column we defined using Paperclip) is passed to the EXIFR Gem, stored in the the exif_img variable and then the GPS data is passed to the database using Active Record methods.  The method load_exif is called after the image is processed:

after_image_post_process :load_exif

Using binding.pry to inspect the exif_img variable containing nested arrays and hashes is quite impressive:



View the project on Github: https://github.com/dtothefp/dylanthedog

No comments:

Post a Comment