@@ -32,23 +32,27 @@ speed_profile = {
3232 [" default" ] = 10
3333}
3434
35- take_minimum_of_speeds = false
36- obey_oneway = true
37- obey_bollards = true
38- use_turn_restrictions = true
39- ignore_areas = true -- future feature
40- traffic_signal_penalty = 2
41- u_turn_penalty = 20
35+ local take_minimum_of_speeds = false
36+ local obey_oneway = true
37+ local obey_bollards = true
38+ local use_turn_restrictions = true
39+ local ignore_areas = true -- future feature
40+ local traffic_signal_penalty = 2
41+ local u_turn_penalty = 20
42+
43+ local abs = math.abs
44+ local min = math.min
45+ local max = math.max
4246
4347-- End of globals
44- function find_access_tag (source ,access_tags_hierachy )
45- for i ,v in ipairs (access_tags_hierachy ) do
46- local tag = source .tags :Find (v )
47- if tag ~= ' ' then
48- return tag
49- end
48+ local function find_access_tag (source ,access_tags_hierachy )
49+ for i ,v in ipairs (access_tags_hierachy ) do
50+ local has_tag = source .tags :Holds (v )
51+ if has_tag then
52+ return source .tags :Find (v )
5053 end
51- return nil
54+ end
55+ return nil
5256end
5357
5458function get_exceptions (vector )
@@ -58,36 +62,36 @@ function get_exceptions(vector)
5862end
5963
6064local function parse_maxspeed (source )
61- if source == nil then
65+ if not source then
6266 return 0
6367 end
6468 local n = tonumber (source :match (" %d*" ))
65- if n == nil then
69+ if not n then
6670 n = 0
6771 end
6872 if string.match (source , " mph" ) or string.match (source , " mp/h" ) then
6973 n = (n * 1609 )/ 1000 ;
7074 end
71- return math. abs (n )
75+ return abs (n * 0.66 )
7276end
7377
7478function node_function (node )
75- local barrier = node .tags :Find (" barrier" )
7679 local access = find_access_tag (node , access_tags_hierachy )
77- local traffic_signal = node .tags :Find (" highway" )
7880
7981 -- flag node if it carries a traffic light
80-
81- if traffic_signal == " traffic_signals" then
82- node .traffic_light = true ;
82+ if node .tags :Holds (" highway" ) then
83+ if node .tags :Find (" highway" ) == " traffic_signals" then
84+ node .traffic_light = true ;
85+ end
8386 end
8487
8588 -- parse access and barrier tags
86- if access and access ~= " " then
89+ if access and access ~= " " then
8790 if access_tag_blacklist [access ] then
8891 node .bollard = true
8992 end
90- elseif barrier and barrier ~= " " then
93+ elseif node .tags :Holds (" barrier" ) then
94+ local barrier = node .tags :Find (" barrier" )
9195 if barrier_whitelist [barrier ] then
9296 return
9397 else
@@ -96,12 +100,14 @@ function node_function (node)
96100 end
97101end
98102
99-
100103function way_function (way )
101104 -- we dont route over areas
102- local area = way .tags :Find (" area" )
103- if ignore_areas and (" yes" == area ) then
104- return
105+ local is_area = way .tags :Holds (" area" )
106+ if ignore_areas and is_area then
107+ local area = way .tags :Find (" area" )
108+ if " yes" == area then
109+ return
110+ end
105111 end
106112
107113 -- check if oneway tag is unsupported
@@ -110,14 +116,20 @@ function way_function (way)
110116 return
111117 end
112118
113- local impassable = way .tags :Find (" impassable" )
114- if " yes" == impassable then
115- return
119+ local is_impassable = way .tags :Holds (" impassable" )
120+ if is_impassable then
121+ local impassable = way .tags :Find (" impassable" )
122+ if " yes" == impassable then
123+ return
124+ end
116125 end
117126
118- local status = way .tags :Find (" status" )
119- if " impassable" == status then
120- return
127+ local is_status = way .tags :Holds (" status" )
128+ if is_status then
129+ local status = way .tags :Find (" status" )
130+ if " impassable" == status then
131+ return
132+ end
121133 end
122134
123135 -- Check if we are allowed to access the way
@@ -128,63 +140,71 @@ function way_function (way)
128140
129141 -- Second, parse the way according to these properties
130142 local highway = way .tags :Find (" highway" )
131- local name = way .tags :Find (" name" )
132- local ref = way .tags :Find (" ref" )
133- local junction = way .tags :Find (" junction" )
134143 local route = way .tags :Find (" route" )
135- local maxspeed = parse_maxspeed (way .tags :Find ( " maxspeed" ) )
136- local maxspeed_forward = parse_maxspeed (way .tags :Find ( " maxspeed:forward" ))
137- local maxspeed_backward = parse_maxspeed (way .tags :Find ( " maxspeed:backward" ))
138- local barrier = way .tags :Find (" barrier" )
139- local cycleway = way .tags :Find (" cycleway" )
140- local duration = way .tags :Find (" duration" )
141- local service = way .tags :Find (" service" )
142-
143- -- Set the name that will be used for instructions
144- if " " ~= ref then
145- way .name = ref
146- elseif " " ~= name then
147- way .name = name
148- -- else
149- -- way.name = highway -- if no name exists, use way type
150- end
151-
152- if " roundabout" == junction then
153- way .roundabout = true ;
154- end
155144
156145 -- Handling ferries and piers
157- if (speed_profile [route ] ~= nil and speed_profile [route ] > 0 ) then
146+ local route_speed = speed_profile [route ]
147+ if (route_speed and route_speed > 0 ) then
148+ highway = route ;
149+ local duration = way .tags :Find (" duration" )
158150 if durationIsValid (duration ) then
159- way .duration = math. max ( parseDuration (duration ), 1 );
151+ way .duration = max ( parseDuration (duration ), 1 );
160152 end
161153 way .direction = Way .bidirectional
162- if speed_profile [ route ] ~= nil then
163- highway = route ;
164- end
165- if tonumber ( way . duration ) < 0 then
166- way . speed = speed_profile [ highway ]
167- end
154+ way . speed = route_speed
155+ end
156+
157+ -- leave early of this way is not accessible
158+ if " " == highway then
159+ return
168160 end
169161
170- -- Set the avg speed on the way if it is accessible by road class
171- if (speed_profile [highway ] ~= nil and way .speed == - 1 ) then
172- if maxspeed > speed_profile [highway ] then
173- way .speed = maxspeed
162+ if way .speed == - 1 then
163+ local highway_speed = speed_profile [highway ]
164+ local max_speed = parse_maxspeed ( way .tags :Find (" maxspeed" ) )
165+ -- Set the avg speed on the way if it is accessible by road class
166+ if highway_speed then
167+ if max_speed > highway_speed then
168+ way .speed = max_speed
169+ -- max_speed = math.huge
170+ else
171+ way .speed = highway_speed
172+ end
174173 else
175- if 0 == maxspeed then
176- maxspeed = math.huge
174+ -- Set the avg speed on ways that are marked accessible
175+ if access_tag_whitelist [access ] then
176+ way .speed = speed_profile [" default" ]
177177 end
178- way .speed = math.min (speed_profile [highway ], maxspeed )
179178 end
179+ if 0 == max_speed then
180+ max_speed = math.huge
181+ end
182+ way .speed = min (way .speed , max_speed )
180183 end
181184
182- -- Set the avg speed on ways that are marked accessible
183- if " " ~= highway and access_tag_whitelist [access ] and way .speed == - 1 then
184- if 0 == maxspeed then
185- maxspeed = math.huge
186- end
187- way .speed = math.min (speed_profile [" default" ], maxspeed )
185+ if - 1 == way .speed then
186+ return
187+ end
188+
189+ -- parse the remaining tags
190+ local name = way .tags :Find (" name" )
191+ local ref = way .tags :Find (" ref" )
192+ local junction = way .tags :Find (" junction" )
193+ -- local barrier = way.tags:Find("barrier")
194+ -- local cycleway = way.tags:Find("cycleway")
195+ local service = way .tags :Find (" service" )
196+
197+ -- Set the name that will be used for instructions
198+ if " " ~= ref then
199+ way .name = ref
200+ elseif " " ~= name then
201+ way .name = name
202+ -- else
203+ -- way.name = highway -- if no name exists, use way type
204+ end
205+
206+ if " roundabout" == junction then
207+ way .roundabout = true ;
188208 end
189209
190210 -- Set access restriction flag if access is allowed under certain restrictions only
@@ -214,18 +234,20 @@ function way_function (way)
214234 end
215235
216236 -- Override speed settings if explicit forward/backward maxspeeds are given
217- if way .speed > 0 and maxspeed_forward ~= nil and maxspeed_forward > 0 then
237+ local maxspeed_forward = parse_maxspeed (way .tags :Find ( " maxspeed:forward" ))
238+ local maxspeed_backward = parse_maxspeed (way .tags :Find ( " maxspeed:backward" ))
239+ if maxspeed_forward > 0 then
218240 if Way .bidirectional == way .direction then
219241 way .backward_speed = way .speed
220242 end
221243 way .speed = maxspeed_forward
222244 end
223- if maxspeed_backward ~= nil and maxspeed_backward > 0 then
245+ if maxspeed_backward > 0 then
224246 way .backward_speed = maxspeed_backward
225247 end
226248
227249 -- Override general direction settings of there is a specific one for our mode of travel
228- if ignore_in_grid [highway ] ~= nil and ignore_in_grid [ highway ] then
250+ if ignore_in_grid [highway ] then
229251 way .ignore_in_grid = true
230252 end
231253 way .type = 1
0 commit comments