Skip to content

Instantly share code, notes, and snippets.

@gousiosg
Created October 8, 2015 21:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gousiosg/707fc9ae413e3dbbd875 to your computer and use it in GitHub Desktop.
Save gousiosg/707fc9ae413e3dbbd875 to your computer and use it in GitHub Desktop.
Geocode a list of addresses using OpenStreetMaps
#!/usr/bin/env ruby
#
require 'open-uri'
require 'json'
require 'pp'
require 'uri'
File.open('locs').each_line do |location|
url=URI.escape("http://nominatim.openstreetmap.org/search/#{location}?format=json&addressdetails=1")
begin
ts = Time.now
geocoded = JSON.parse(open(url).read).sort{|x,y| x['importance'] <=> y['importance']}.first
data = {
:long => geocoded['lon'],
:lat => geocoded['lat'],
:city => geocoded['address']['city'],
:country => geocoded['address']['country'],
:state => geocoded['address']['state']
}
pp data
rescue
STDERR.puts "Cannot geocode: #{location}"
ensure
taken = Time.now.to_f - ts.to_f
sleep(1 - taken) if 1 - taken > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment