|
| 1 | +-- Copyright 2017-2019 Railnova SA <[email protected]>, Nikita Marchant <[email protected]> |
| 2 | +-- Code under the 2-clause BSD license |
| 3 | + |
| 4 | +api_version = 4 |
| 5 | + |
| 6 | +function setup() |
| 7 | + return { |
| 8 | + properties = { |
| 9 | + max_speed_for_map_matching = 400/3.6, -- speed conversion to m/s |
| 10 | + weight_name = 'train', |
| 11 | + left_hand_driving = false, |
| 12 | + u_turn_penalty = 60 * 2, -- 2 minutes to change cabin |
| 13 | + -- turn_duration = 20, |
| 14 | + continue_straight_at_waypoint = false, |
| 15 | + max_angle = 30, |
| 16 | + |
| 17 | + secondary_speed = 20, |
| 18 | + speed = 100, |
| 19 | + }, |
| 20 | + |
| 21 | + default_mode = mode.train, |
| 22 | + default_speed = 100, |
| 23 | +} |
| 24 | + |
| 25 | +end |
| 26 | + |
| 27 | + |
| 28 | +function ternary ( cond , T , F ) |
| 29 | + if cond then return T else return F end |
| 30 | +end |
| 31 | + |
| 32 | + |
| 33 | +function process_node(profile, node, result, relations) |
| 34 | + local railway = node:get_value_by_key("railway") |
| 35 | + |
| 36 | + -- refuse railway nodes that we cannot go through |
| 37 | + result.barrier = ( |
| 38 | + railway == "buffer_stop" or |
| 39 | + railway == "derail" |
| 40 | + ) |
| 41 | + result.traffic_lights = false |
| 42 | +end |
| 43 | + |
| 44 | +function process_way(profile, way, result, relations) |
| 45 | + local data = { |
| 46 | + railway = way:get_value_by_key("railway"), |
| 47 | + service = way:get_value_by_key("service"), |
| 48 | + usage = way:get_value_by_key("usage"), |
| 49 | + maxspeed = way:get_value_by_key("maxspeed"), |
| 50 | + gauge = way:get_value_by_key("gauge"), |
| 51 | + } |
| 52 | + |
| 53 | + -- Remove everything that is not railway |
| 54 | + if not data.railway then |
| 55 | + return |
| 56 | + -- Remove everything that is not a rail, a turntable, a traverser |
| 57 | + elseif ( |
| 58 | + data.railway ~= 'rail' and |
| 59 | + data.railway ~= 'turntable' and |
| 60 | + data.railway ~= 'traverser' and |
| 61 | + data.railway ~= 'ferry' |
| 62 | + ) then |
| 63 | + return |
| 64 | + -- Remove military and tourism rails |
| 65 | + elseif ( |
| 66 | + data.usage == "military" or |
| 67 | + data.usage == "tourism" |
| 68 | + ) then |
| 69 | + return |
| 70 | + -- Keep only most common gauges (and undefined) |
| 71 | + -- uses .find() as some gauges are specified like "1668;1435" |
| 72 | + elseif ( |
| 73 | + data.gauge ~= nil and |
| 74 | + data.gauge ~= 1000 and not string.find(data.gauge, "1000") and |
| 75 | + data.gauge ~= 1435 and not string.find(data.gauge, "1435") and |
| 76 | + data.gauge ~= 1520 and not string.find(data.gauge, "1520") and |
| 77 | + data.gauge ~= 1524 and not string.find(data.gauge, "1524") and |
| 78 | + data.gauge ~= 1600 and not string.find(data.gauge, "1600") and |
| 79 | + data.gauge ~= 1668 and not string.find(data.gauge, "1668") |
| 80 | + ) then |
| 81 | + return |
| 82 | + end |
| 83 | + |
| 84 | + local is_secondary = ( |
| 85 | + data.service == "siding" or |
| 86 | + data.service == "spur" or |
| 87 | + data.service == "yard" or |
| 88 | + data.usage == "industrial" |
| 89 | + ) |
| 90 | + |
| 91 | + -- by default, use 20km/h for secondary rails, else 140 |
| 92 | + local default_speed = ternary(is_secondary, profile.properties.secondary_speed, profile.properties.speed) |
| 93 | + -- but if OSM specifies a maxspeed, use the one from OSM |
| 94 | + local speed = default_speed |
| 95 | + if (not data.maxspeed and not data.maxspeed == '' and tonumber(data.maxspeed) < default_speed) then |
| 96 | + speed = data.maxspeed |
| 97 | + end |
| 98 | + |
| 99 | + |
| 100 | + -- fix speed for mph issue |
| 101 | + speed = tostring(speed) |
| 102 | + if speed:find(" mph") or speed:find("mph") then |
| 103 | + speed = speed:gsub(" mph", "") |
| 104 | + speed = speed:gsub("mph", "") |
| 105 | + speed = tonumber (speed) |
| 106 | + if speed == nil then speed = 20 end |
| 107 | + speed = speed * 1.609344 |
| 108 | + else |
| 109 | + speed = tonumber (speed) |
| 110 | + end |
| 111 | + -- fix speed for mph issue end |
| 112 | + |
| 113 | + |
| 114 | + result.forward_speed = speed |
| 115 | + result.backward_speed = speed |
| 116 | + -- |
| 117 | + result.forward_mode = mode.train |
| 118 | + result.backward_mode = mode.train |
| 119 | + -- |
| 120 | + result.forward_rate = 1 |
| 121 | + result.backward_rate = 1 |
| 122 | + |
| 123 | +end |
| 124 | + |
| 125 | +function process_turn(profile, turn) |
| 126 | + -- Refuse turns that have a big angle |
| 127 | + local angle = math.abs(turn.angle) |
| 128 | + if angle > profile.properties.max_angle then |
| 129 | + turn.duration = turn.duration + (angle - profile.properties.max_angle) * 3 |
| 130 | + end |
| 131 | + |
| 132 | + -- If we go backwards, add the penalty to change cabs |
| 133 | + if turn.is_u_turn then |
| 134 | + turn.duration = turn.duration + profile.properties.u_turn_penalty |
| 135 | + end |
| 136 | +end |
| 137 | + |
| 138 | +return { |
| 139 | + setup = setup, |
| 140 | + process_way = process_way, |
| 141 | + process_node = process_node, |
| 142 | + process_turn = process_turn |
| 143 | +} |
0 commit comments