forked from codebutler/farebot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocode-stations.rb
More file actions
56 lines (42 loc) · 1.37 KB
/
geocode-stations.rb
File metadata and controls
56 lines (42 loc) · 1.37 KB
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
# encoding: utf-8
require './common.rb'
require 'graticule'
@geocoder = Graticule.service(:google).new ""
def geocode_station(station_name)
@geocoder.locate [ station_name, '駅' ].join(' ')
end
db = SQLite3::Database.new( "../assets/StationCode.db" )
db.results_as_hash = true
TABLES.each do |table_name|
puts table_name
i = 0
count = db.get_first_value("SELECT COUNT(*) FROM #{table_name} WHERE Latitude IS NULL AND Longitude IS NULL")
db.execute("SELECT #{COLUMNS.join(',')} FROM #{table_name} WHERE Latitude IS NULL AND Longitude IS NULL") do |row|
station_name = row["StationName"]
status = "[#{i}/#{count}]"
puts "#{status} #{station_name}"
# Test
#break if station_name.include?('端末試験用')
begin
loc = geocode_station station_name
puts "#{status} #{loc.latitude} #{loc.longitude}"
update_sql = "UPDATE #{table_name}
SET
Latitude = :latitude,
Longitude = :longitude
WHERE _id = :id"
db.prepare update_sql do |stmt|
r = stmt.execute id: row['_id'], latitude: loc.latitude, longitude: loc.longitude
raise "Wrong number of rows changed: #{db.changes}" if db.changes != 1
end
rescue Exception => ex
if ex.message == "unknown address"
puts "#{status} #{ex}"
else
raise ex
end
end
sleep 0.1
i += 1
end
end