Add turn cost factor calculation to itinero 2.0

This commit is contained in:
Pieter Vander Vennet 2022-06-01 14:15:38 +02:00
parent 99dbe5961c
commit 798b29c0e6
4 changed files with 75 additions and 1 deletions

View file

@ -75,6 +75,23 @@ namespace AspectedRouting.IO.itinero2
return code.Lined();
}
private string GenerateTurnCostFunction()
{
var vehicleTypes = _profile.VehicleTyps;
_skeleton.AddDep("containedIn");
_skeleton.AddDep("str_split");
_skeleton.AddDep("calculate_turn_cost_factor");
var code = new List<string> {
"--[[ Function called by itinero2 on every turn restriction relation"," ]]",
"function turn_cost_factor(attributes, result)",
" result.factor = calculate_turn_cost_factor(attributes, vehicle_types)" ,
"end",
"",
};
return code.Lined();
}
private string GenerateMainFunction()
{
var parameters = _profile.Behaviours[_behaviourName];
@ -85,6 +102,7 @@ namespace AspectedRouting.IO.itinero2
_skeleton.AddDep("eq");
_skeleton.AddDep("remove_relation_prefix");
_skeleton.AddDep("debug_table");
var code = new List<string>
{
"--[[",

View file

@ -4,6 +4,7 @@ using System.Linq;
using AspectedRouting.IO.itinero1;
using AspectedRouting.Language;
using AspectedRouting.Language.Expression;
using AspectedRouting.Language.Functions;
using AspectedRouting.Tests;
namespace AspectedRouting.IO.itinero2
@ -56,7 +57,10 @@ namespace AspectedRouting.IO.itinero2
var header =
new List<string> {
$"name = \"{_profile.Name}.{_behaviourName}\"",
$"description = \"{profileDescr} ({_profile.Description})\""
$"description = \"{profileDescr} ({_profile.Description})\"",
"",
"-- The hierarchy of types that this vehicle is; mostly used to check access restrictions",
$"vehicle_types = "+_skeleton.ToLua(new Constant( _profile.VehicleTyps.Select(v => new Constant(v)).ToArray()))
};
var testPrinter = new LuaTestPrinter(_skeleton,
@ -72,6 +76,8 @@ namespace AspectedRouting.IO.itinero2
"",
GenerateFactorFunction(),
"",
GenerateTurnCostFunction(),
"",
_parameterPrinter.GenerateDefaultParameters(),
"",
"",

View file

@ -0,0 +1,36 @@
--[[
Calculates the turn cost factor for relation attributes, returns '0' if the turn is allowed and '-1' if the turn is forbidden.
Dependencies: str_split, containedIn
]]
function calculate_turn_cost_factor(attributes, vehicle_types)
if (attributes["type"] ~= "restriction") then
-- not a turn restriction; no cost to turn,
return 0
end
for _, vehicle_type in pairs(vehicle_types) do
if (attributes["restriction:"..vehicle_type] ~= nil) then
-- There is a turn restriction specifically for one of our vehicle types!
return -1
end
end
if (attributes["restriction"] == nil) then
-- Not a turn restriction; no cost to turn
return 0
end
if (attributes["except"] ~= nil) then
local except_types = str_split(attributes["except"],";")
for _, vehicle_type in pairs(vehicle_types) do
if (containedIn(except_types, vehicle_type)) then
-- This vehicle is exempt from this turn restriction
return 0
end
end
end
return -1
end

View file

@ -0,0 +1,14 @@
--[[
Splits a string on the specified character, e.g.
str_split("abc;def;ghi", ";") will result in a table ["abc","def","ghi"]
]]
function str_split (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end