profiles: Directional helper, some car refactoring

This commit is contained in:
Emil Tin
2016-11-18 20:49:51 +01:00
committed by Daniel J. H
parent 0fbd18b0dd
commit 532cbfce13
6 changed files with 334 additions and 274 deletions
+59
View File
@@ -0,0 +1,59 @@
local Directional = {}
-- return [forward,backward] values for a specific tag.
-- e.g. for maxspeed search forward:
-- maxspeed:forward
-- maxspeed
-- and backward:
-- maxspeed:backward
-- maxspeed
function Directional.get_values_by_key(way,data,key)
local forward = way:get_value_by_key(key .. ':forward')
local backward = way:get_value_by_key(key .. ':backward')
if forward and backward then
return forward, backward
end
local common = way:get_value_by_key(key)
return forward or common,
backward or common
end
-- return [forward,backward] values, searching a
-- prioritized sequence of tags
-- e.g. for the sequence [maxspeed,advisory] search forward:
-- maxspeed:forward
-- maxspeed
-- advisory:forward
-- advisory
-- and for backward:
-- maxspeed:backward
-- maxspeed
-- advisory:backward
-- advisory
function Directional.get_values_by_set(way,data,keys)
local forward, backward
for i,key in ipairs(keys) do
if not forward then
forward = way:get_value_by_key(key .. ':forward')
end
if not backward then
backward = way:get_value_by_key(key .. ':backward')
end
if not forward or not backward then
local common = way:get_value_by_key(key)
forward = forward or common
backward = backward or common
end
if forward and backward then
break
end
end
return forward, backward
end
return Directional
+120 -128
View File
@@ -1,154 +1,146 @@
local Directional = require('lib/directional')
local Set = require('lib/set')
local Guidance = {}
-- Guidance: Default Mapping from roads to types/priorities
highway_classes = { ["motorway"] = road_priority_class.motorway,
["motorway_link"] = road_priority_class.link_road,
["trunk"] = road_priority_class.trunk,
["trunk_link"] = road_priority_class.link_road,
["primary"] = road_priority_class.primary,
["primary_link"] = road_priority_class.link_road,
["secondary"] = road_priority_class.secondary,
["secondary_link"] = road_priority_class.link_road,
["tertiary"] = road_priority_class.tertiary,
["tertiary_link"] = road_priority_class.link_road,
["unclassified"] = road_priority_class.side_residential,
["residential"] = road_priority_class.side_residential,
["service"] = road_priority_class.connectivity,
["living_street"] = road_priority_class.main_residential,
["track"] = road_priority_class.bike_path,
["path"] = road_priority_class.bike_path,
["footway"] = road_priority_class.foot_path,
["pedestrian"] = road_priority_class.foot_path,
["steps"] = road_priority_class.foot_path}
highway_classes = {
motorway = road_priority_class.motorway,
motorway_link = road_priority_class.link_road,
trunk = road_priority_class.trunk,
trunk_link = road_priority_class.link_road,
primary = road_priority_class.primary,
primary_link = road_priority_class.link_road,
secondary = road_priority_class.secondary,
secondary_link = road_priority_class.link_road,
tertiary = road_priority_class.tertiary,
tertiary_link = road_priority_class.link_road,
unclassified = road_priority_class.side_residential,
residential = road_priority_class.side_residential,
service = road_priority_class.connectivity,
living_street = road_priority_class.main_residential,
track = road_priority_class.bike_path,
path = road_priority_class.bike_path,
footway = road_priority_class.foot_path,
pedestrian = road_priority_class.foot_path,
steps = road_priority_class.foot_path
}
default_highway_class = road_priority_class.connectivity;
motorway_types = { ["motorway"] = true, ["motorway_link"] = true, ["trunk"] = true, ["trunk_link"] = true }
motorway_types = Set {
'motorway',
'motorway_link',
'trunk',
'trunk_link'
}
-- these road types are set with a car in mind. For bicycle/walk we probably need different ones
road_types = { ["motorway"] = true,
["motorway_link"] = true,
["trunk"] = true,
["trunk_link"] = true,
["primary"] = true,
["primary_link"] = true,
["secondary"] = true,
["secondary_link"] = true,
["tertiary"] = true,
["tertiary_link"] = true,
["unclassified"] = true,
["residential"] = true,
["living_street"] = true }
road_types = Set {
'motorway',
'motorway_link',
'trunk',
'trunk_link',
'primary',
'primary_link',
'secondary',
'secondary_link',
'tertiary',
'tertiary_link',
'unclassified',
'residential',
'living_street'
}
link_types = { ["motorway_link"] = true, ["trunk_link"] = true, ["primary_link"] = true, ["secondary_link"] = true, ["tertiary_link"] = true }
link_types = Set {
'motorway_link',
'trunk_link',
'primary_link',
'secondary_link',
'tertiary_link'
}
function Guidance.set_classification (highway, result, input_way)
if motorway_types[highway] then
result.road_classification.motorway_class = true;
end
if link_types[highway] then
result.road_classification.link_class = true;
end
if highway_classes[highway] ~= nil then
result.road_classification.road_priority_class = highway_classes[highway]
else
result.road_classification.road_priority_class = default_highway_class
end
if road_types[highway] then
result.road_classification.may_be_ignored = false;
else
result.road_classification.may_be_ignored = true;
end
if motorway_types[highway] then
result.road_classification.motorway_class = true;
end
if link_types[highway] then
result.road_classification.link_class = true;
end
if highway_classes[highway] ~= nil then
result.road_classification.road_priority_class = highway_classes[highway]
else
result.road_classification.road_priority_class = default_highway_class
end
if road_types[highway] then
result.road_classification.may_be_ignored = false;
else
result.road_classification.may_be_ignored = true;
end
local lane_count = input_way:get_value_by_key("lanes")
if lane_count then
local lc = tonumber(lane_count)
if lc ~= nil then
result.road_classification.num_lanes = lc
end
else
local total_count = 0
local forward_count = input_way:get_value_by_key("lanes:forward")
if forward_count then
local fc = tonumber(forward_count)
if fc ~= nil then
total_count = fc
end
end
local backward_count = input_way:get_value_by_key("lanes:backward")
if backward_count then
local bc = tonumber(backward_count)
if bc ~= nil then
total_count = total_count + bc
end
end
if total_count ~= 0 then
result.road_classification.num_lanes = total_count
end
local lane_count = input_way:get_value_by_key("lanes")
if lane_count then
local lc = tonumber(lane_count)
if lc ~= nil then
result.road_classification.num_lanes = lc
end
else
local total_count = 0
local forward_count = input_way:get_value_by_key("lanes:forward")
if forward_count then
local fc = tonumber(forward_count)
if fc ~= nil then
total_count = fc
end
end
local backward_count = input_way:get_value_by_key("lanes:backward")
if backward_count then
local bc = tonumber(backward_count)
if bc ~= nil then
total_count = total_count + bc
end
end
if total_count ~= 0 then
result.road_classification.num_lanes = total_count
end
end
end
-- returns forward,backward psv lane count
local function get_psv_counts(way)
local psv = way:get_value_by_key("lanes:psv")
local psv_forward = way:get_value_by_key("lanes:psv:forward");
local psv_backward = way:get_value_by_key("lanes:psv:backward");
local fw = 0;
local bw = 0;
if psv then
fw = tonumber(psv)
if( fw == nil ) then
fw = 0
end
end
if psv_forward then
fw = tonumber(psv_forward)
if( fw == nil ) then
fw = 0
end
end
if psv_backward then
bw = tonumber(psv_backward);
if( bw == nil ) then
bw = 0
end
end
return fw, bw
local function get_psv_counts(way,data)
local psv_forward, psv_backward = Directional.get_values_by_key(way,data,'lanes:psv')
if psv_forward then
psv_forward = tonumber(psv_forward)
end
if psv_backward then
psv_backward = tonumber(psv_backward)
end
return psv_forward or 0,
psv_backward or 0
end
-- trims lane string with regard to supported lanes
local function process_lanes(turn_lane,vehicle_lane,first_count,second_count)
if turn_lane then
if vehicle_lane then
turn_lane = applyAccessTokens(turn_lane,vehicle_lane)
elseif first_count ~= 0 or second_count ~= 0 then
turn_lane = trimLaneString(turn_lane, first_count, second_count)
end
local function process_lanes(turn_lanes,vehicle_lanes,first_count,second_count)
if turn_lanes then
if vehicle_lanes then
return applyAccessTokens(turn_lanes,vehicle_lanes)
elseif first_count ~= 0 or second_count ~= 0 then
return trimLaneString(turn_lanes, first_count, second_count)
else
return turn_lanes
end
return turn_lane;
end
end
-- this is broken for left-sided driving. It needs to switch left and right in case of left-sided driving
function Guidance.get_turn_lanes(way)
local fw_psv = 0
local bw_psv = 0
fw_psv, bw_psv = get_psv_counts(way)
local turn_lanes = way:get_value_by_key("turn:lanes")
local turn_lanes_fw = way:get_value_by_key("turn:lanes:forward")
local turn_lanes_bw = way:get_value_by_key("turn:lanes:backward")
local vehicle_lanes = way:get_value_by_key("vehicle:lanes");
local vehicle_lanes_fw = way:get_value_by_key("vehicle:lanes:forward");
local vehicle_lanes_bw = way:get_value_by_key("vehicle:lanes:backward");
turn_lanes = process_lanes(turn_lanes,vehicle_lanes,bw_psv,fw_psv)
turn_lanes_fw = process_lanes(turn_lanes_fw,vehicle_lanes_fw,bw_psv,fw_psv)
--backwards turn lanes need to treat bw_psv as fw_psv and vice versa
turn_lanes_bw = process_lanes(turn_lanes_bw,vehicle_lanes_bw,fw_psv,bw_psv)
return turn_lanes, turn_lanes_fw, turn_lanes_bw
function Guidance.get_turn_lanes(way,data)
local psv_fw, psv_bw = get_psv_counts(way,data)
local turn_lanes_fw, turn_lanes_bw = Directional.get_values_by_key(way,data,'turn:lanes')
local vehicle_lanes_fw, vehicle_lanes_bw = Directional.get_values_by_key(way,data,'vehicle:lanes')
--note: backward lanes swap psv_bw and psv_fw
return process_lanes(turn_lanes_fw,vehicle_lanes_fw,psv_bw,psv_fw) or turn_lanes,
process_lanes(turn_lanes_bw,vehicle_lanes_bw,psv_fw,psv_bw) or turn_lanes
end
return Guidance