2015年6月17日 星期三

google maps Google Geocoding API error: over query limit.

6/18 
set timout :5 sec in config/initializers/geocoder.rb



next stwp set sleep
heroku  rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100




next next step 

I create pull request based on this answer and now you can use batch in geocoder:
rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100
I would use this solution for large database, i take it from geocoder gem rake task: You can refine this for your needs. Some example create rake task:
namespace :geocode_my_data do
  desc "Geocode all objects in my databse."
  task all: :environment do
  klass = User
    klass.where(geocoded: false).find_each(limit: 100) do |obj|
      obj.geocode; obj.save
    end
  end
end

$> rake geocode_my_data:all




Used below code and put it into my lib/tasks folder as geocode_my_data.rake To run:
rake geocode:all CLASS=YourModel
Works great!
namespace :geocode_my_data do
  desc "Geocode all objects without coordinates."
  task :all => :environment do
    class_name = ENV['CLASS'] || ENV['class']
    sleep_timer = ENV['SLEEP'] || ENV['sleep']
    raise "Please specify a CLASS (model)" unless class_name
    klass = class_from_string(class_name)

klass.not_geocoded.find_each(batch_size: 100) do |obj|
      obj.geocode; obj.save
      sleep(sleep_timer.to_f) unless sleep_timer.nil?
    end
  end
end

##
# Get a class object from the string given in the shell environment.
# Similar to ActiveSupport's +constantize+ method.
#
def class_from_string(class_name)
  parts = class_name.split("::")
  constant = Object
  parts.each do |part|
    constant = constant.const_get(part)
  end
  constant
end

沒有留言:

張貼留言