-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeocoder.rb
59 lines (45 loc) · 1.07 KB
/
geocoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# coding: utf-8
require "rubygems"
require "bundler/setup"
require "csv"
require "geocoder"
Geocoder.configure(
timeout: 3,
lookup: :google,
use_https: true,
api_key: "AIzaSyAwBPxrD7iEdmnu6MuK2-bkFW3pVfHXpEA",
units: :km
)
def geocode(address)
results = Geocoder.search(clean(address))
results.first if results.any?
end
def clean(address)
if address && !address.empty?
address.split(/\n+/).join(" ")
end
end
output = []
CSV.foreach("locations.csv", col_sep: ";").with_index(1) do |row, i|
location = [row[0]]
log = "#{i}: processing: #{row[0]}… "
if geocoded = geocode(row[1])
log << "found address"
location << geocoded.formatted_address
location << geocoded.latitude
location << geocoded.longitude
else
log << "could not find address"
location << clean(row[1])
location << ""
location << ""
end
puts log
location << row[2]
location << row[3]
location << row[4]
output << location
end
# Write data
File.write("geocoded_locations.csv", output.map(&:to_csv).join)
File.write("locations.json", output.to_json)