-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.rb
executable file
·99 lines (79 loc) · 2.14 KB
/
lookup.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# encoding: UTF-8
require 'net/http'
require 'json'
class Lookup
SERVER = "http://localhost:5000"
#SERVER = "http://routes.ibikecph.dk"
STD_HASTIGHED = 16.4 # fra cykelsekretariatet
def route from,to,out_file, n, direction
a = "#{from[4]},#{from[3]}"
b = "#{to[4]},#{to[3]}"
uri = URI.parse "#{SERVER}/route/v1/fast/#{a};#{b}"
response = Net::HTTP.get uri
json = JSON.parse response
code = json["code"]
if code != 'Ok'
puts
puts "ERROR: #{code}"
p from
p to
p str
p uri
p json
exit
end
route = json["routes"][0]
distance = route["distance"]
duration = route["duration"]
str = [
n,
direction,
from,
to,
code,
distance,
duration,
(distance.to_f/duration.to_f)*3.6,
(distance.to_f/(STD_HASTIGHED/3.6)).to_i
].flatten.join(',')+"\n"
#print '.'
@distance_sum = @distance_sum + distance
return str
end
def run
homes = []
works = []
@distance_sum = 0
path_home = 'home.loc.csv'
path_work = 'work.loc.csv'
path_out = "result.csv"
if File.exists? path_out
puts "#{path_out} already exits."
exit
end
out_file = File.open(path_out, 'w')
File.open("./#{path_home}").each_line do |line|
address = line.chop
homes << address.split(',')
end
File.open("./#{path_work}").each_line do |line|
address = line.chop
works << address.split(',')
end
out_file.write "id, retning, from address,from nr, from zip, from lon, from lat, to address,to nr, to zip, to lon, to lat, kode, afstand, tid, hastighed, tid (standardhastighed)\n"
n = 0
print "Calculating #{2 * homes.length * works.length} routes..."
homes.each do |home|
works.each do |work|
out_file.write route(home,work,out_file, n, 'hjem > arbejde')
n = n + 1
out_file.write route(work,home,out_file, n, 'arbejde > hjem')
n = n + 1
end
end
puts
puts "Distance sum: #{@distance_sum}"
puts 'Done'
end
end
Lookup.new.run