itinero / routing

The routing core of itinero.
Apache License 2.0
220 stars 70 forks source link

Change maxheight of the vehicle in runtime for lua profile #343

Open slemvs opened 2 years ago

slemvs commented 2 years ago

Hi @xivk and all itinero team, thank you for great and very usefull library.

In my application user can add to own profile several vehicles, for example, several small trucks with different heights: 3m and 4m. Can we use for this case one lua profile, save contracted db with this lua profile and use it later with 2 different vehicles with different heights?

Can we set height of the vehicle in runtime (not hardcoded constant in lua, but any variable that we could check)? Is there any "constraints" of the vehicle from C# code available in lua?

TomQv commented 2 years ago

Good question, would also like to know

slemvs commented 2 years ago

If anybody interesting in this, I can set custom parameter of the car lua profile in runtime this way:

define custom parameter "currentHeight" in lua profile like this

-- car globals
name = "car"
vehicle_types = { "vehicle", "motor_vehicle", "motorcar" }
constraints =  { "maxweight", "maxwidth", "maxheight" }

parameters = {
    currentHeight = "2"
}

minspeed = 30
maxspeed = 200
...

and then use this parameter in C# code like this:

var vehicle = DynamicVehicle.Load(File.ReadAllText(@"k:\_osm\profiles\custom-car.lua"));
var parameters = vehicle.Script.Globals.Pairs.FirstOrDefault(x => x.Key.String == "parameters");
parameters.Value.Table["currentHeight"] = req.VehicleHeight.ToString(CultureInfo.InvariantCulture);
var targetProfile = vehicle.Fastest();

In runtime i see that new value for this parameter was set, but i dont know how to avoid edge in factor_and_speed function using this custom vehicle height.

TomQv commented 2 years ago

I also don't know. From what I remember, @xivk said a while (some years) ago, with contracted routerdbs its not possible to change anything at runtime. For example, I would also need an "avoid highways" option, selectable at runtime.

slemvs commented 2 years ago

In lua profile, in factor_and_speed function i can get dynamic value of the parameter:

         local canAccess = true;
     if attributes.maxheight then
        local height = itinero.parsespeed(attributes.maxheight)
        local height2 = itinero.parsespeed(parameters.currentHeight)
        itinero.log('height: ' .. tostring(height) .. ' - ' .. tostring(height2))
        if height < height2 then
            canAccess = false;
        end
     end
     if canAccess == false then
        result.speed = 0
        result.direction = 0
        result.canstop = true
        return
     end

in console i can see new value of the currentHeight (height2) parameter, but router builds route ignoring this code. In app i set custom vehicle height to 6.5m, and then, when i build route with router i can see this data in console:

[RouterBaseExtensions] information -  Profile(s) car not cached, building cache.
[Lua] information -  height: 3.5 - 6.5
[Lua] information -  height: 3.5 - 6.5
[Lua] information -  height: 2.5 - 6.5
[Lua] information -  height: 3 - 6.5
[Lua] information -  height: 4 - 6.5
[Lua] information -  height: 2 - 6.5
[Lua] information -  height: 4.800000190734863 - 6.5
[Lua] information -  height: 3.299999952316284 - 6.5
[Lua] information -  height: 3.5 - 6.5
[Lua] information -  height: 2.0999999046325684 - 6.5
[Lua] information -  height: 3.5 - 6.5
[Lua] information -  height: 3.5 - 6.5

these logs from line in lua profile

        itinero.log('height: ' .. tostring(height) .. ' - ' .. tostring(height2))

routerdb file was created with custom lua profile in this case