1

I have about 100,000 records, that I need to find latitude and longitude. Currently I am using geokit-rails for geo-coding.

What I am doing now is looping the records one by one to find latitude and longitude. But it's very time consuming. Is there a way to do this in bulk? Any batch processing methods or something like that.

Please help!

talonmies
  • 70,661
  • 34
  • 192
  • 269
Amal Kumar S
  • 15,555
  • 19
  • 56
  • 88
  • I have found a sample link in perl which does this http://code.activestate.com/ppm/Geo-Coder-Bing-Bulk/ I think so, am not sure. Do we have something like this in ruby – Amal Kumar S Jan 06 '12 at 11:42

2 Answers2

0

The only improvement I can think of is using find_each

Model.find_each :batch_size => 500 do |model|
   #geocode
end

It will load 500 objects at a time and loop through them. I don't think you can do a lot better than that, even if there was a native method to do it, the performances would be the same, I think.

Robin
  • 21,667
  • 10
  • 62
  • 85
0

I don't think there is a faster way to do it. I agree with @Robin, maybe you can do this little improvement, but you can't improve a lot the performance.

I suggest, to improve the system, to insert, as I have done time ago on Rails 2, few lines of code to automatically detect the longitude and the latitude of a record, given the address and the city of the record, once you have to create a new record. In this way, you won't do a time-consuming loop like you wrote!

@shop = Shop.new(params[:shop])
@coordinates = []
seek_str = @shop.city + "," + @shop.address
@coordinates = Geocoding.get(seek_str)
@shop.lat = @coordinates[0][:latitude]
@shop.lng = @coordinates[0][:longitude]

Latitude and longitude are now geo-coded through city and address. City and address of the record are inserted with a form. Consider this is Rails 2, and maybe there are some edits you should do to get this working on Rails 3!

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • I have found a sample link in perl which does this http://code.activestate.com/ppm/Geo-Coder-Bing-Bulk/ I think so, am not sure. Do we have something like this in ruby – Amal Kumar S Jan 06 '12 at 11:41
  • Interesting. Yes, the geokit gem you're using is already doing the same work and it's similar to that plugin in perl! --> https://github.com/andre/geokit-gem – Alberto Solano Jan 06 '12 at 14:47