osrm-backend/profiles/bicycle.lua

414 lines
14 KiB
Lua
Raw Normal View History

api_version = 0
-- Bicycle profile
local find_access_tag = require("lib/access").find_access_tag
local limit = require("lib/maxspeed").limit
local set_classification = require("lib/guidance").set_classification
2012-12-19 09:53:05 -05:00
-- Begin of globals
barrier_whitelist = { [""] = true, ["cycle_barrier"] = true, ["bollard"] = true, ["entrance"] = true, ["cattle_grid"] = true, ["border_control"] = true, ["toll_booth"] = true, ["sally_port"] = true, ["gate"] = true, ["no"] = true, ["block"] = true }
2014-03-27 16:19:26 -04:00
access_tag_whitelist = { ["yes"] = true, ["permissive"] = true, ["designated"] = true }
access_tag_blacklist = { ["no"] = true, ["private"] = true, ["agricultural"] = true, ["forestry"] = true, ["delivery"] = true }
2016-04-04 05:09:13 -04:00
access_tags_hierarchy = { "bicycle", "vehicle", "access" }
2012-10-05 06:06:42 -04:00
cycleway_tags = {["track"]=true,["lane"]=true,["opposite"]=true,["opposite_lane"]=true,["opposite_track"]=true,["share_busway"]=true,["sharrow"]=true,["shared"]=true }
restrictions = { "bicycle" }
2015-05-15 08:45:50 -04:00
unsafe_highway_list = { ["primary"] = true, ["secondary"] = true, ["tertiary"] = true, ["primary_link"] = true, ["secondary_link"] = true, ["tertiary_link"] = true}
2015-05-15 08:45:50 -04:00
local default_speed = 15
local walking_speed = 6
2013-08-12 12:00:56 -04:00
bicycle_speeds = {
2014-03-27 16:19:26 -04:00
["cycleway"] = default_speed,
["primary"] = default_speed,
["primary_link"] = default_speed,
["secondary"] = default_speed,
["secondary_link"] = default_speed,
["tertiary"] = default_speed,
["tertiary_link"] = default_speed,
["residential"] = default_speed,
["unclassified"] = default_speed,
["living_street"] = default_speed,
["road"] = default_speed,
["service"] = default_speed,
["track"] = 12,
["path"] = 12
--["footway"] = 12,
--["pedestrian"] = 12,
2012-10-11 10:50:17 -04:00
}
2013-08-12 12:00:56 -04:00
pedestrian_speeds = {
2014-03-27 16:19:26 -04:00
["footway"] = walking_speed,
["pedestrian"] = walking_speed,
["steps"] = 2
2012-10-11 10:50:17 -04:00
}
2013-08-12 12:00:56 -04:00
railway_speeds = {
2014-03-27 16:19:26 -04:00
["train"] = 10,
["railway"] = 10,
["subway"] = 10,
["light_rail"] = 10,
["monorail"] = 10,
["tram"] = 10
2012-10-11 10:50:17 -04:00
}
2013-08-12 12:00:56 -04:00
platform_speeds = {
2014-03-27 16:19:26 -04:00
["platform"] = walking_speed
}
2013-08-12 12:00:56 -04:00
amenity_speeds = {
2014-03-27 16:19:26 -04:00
["parking"] = 10,
["parking_entrance"] = 10
}
2013-08-12 12:00:56 -04:00
man_made_speeds = {
2014-03-27 16:19:26 -04:00
["pier"] = walking_speed
}
2013-08-12 12:00:56 -04:00
route_speeds = {
2014-03-27 16:19:26 -04:00
["ferry"] = 5
}
bridge_speeds = {
["movable"] = 5
}
2013-08-12 12:00:56 -04:00
surface_speeds = {
2014-03-27 16:19:26 -04:00
["asphalt"] = default_speed,
["cobblestone:flattened"] = 10,
["paving_stones"] = 10,
["compacted"] = 10,
["cobblestone"] = 6,
["unpaved"] = 6,
["fine_gravel"] = 6,
["gravel"] = 6,
["pebblestone"] = 6,
2014-03-27 16:19:26 -04:00
["ground"] = 6,
["dirt"] = 6,
["earth"] = 6,
["grass"] = 6,
["mud"] = 3,
["sand"] = 3
2013-04-27 11:00:25 -04:00
}
2015-12-18 09:54:33 -05:00
-- these need to be global because they are accesed externaly
2016-04-12 12:47:00 -04:00
properties.traffic_signal_penalty = 2
properties.u_turn_penalty = 20
properties.max_speed_for_map_matching = 110/3.6 -- kmph -> m/s
properties.use_turn_restrictions = false
2016-04-12 12:47:00 -04:00
properties.continue_straight_at_waypoint = false
2015-05-15 08:45:50 -04:00
local obey_oneway = true
local ignore_areas = true
2016-08-19 07:51:53 -04:00
local turn_penalty = 6
2015-05-15 08:45:50 -04:00
local turn_bias = 1.4
-- reduce the driving speed by 30% for unsafe roads
-- local safety_penalty = 0.7
local safety_penalty = 1.0
local use_public_transport = true
2014-03-05 13:16:17 -05:00
local function parse_maxspeed(source)
if not source then
return 0
end
local n = tonumber(source:match("%d*"))
if not n then
n = 0
end
if string.match(source, "mph") or string.match(source, "mp/h") then
n = (n*1609)/1000
2014-03-05 13:16:17 -05:00
end
return n
end
function get_restrictions(vector)
for i,v in ipairs(restrictions) do
2014-03-27 16:19:26 -04:00
vector:Add(v)
end
end
2014-08-27 12:38:30 -04:00
function node_function (node, result)
2015-05-15 08:45:50 -04:00
-- parse access and barrier tags
local highway = node:get_value_by_key("highway")
local is_crossing = highway and highway == "crossing"
2016-04-04 05:09:13 -04:00
local access = find_access_tag(node, access_tags_hierarchy)
2015-05-15 11:01:21 -04:00
if access and access ~= "" then
-- access restrictions on crossing nodes are not relevant for
-- the traffic on the road
if access_tag_blacklist[access] and not is_crossing then
2015-05-15 08:45:50 -04:00
result.barrier = true
end
else
local barrier = node:get_value_by_key("barrier")
if barrier and "" ~= barrier then
if not barrier_whitelist[barrier] then
result.barrier = true
end
end
end
2014-03-05 13:16:17 -05:00
2015-05-15 08:45:50 -04:00
-- check if node is a traffic light
local tag = node:get_value_by_key("highway")
if tag and "traffic_signals" == tag then
result.traffic_lights = true
2015-05-15 08:45:50 -04:00
end
2014-03-05 13:16:17 -05:00
end
2014-08-27 12:38:30 -04:00
function way_function (way, result)
2014-03-27 16:19:26 -04:00
-- initial routability check, filters out buildings, boundaries, etc
2014-08-27 12:38:30 -04:00
local highway = way:get_value_by_key("highway")
local route = way:get_value_by_key("route")
local man_made = way:get_value_by_key("man_made")
local railway = way:get_value_by_key("railway")
local amenity = way:get_value_by_key("amenity")
local public_transport = way:get_value_by_key("public_transport")
local bridge = way:get_value_by_key("bridge")
2014-03-27 16:19:26 -04:00
if (not highway or highway == '') and
(not route or route == '') and
2015-05-15 08:45:50 -04:00
(not use_public_transport or not railway or railway=='') and
2014-03-27 16:19:26 -04:00
(not amenity or amenity=='') and
(not man_made or man_made=='') and
(not public_transport or public_transport=='') and
(not bridge or bridge=='')
2014-03-27 16:19:26 -04:00
then
2014-08-20 11:05:39 -04:00
return
2014-03-27 16:19:26 -04:00
end
-- don't route on ways or railways that are still under construction
if highway=='construction' or railway=='construction' then
2014-08-20 11:05:39 -04:00
return
2014-03-27 16:19:26 -04:00
end
-- access
2016-04-04 05:09:13 -04:00
local access = find_access_tag(way, access_tags_hierarchy)
2014-08-27 12:38:30 -04:00
if access and access_tag_blacklist[access] then
2014-08-20 11:05:39 -04:00
return
2014-03-27 16:19:26 -04:00
end
result.forward_mode = mode.cycling
result.backward_mode = mode.cycling
2014-03-27 16:19:26 -04:00
-- other tags
2014-08-27 12:38:30 -04:00
local name = way:get_value_by_key("name")
local ref = way:get_value_by_key("ref")
local junction = way:get_value_by_key("junction")
local maxspeed = parse_maxspeed(way:get_value_by_key ( "maxspeed") )
local maxspeed_forward = parse_maxspeed(way:get_value_by_key( "maxspeed:forward"))
local maxspeed_backward = parse_maxspeed(way:get_value_by_key( "maxspeed:backward"))
local barrier = way:get_value_by_key("barrier")
local oneway = way:get_value_by_key("oneway")
local onewayClass = way:get_value_by_key("oneway:bicycle")
local cycleway = way:get_value_by_key("cycleway")
local cycleway_left = way:get_value_by_key("cycleway:left")
local cycleway_right = way:get_value_by_key("cycleway:right")
local duration = way:get_value_by_key("duration")
local service = way:get_value_by_key("service")
local area = way:get_value_by_key("area")
local foot = way:get_value_by_key("foot")
local foot_forward = way:get_value_by_key("foot:forward")
local foot_backward = way:get_value_by_key("foot:backward")
2014-08-27 12:38:30 -04:00
local bicycle = way:get_value_by_key("bicycle")
2014-03-27 16:19:26 -04:00
-- name
2016-09-05 09:01:51 -04:00
if name and "" ~= name then
2014-08-27 12:38:30 -04:00
result.name = name
2014-03-27 16:19:26 -04:00
end
2016-09-05 09:01:51 -04:00
-- ref
if ref and "" ~= ref then
result.ref = ref
end
2014-03-27 16:19:26 -04:00
-- roundabout handling
if junction and ("roundabout" == junction) then
result.roundabout = true
2014-03-27 16:19:26 -04:00
end
if junction and ("circular" == junction) then
result.circular = true;
end
2014-03-27 16:19:26 -04:00
-- speed
local bridge_speed = bridge_speeds[bridge]
if (bridge_speed and bridge_speed > 0) then
highway = bridge
if duration and durationIsValid(duration) then
result.duration = math.max( parseDuration(duration), 1 )
end
result.forward_speed = bridge_speed
result.backward_speed = bridge_speed
elseif route_speeds[route] then
2014-03-27 16:19:26 -04:00
-- ferries (doesn't cover routes tagged using relations)
result.forward_mode = mode.ferry
result.backward_mode = mode.ferry
2014-08-27 12:38:30 -04:00
if duration and durationIsValid(duration) then
result.duration = math.max( 1, parseDuration(duration) )
2014-03-27 16:19:26 -04:00
else
2014-08-27 12:38:30 -04:00
result.forward_speed = route_speeds[route]
result.backward_speed = route_speeds[route]
2014-03-27 16:19:26 -04:00
end
2015-05-15 08:45:50 -04:00
-- public transport
elseif use_public_transport and railway and platform_speeds[railway] then
2014-03-27 16:19:26 -04:00
-- railway platforms (old tagging scheme)
2014-08-27 12:38:30 -04:00
result.forward_speed = platform_speeds[railway]
result.backward_speed = platform_speeds[railway]
2015-05-15 08:45:50 -04:00
elseif use_public_transport and platform_speeds[public_transport] then
2014-03-27 16:19:26 -04:00
-- public_transport platforms (new tagging platform)
2014-08-27 12:38:30 -04:00
result.forward_speed = platform_speeds[public_transport]
result.backward_speed = platform_speeds[public_transport]
2015-05-15 08:45:50 -04:00
elseif use_public_transport and railway and railway_speeds[railway] then
result.forward_mode = mode.train
result.backward_mode = mode.train
2014-03-27 16:19:26 -04:00
-- railways
if access and access_tag_whitelist[access] then
2014-08-27 12:38:30 -04:00
result.forward_speed = railway_speeds[railway]
result.backward_speed = railway_speeds[railway]
end
2014-03-27 16:19:26 -04:00
elseif amenity and amenity_speeds[amenity] then
-- parking areas
2014-08-27 12:38:30 -04:00
result.forward_speed = amenity_speeds[amenity]
result.backward_speed = amenity_speeds[amenity]
2014-03-27 16:19:26 -04:00
elseif bicycle_speeds[highway] then
-- regular ways
2014-08-27 12:38:30 -04:00
result.forward_speed = bicycle_speeds[highway]
result.backward_speed = bicycle_speeds[highway]
if safety_penalty < 1 and unsafe_highway_list[highway] then
2015-05-15 09:35:48 -04:00
result.forward_speed = result.forward_speed * safety_penalty
result.backward_speed = result.backward_speed * safety_penalty
2015-05-15 08:45:50 -04:00
end
2014-03-27 16:19:26 -04:00
elseif access and access_tag_whitelist[access] then
-- unknown way, but valid access tag
2014-08-27 12:38:30 -04:00
result.forward_speed = default_speed
result.backward_speed = default_speed
2014-03-27 16:19:26 -04:00
else
-- biking not allowed, maybe we can push our bike?
-- essentially requires pedestrian profiling, for example foot=no mean we can't push a bike
if foot ~= 'no' and (junction ~= "roundabout" and junction ~= "circular") then
2014-03-27 16:19:26 -04:00
if pedestrian_speeds[highway] then
-- pedestrian-only ways and areas
2014-08-27 12:38:30 -04:00
result.forward_speed = pedestrian_speeds[highway]
result.backward_speed = pedestrian_speeds[highway]
result.forward_mode = mode.pushing_bike
result.backward_mode = mode.pushing_bike
2014-03-27 16:19:26 -04:00
elseif man_made and man_made_speeds[man_made] then
-- man made structures
2014-08-27 12:38:30 -04:00
result.forward_speed = man_made_speeds[man_made]
result.backward_speed = man_made_speeds[man_made]
result.forward_mode = mode.pushing_bike
result.backward_mode = mode.pushing_bike
2014-03-27 16:19:26 -04:00
elseif foot == 'yes' then
2014-08-27 12:38:30 -04:00
result.forward_speed = walking_speed
result.backward_speed = walking_speed
result.forward_mode = mode.pushing_bike
result.backward_mode = mode.pushing_bike
2014-08-12 08:18:02 -04:00
elseif foot_forward == 'yes' then
2014-08-27 12:38:30 -04:00
result.forward_speed = walking_speed
result.forward_mode = mode.pushing_bike
result.backward_mode = mode.inaccessible
2014-08-12 08:18:02 -04:00
elseif foot_backward == 'yes' then
2014-08-27 12:38:30 -04:00
result.forward_speed = walking_speed
result.forward_mode = mode.inaccessible
result.backward_mode = mode.pushing_bike
2014-03-27 16:19:26 -04:00
end
end
2014-03-27 16:19:26 -04:00
end
-- direction
local impliedOneway = false
if junction == "roundabout" or junction == "circular" or highway == "motorway" then
2014-03-27 16:19:26 -04:00
impliedOneway = true
end
if onewayClass == "yes" or onewayClass == "1" or onewayClass == "true" then
result.backward_mode = mode.inaccessible
2014-03-27 16:19:26 -04:00
elseif onewayClass == "no" or onewayClass == "0" or onewayClass == "false" then
2014-08-12 08:18:02 -04:00
-- prevent implied oneway
2014-03-27 16:19:26 -04:00
elseif onewayClass == "-1" then
result.forward_mode = mode.inaccessible
2014-03-27 16:19:26 -04:00
elseif oneway == "no" or oneway == "0" or oneway == "false" then
2014-08-12 08:18:02 -04:00
-- prevent implied oneway
2014-03-27 16:19:26 -04:00
elseif cycleway and string.find(cycleway, "opposite") == 1 then
if impliedOneway then
result.forward_mode = mode.inaccessible
result.backward_mode = mode.cycling
2014-08-27 12:38:30 -04:00
result.backward_speed = bicycle_speeds["cycleway"]
2013-04-27 11:00:25 -04:00
end
2014-03-27 16:19:26 -04:00
elseif cycleway_left and cycleway_tags[cycleway_left] and cycleway_right and cycleway_tags[cycleway_right] then
2014-08-12 08:18:02 -04:00
-- prevent implied
2014-03-27 16:19:26 -04:00
elseif cycleway_left and cycleway_tags[cycleway_left] then
if impliedOneway then
result.forward_mode = mode.inaccessible
result.backward_mode = mode.cycling
2014-08-27 12:38:30 -04:00
result.backward_speed = bicycle_speeds["cycleway"]
2014-03-27 16:19:26 -04:00
end
elseif cycleway_right and cycleway_tags[cycleway_right] then
if impliedOneway then
result.forward_mode = mode.cycling
2014-08-27 12:38:30 -04:00
result.backward_speed = bicycle_speeds["cycleway"]
2016-04-01 05:39:47 -04:00
result.backward_mode = mode.inaccessible
2014-03-27 16:19:26 -04:00
end
elseif oneway == "-1" then
result.forward_mode = mode.inaccessible
2014-08-12 08:18:02 -04:00
elseif oneway == "yes" or oneway == "1" or oneway == "true" or impliedOneway then
result.backward_mode = mode.inaccessible
2014-03-27 16:19:26 -04:00
end
2014-08-27 12:38:30 -04:00
2014-03-27 16:19:26 -04:00
-- pushing bikes
if bicycle_speeds[highway] or pedestrian_speeds[highway] then
if foot ~= "no" and junction ~= "roundabout" and junction ~= "circular" then
if result.backward_mode == mode.inaccessible then
2014-08-27 12:38:30 -04:00
result.backward_speed = walking_speed
result.backward_mode = mode.pushing_bike
elseif result.forward_mode == mode.inaccessible then
2014-08-27 12:38:30 -04:00
result.forward_speed = walking_speed
result.forward_mode = mode.pushing_bike
2014-03-27 16:19:26 -04:00
end
end
2014-03-27 16:19:26 -04:00
end
-- cycleways
if cycleway and cycleway_tags[cycleway] then
2014-08-27 12:38:30 -04:00
result.forward_speed = bicycle_speeds["cycleway"]
result.backward_speed = bicycle_speeds["cycleway"]
2014-03-27 16:19:26 -04:00
elseif cycleway_left and cycleway_tags[cycleway_left] then
2014-08-27 12:38:30 -04:00
result.forward_speed = bicycle_speeds["cycleway"]
result.backward_speed = bicycle_speeds["cycleway"]
2014-03-27 16:19:26 -04:00
elseif cycleway_right and cycleway_tags[cycleway_right] then
2014-08-27 12:38:30 -04:00
result.forward_speed = bicycle_speeds["cycleway"]
result.backward_speed = bicycle_speeds["cycleway"]
2014-03-27 16:19:26 -04:00
end
2014-08-12 08:18:02 -04:00
-- dismount
if bicycle == "dismount" then
result.forward_mode = mode.pushing_bike
result.backward_mode = mode.pushing_bike
2014-08-27 12:38:30 -04:00
result.forward_speed = walking_speed
result.backward_speed = walking_speed
2014-08-12 08:18:02 -04:00
end
-- reduce speed on bad surfaces
local surface = way:get_value_by_key("surface")
if surface and surface_speeds[surface] then
result.forward_speed = math.min(surface_speeds[surface], result.forward_speed)
result.backward_speed = math.min(surface_speeds[surface], result.backward_speed)
2014-03-27 16:19:26 -04:00
end
-- set the road classification based on guidance globals configuration
set_classification(highway,result,way)
2014-03-27 16:19:26 -04:00
-- maxspeed
limit( result, maxspeed, maxspeed_forward, maxspeed_backward )
end
2013-02-23 02:33:33 -05:00
function turn_function (angle)
2014-03-27 16:19:26 -04:00
-- compute turn penalty as angle^2, with a left/right bias
2016-08-19 07:51:53 -04:00
-- multiplying by 10 converts to deci-seconds see issue #1318
k = 10*turn_penalty/(90.0*90.0)
2014-03-27 16:19:26 -04:00
if angle>=0 then
return angle*angle*k/turn_bias
else
return angle*angle*k*turn_bias
end
2013-02-23 02:33:33 -05:00
end