Geo coding example from Ruby API

pthpth Registered Users Posts: 49 Big grins
I just threw this example together to show off some of the Ruby API feature. Note you need the latest version of the gem (0.9.7) for this example (included in the examples directory of the gem).
require "rubygems"
require "smugmugapi"

SmugMugAPI.api_version = '1.2.1'
smugmug.login("phurley@gmail.com", IO.read('password').chomp) do
  albums = smugmug.albums.get
  
  albums.each_album do |album|
    puts "Album Title: #{album.title}"
    locations = []
    lost = []
    
    smugmug.images.get(:album_id => album.id).each_image do |image|
      info = smugmug.images.get_info(:image_id => image.id)
      if info.longitude && info.latitude
        locations << [info.longitude, info.latitude]
      else
        lost << image.id
      end
    end

    unless locations.empty?
      # average the locations, would be better to interpolate, based on time/location
      longitude = locations.inject(0.0) { |t,v| t + v.first.to_f } / locations.size
      latitude  = locations.inject(0.0) { |t,v| t + v.last.to_f } / locations.size
      
      lost.each do |id|
        smugmug.images.change_settings(:image_id => id, :longitude => longitude, :latitude => latitude)
      end
    end
  end  
end

Note the handy each_XXX method now available when parsing results from API (and feed) calls. I am going to clean up this code soon (when I have a free moment or two) and fix it to interpolate lon/lat settings based upon time -- like way points. If there is interest I could make a web based front end for people who do not want to install Ruby (of course if you are running Leopard you already have it :-).

pth
Sign In or Register to comment.