Created
October 8, 2015 21:27
-
-
Save gousiosg/707fc9ae413e3dbbd875 to your computer and use it in GitHub Desktop.
Geocode a list of addresses using OpenStreetMaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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