|
3 | 3 | actual = [] |
4 | 4 | OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do |
5 | 5 | table.hashes.each_with_index do |row,ri| |
6 | | - waypoints = [] |
7 | | - if row['from'] and row['to'] |
8 | | - node = find_node_by_name(row['from']) |
9 | | - raise "*** unknown from-node '#{row['from']}" unless node |
10 | | - waypoints << node |
11 | | - |
12 | | - node = find_node_by_name(row['to']) |
13 | | - raise "*** unknown to-node '#{row['to']}" unless node |
14 | | - waypoints << node |
| 6 | + if row['request'] |
| 7 | + got = {'request' => row['request'] } |
| 8 | + response = request_url row['request'] |
| 9 | + else |
| 10 | + params = {} |
| 11 | + waypoints = [] |
| 12 | + if row['from'] and row['to'] |
| 13 | + node = find_node_by_name(row['from']) |
| 14 | + raise "*** unknown from-node '#{row['from']}" unless node |
| 15 | + waypoints << node |
15 | 16 |
|
16 | | - got = {'from' => row['from'], 'to' => row['to'] } |
17 | | - elsif row['waypoints'] |
18 | | - row['waypoints'].split(',').each do |n| |
19 | | - node = find_node_by_name(n.strip) |
20 | | - raise "*** unknown waypoint node '#{n.strip}" unless node |
| 17 | + node = find_node_by_name(row['to']) |
| 18 | + raise "*** unknown to-node '#{row['to']}" unless node |
21 | 19 | waypoints << node |
| 20 | + |
| 21 | + got = {'from' => row['from'], 'to' => row['to'] } |
| 22 | + response = request_route waypoints, params |
| 23 | + elsif row['waypoints'] |
| 24 | + row['waypoints'].split(',').each do |n| |
| 25 | + node = find_node_by_name(n.strip) |
| 26 | + raise "*** unknown waypoint node '#{n.strip}" unless node |
| 27 | + waypoints << node |
| 28 | + end |
| 29 | + got = {'waypoints' => row['waypoints'] } |
| 30 | + response = request_route waypoints, params |
| 31 | + else |
| 32 | + raise "*** no waypoints" |
22 | 33 | end |
23 | | - got = {'waypoints' => row['waypoints'] } |
24 | | - else |
25 | | - raise "*** no waypoints" |
26 | 34 | end |
27 | 35 |
|
28 | | - params = {} |
29 | 36 | row.each_pair do |k,v| |
30 | 37 | if k =~ /param:(.*)/ |
31 | 38 | if v=='(nil)' |
|
37 | 44 | end |
38 | 45 | end |
39 | 46 |
|
40 | | - response = request_route(waypoints, params) |
41 | | - if response.code == "200" && response.body.empty? == false |
| 47 | + if response.body.empty? == false |
42 | 48 | json = JSON.parse response.body |
| 49 | + end |
| 50 | + |
| 51 | + if response.body.empty? == false |
43 | 52 | if json['status'] == 0 |
44 | 53 | instructions = way_list json['route_instructions'] |
45 | 54 | bearings = bearing_list json['route_instructions'] |
|
51 | 60 | end |
52 | 61 | end |
53 | 62 |
|
54 | | - if table.headers.include? 'start' |
55 | | - got['start'] = instructions ? json['route_summary']['start_point'] : nil |
| 63 | + if table.headers.include? 'status' |
| 64 | + got['status'] = json['status'].to_s |
| 65 | + end |
| 66 | + if table.headers.include? 'message' |
| 67 | + got['message'] = json['status_message'] |
56 | 68 | end |
57 | | - if table.headers.include? 'end' |
58 | | - got['end'] = instructions ? json['route_summary']['end_point'] : nil |
| 69 | + if table.headers.include? '#' # comment column |
| 70 | + got['#'] = row['#'] # copy value so it always match |
59 | 71 | end |
60 | | - if table.headers.include? 'route' |
61 | | - got['route'] = (instructions || '').strip |
62 | | - if table.headers.include?('distance') |
63 | | - if row['distance']!='' |
64 | | - raise "*** Distance must be specied in meters. (ex: 250m)" unless row['distance'] =~ /\d+m/ |
| 72 | + |
| 73 | + if response.code == "200" |
| 74 | + if table.headers.include? 'start' |
| 75 | + got['start'] = instructions ? json['route_summary']['start_point'] : nil |
| 76 | + end |
| 77 | + if table.headers.include? 'end' |
| 78 | + got['end'] = instructions ? json['route_summary']['end_point'] : nil |
| 79 | + end |
| 80 | + if table.headers.include? 'route' |
| 81 | + got['route'] = (instructions || '').strip |
| 82 | + if table.headers.include?('distance') |
| 83 | + if row['distance']!='' |
| 84 | + raise "*** Distance must be specied in meters. (ex: 250m)" unless row['distance'] =~ /\d+m/ |
| 85 | + end |
| 86 | + got['distance'] = instructions ? "#{json['route_summary']['total_distance'].to_s}m" : '' |
65 | 87 | end |
66 | | - got['distance'] = instructions ? "#{json['route_summary']['total_distance'].to_s}m" : '' |
67 | | - end |
68 | | - if table.headers.include?('time') |
69 | | - raise "*** Time must be specied in seconds. (ex: 60s)" unless row['time'] =~ /\d+s/ |
70 | | - got['time'] = instructions ? "#{json['route_summary']['total_time'].to_s}s" : '' |
71 | | - end |
72 | | - if table.headers.include?('speed') |
73 | | - if row['speed'] != '' && instructions |
74 | | - raise "*** Speed must be specied in km/h. (ex: 50 km/h)" unless row['speed'] =~ /\d+ km\/h/ |
75 | | - time = json['route_summary']['total_time'] |
76 | | - distance = json['route_summary']['total_distance'] |
77 | | - speed = time>0 ? (3.6*distance/time).to_i : nil |
78 | | - got['speed'] = "#{speed} km/h" |
79 | | - else |
80 | | - got['speed'] = '' |
| 88 | + if table.headers.include?('time') |
| 89 | + raise "*** Time must be specied in seconds. (ex: 60s)" unless row['time'] =~ /\d+s/ |
| 90 | + got['time'] = instructions ? "#{json['route_summary']['total_time'].to_s}s" : '' |
| 91 | + end |
| 92 | + if table.headers.include?('speed') |
| 93 | + if row['speed'] != '' && instructions |
| 94 | + raise "*** Speed must be specied in km/h. (ex: 50 km/h)" unless row['speed'] =~ /\d+ km\/h/ |
| 95 | + time = json['route_summary']['total_time'] |
| 96 | + distance = json['route_summary']['total_distance'] |
| 97 | + speed = time>0 ? (3.6*distance/time).to_i : nil |
| 98 | + got['speed'] = "#{speed} km/h" |
| 99 | + else |
| 100 | + got['speed'] = '' |
| 101 | + end |
| 102 | + end |
| 103 | + if table.headers.include? 'bearing' |
| 104 | + got['bearing'] = bearings |
| 105 | + end |
| 106 | + if table.headers.include? 'compass' |
| 107 | + got['compass'] = compasses |
| 108 | + end |
| 109 | + if table.headers.include? 'turns' |
| 110 | + got['turns'] = turns |
| 111 | + end |
| 112 | + if table.headers.include? 'modes' |
| 113 | + got['modes'] = modes |
| 114 | + end |
| 115 | + if table.headers.include? 'times' |
| 116 | + got['times'] = times |
| 117 | + end |
| 118 | + if table.headers.include? 'distances' |
| 119 | + got['distances'] = distances |
81 | 120 | end |
82 | | - end |
83 | | - if table.headers.include? 'bearing' |
84 | | - got['bearing'] = bearings |
85 | | - end |
86 | | - if table.headers.include? 'compass' |
87 | | - got['compass'] = compasses |
88 | | - end |
89 | | - if table.headers.include? 'turns' |
90 | | - got['turns'] = turns |
91 | | - end |
92 | | - if table.headers.include? 'modes' |
93 | | - got['modes'] = modes |
94 | | - end |
95 | | - if table.headers.include? 'times' |
96 | | - got['times'] = times |
97 | | - end |
98 | | - if table.headers.include? 'distances' |
99 | | - got['distances'] = distances |
100 | | - end |
101 | | - if table.headers.include? 'status' |
102 | | - got['status'] = json['status'].to_s |
103 | | - end |
104 | | - if table.headers.include? 'message' |
105 | | - got['message'] = json['status_message'] |
106 | | - end |
107 | | - if table.headers.include? '#' # comment column |
108 | | - got['#'] = row['#'] # copy value so it always match |
109 | 121 | end |
110 | 122 | end |
111 | 123 |
|
|
0 commit comments