Show / Hide Table of Contents

Lua

The default way to define a vehicle profile is by using Lua. You can control pretty much everything about a vehicle.

An example

A vehicle can be defined by one lua script. This vehicle definition gets embedded in a RouterDb when created. This is the minimum code needed to define a profile:

name = "car"
-- whitelists for profile and meta
profile_whitelist = {
    "highway"
}
meta_whitelist = {
    "name"
}
-- profile definitions linking a function to a profile
profiles = {
    {
        name = "",
        function_name = "factor_and_speed",
        metric = "time"
    },
    { 
        name = "shortest",
        function_name = "factor_and_speed",
        metric = "distance",
    }
}
-- the main function turning attributes into a factor_and_speed and a tag whitelist
function factor_and_speed (attributes, result)

     result.speed = 0
     result.direction = 0
     result.canstop = true
     result.attributes_to_keep = {}

     -- get default speed profiles
     local highway = attributes.highway
     if highway == "motorway" or 
        highway == "motorway_link" then
        result.speed = 100 -- speed in km/h
        result.direction = 0
        result.canstop = true
        result.attributes_to_keep.highway = highway
    end
end

The main factor_and_speed is function is the most important active part of the vehicle definition but these variables/functions need to be defined in any vehicle definition:

  • name: The name of the vehicle, should be unique for all loaded profiles.
  • profile_whitelist: A list of all attributes that will be added to an edge as part of the profile.
  • meta_whitelist: A list of all attributes will be added to an edge as meta data.
  • profiles: Defines the profiles for this vehicle, by default at minimum fastest (with no name) and shortest have to be defined.
  • factor_and_speed: The core of the vehicle definition, converts edge attributes into a factor and speed.

Profiles

A profile describes the behaviour of a vehicle and is defined by a metric, a function to calculate speed/factor and a name:

  • metric: This can be time, distance or completely custom.
  • function_name: The name of the function to calculate factor and/or speed.
  • name: The name of the profile (an empty string automatically means 'fastest', the default).

Factor and speed functions

A factor and speed function converts a set of attributes into a speed and/or factor. Usually the default factor_and_speed will define a speed for each possible set of profile attributes. Based on this Itinero can define a fastest and a shortest profile with metrics being time and distance respectively. A custom profile uses a factor to define weights of edges. For shortest this factor is constant, usually equal to 1.

To summarize there are three options when using a factor_and_speed function:

  • With metric distance: Itinero will use the function to get the speed and set factor to a constant, usually 1.
  • With metric time: Itinero will use the function to get the speed and set factor to 1/speed.
  • With metric custom: Itinero will use the function to get the speed and the factor.

Default profiles

There are some vehicle definitions included in Itinero by default assuming OSM data is used. A few examples here:

  • The car vehicle: Defines a default factor_and_speed function but also a custom function to define a car profile called classifications to prefer higher classified roads even more than the regular profile.
  • The bicycle vehicle: Also definas a default factor_and_speed function but in addition it defines two more:
    • balanced: Aggressively chooses bicycle-only parts of the network.
    • networks: Aggressifely chooses bicycle routing networks.

Build a routerdb with embedded vehicles

By default all vehicles that are used to build a routerdb are embedded into it. When you have your own custom lua profile you can build a routerdb as follows:

var vehicle = DynamicVehicle.LoadFromStream(File.OpenRead("path/to/custom.lua"));
var routerDb = new RouterDb();
// load the raw osm data.
using (var stream = new FileInfo(@"/path/to/some/osmfile.osm.pbf").OpenRead())
{
    routerDb.LoadOsmData(stream, vehicle);
}
// write the routerdb to disk.
using (var stream = new FileInfo(@"/path/to/some/osmfile.routerdb").OpenWrite())
{
    routerDb.Serialize(stream);
}

Now the vehicles are embedded in the routerdb, you can get them by their name, in this case assumed to be custom:

var routerDb = RouterDb.Deserialize(File.OpenRead(@"/path/to/some/osmfile.routerdb"));
var vehicle = routerDb.GetSupportedVehicle("custom");
var router = new Router(routerDb);

var route = router.Calculate(vehicle.Fastest(), ...);
Back to top Built by Itinero, MIT licensed.