如何在 Rails 3 中进行批量地理编码

发布于 2024-12-24 01:34:49 字数 142 浏览 6 评论 0原文

我有大约 100,000 条记录,我需要查找纬度和经度。目前我正在使用 geokit-rails 进行地理编码。

我现在所做的就是逐一循环记录来查找纬度和经度。但这非常耗时。有没有办法批量执行此操作?任何批处理方法或类似的方法。

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久而酒知 2024-12-31 01:34:49

我能想到的唯一改进是使用 find_each

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

它将一次加载 500 个对象并循环访问它们。我认为你不能做得比这更好,即使有一种本机方法可以做到这一点,我认为性能也会是相同的。

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.

不必了 2024-12-31 01:34:49

我不认为有更快的方法。我同意@Robin,也许你可以做一点改进,但​​你不能提高很多性能。

我建议,为了改进系统,插入几行代码来自动检测记录的经度和纬度,就像我之前在 Rails 2 上所做的那样,给定记录的地址和城市,一旦你有创建一个新记录。这样就不会像你写的那样做一个耗时的循环了!

@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]

纬度和经度现在通过城市和地址进行地理编码。记录的城市和地址通过表格插入。考虑这是 Rails 2,也许您应该做一些编辑才能使其在 Rails 3 上运行!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文