Overview
The RouteLeg model represents a single segment of a route. A complete route consists of one or more legs in sequence.
Type Definition
interface RouteLeg {
mode : 'bus' | 'local' | 'walk' ;
submode ?: 'driving' | 'walking' ;
route_id ?: string ;
trip_id ?: string ;
from : string ;
to : string ;
departure ?: string ;
arrival ?: string ;
durationMin ?: number ;
distanceMeters ?: number ;
cost ?: number ;
source ?: 'graph' | 'distance_matrix' ;
}
Source: /home/daytona/workspace/source/MND-backend/src/core/types.ts:71-84
Fields
Primary transport mode for this leg. bus : University bus servicelocal : Local transport (CNG auto-rickshaw, rickshaw, or taxi)walk : Pedestrian walking
Specific mode within local transport (only when mode: 'local'). driving : CNG auto-rickshaw or taxi (motorized)walking : On foot (for short distances within local mode)
Bus route identifier (only when mode: 'bus'). Examples: bus1, bus2, bus3, etc.
Specific bus trip identifier (only when mode: 'bus'). Format: {route_id}_{departure_time} Examples: bus1_0825, bus3_1710
Origin node ID for this leg. Example: TILAGOR, CAMPUS, SUBIDBAZAR
Destination node ID for this leg. Example: CAMPUS, AMBARKHANA, MEDICAL
Departure time in HH:MM format (only for bus legs). Example: 08:30, 17:15
Arrival time in HH:MM format (only for bus legs). Example: 09:20, 17:45
Duration of this leg in minutes.
For buses: Time between departure and arrival
For local transport: Estimated travel time
For walking: Estimated walking time
Distance traveled in meters. Only available when Distance Matrix API is used. May be omitted for graph-based calculations.
Cost in local currency (Bangladeshi Taka).
Bus: 0 (free university service)
Local transport: 15-50 BDT
Walking: 0
Data source for this leg’s calculations. graph : Pre-computed data from graph edgesdistance_matrix : Real-time data from Google Distance Matrix API
Leg Types
Bus Leg
Represents a bus ride between stops.
{
"mode" : "bus" ,
"route_id" : "bus1" ,
"trip_id" : "bus1_0930" ,
"from" : "TILAGOR" ,
"to" : "CAMPUS" ,
"departure" : "09:30" ,
"arrival" : "10:20" ,
"durationMin" : 50 ,
"cost" : 0 ,
"source" : "graph"
}
Required Fields :
mode: 'bus'
route_id
trip_id
from
to
departure
arrival
durationMin
cost: 0
Local Transport Leg
Represents travel by CNG, rickshaw, or taxi.
{
"mode" : "local" ,
"submode" : "driving" ,
"from" : "AMBARKHANA" ,
"to" : "CHOWHATTA" ,
"durationMin" : 6 ,
"distanceMeters" : 1200 ,
"cost" : 25 ,
"source" : "graph"
}
Required Fields :
mode: 'local'
from
to
durationMin
cost
Optional Fields :
submode: Usually 'driving'
distanceMeters: If available from Distance Matrix
Walking Leg
Represents pedestrian walking.
{
"mode" : "walk" ,
"from" : "RIKABI_BAZAR" ,
"to" : "CHOWHATTA" ,
"durationMin" : 6 ,
"cost" : 0 ,
"source" : "graph"
}
Required Fields :
mode: 'walk'
from
to
durationMin
cost: 0
Field Relationships
Mode-Specific Fields
Field bus local walk route_id Required - - trip_id Required - - departure Required - - arrival Required - - submode - Optional - durationMin Required Required Required distanceMeters - Optional Optional cost 0 >0 0
Source Field
graph : Data from pre-computed graph edges
{
"mode" : "local" ,
"from" : "SUBIDBAZAR" ,
"to" : "RIKABI_BAZAR" ,
"durationMin" : 5 ,
"cost" : 20 ,
"source" : "graph"
}
distance_matrix : Real-time data from Google API
{
"mode" : "local" ,
"submode" : "driving" ,
"from" : "TILAGOR" ,
"to" : "MEDICAL" ,
"durationMin" : 15 ,
"distanceMeters" : 3500 ,
"cost" : 40 ,
"source" : "distance_matrix"
}
Examples
Single Bus Leg
Simplest route: one bus from start to end.
{
"legs" : [
{
"mode" : "bus" ,
"route_id" : "bus1" ,
"trip_id" : "bus1_0825" ,
"from" : "TILAGOR" ,
"to" : "CAMPUS" ,
"departure" : "08:25" ,
"arrival" : "09:15" ,
"durationMin" : 50 ,
"cost" : 0 ,
"source" : "graph"
}
]
}
Bus + Local Transport
Bus to intermediate point, then CNG to final destination.
{
"legs" : [
{
"mode" : "bus" ,
"route_id" : "bus1" ,
"trip_id" : "bus1_1710" ,
"from" : "SUBIDBAZAR" ,
"to" : "AMBARKHANA" ,
"departure" : "17:15" ,
"arrival" : "17:20" ,
"durationMin" : 5 ,
"cost" : 0 ,
"source" : "graph"
},
{
"mode" : "local" ,
"submode" : "driving" ,
"from" : "AMBARKHANA" ,
"to" : "CHOWHATTA" ,
"durationMin" : 6 ,
"distanceMeters" : 1200 ,
"cost" : 25 ,
"source" : "graph"
}
]
}
Local + Walking
CNG to nearby point, then walk to final destination.
{
"legs" : [
{
"mode" : "local" ,
"from" : "SUBIDBAZAR" ,
"to" : "RIKABI_BAZAR" ,
"durationMin" : 5 ,
"cost" : 20 ,
"source" : "graph"
},
{
"mode" : "walk" ,
"from" : "RIKABI_BAZAR" ,
"to" : "CHOWHATTA" ,
"durationMin" : 6 ,
"cost" : 0 ,
"source" : "graph"
}
]
}
Bus Transfer
Two buses with a transfer point.
{
"legs" : [
{
"mode" : "bus" ,
"route_id" : "bus1" ,
"trip_id" : "bus1_0825" ,
"from" : "TILAGOR" ,
"to" : "SUBIDBAZAR" ,
"departure" : "08:25" ,
"arrival" : "08:50" ,
"durationMin" : 25 ,
"cost" : 0 ,
"source" : "graph"
},
{
"mode" : "bus" ,
"route_id" : "bus3" ,
"trip_id" : "bus3_0925" ,
"from" : "SUBIDBAZAR" ,
"to" : "CAMPUS" ,
"departure" : "09:25" ,
"arrival" : "09:50" ,
"durationMin" : 25 ,
"cost" : 0 ,
"source" : "graph"
}
]
}
Note the 35-minute gap between legs (08:50 to 09:25) for the transfer.
Calculating Leg Duration
Bus Legs
durationMin = arrival - departure
// Example:
// departure: "08:30" (510 minutes from midnight)
// arrival: "09:20" (560 minutes from midnight)
// duration: 560 - 510 = 50 minutes
Local Transport Legs
From graph data:
// Pre-configured in edges.json
{
"from" : "SUBIDBAZAR" ,
"to" : "RIKABI_BAZAR" ,
"mode" : "local" ,
"time_min" : 5 ,
"cost" : 20
}
From Distance Matrix API:
// Real-time calculation
const response = await distanceMatrixAPI . getDistance (
fromAddress ,
toAddress ,
'driving'
);
durationMin = Math . ceil ( response . durationSeconds / 60 );
distanceMeters = response . distanceMeters ;
Walking Legs
Estimated at 5 km/h (83 meters/minute):
durationMin = Math . ceil ( distanceMeters / 83 );
// Example:
// 500 meters ÷ 83 = 6 minutes
Cost Structure
Bus Rides
All bus rides are free (university service):
{
"mode" : "bus" ,
"cost" : 0
}
Local Transport
Costs vary by distance (pre-configured in graph):
Distance Cost (BDT) < 1 km 15-20 1-2 km 20-25 2-4 km 25-35 > 4 km 35-50
{
"mode" : "local" ,
"distanceMeters" : 1200 ,
"cost" : 25
}
Walking
Always free:
{
"mode" : "walk" ,
"cost" : 0
}
Validation Rules
Required Field Validation
function validateLeg ( leg : RouteLeg ) : boolean {
// All legs must have these
if ( ! leg . mode || ! leg . from || ! leg . to ) {
return false ;
}
// Bus legs must have route info and timing
if ( leg . mode === 'bus' ) {
if ( ! leg . route_id || ! leg . trip_id ||
! leg . departure || ! leg . arrival ||
leg . cost !== 0 ) {
return false ;
}
}
// Cost must be non-negative
if ( leg . cost && leg . cost < 0 ) {
return false ;
}
return true ;
}
Logical Consistency
// Duration should match departure/arrival for buses
if ( leg . mode === 'bus' ) {
const expectedDuration =
timeToMinutes ( leg . arrival ) - timeToMinutes ( leg . departure );
assert ( leg . durationMin === expectedDuration );
}
// Walking and buses should be free
if ( leg . mode === 'walk' || leg . mode === 'bus' ) {
assert ( leg . cost === 0 );
}
// Local transport should have positive cost
if ( leg . mode === 'local' ) {
assert ( leg . cost > 0 );
}
Integration with RouteOption
Legs are always returned within a RouteOption:
{
"label" : "Fastest Route" ,
"totalTimeMin" : 56 ,
"totalCost" : 25 ,
"legs" : [
{ /* Bus leg */ },
{ /* Local leg */ }
]
}
The totals should match sum of legs:
const totalTime = legs . reduce (( sum , leg ) => sum + leg . durationMin , 0 );
const totalCost = legs . reduce (( sum , leg ) => sum + leg . cost , 0 );
RouteOption Complete route containing multiple legs
Node From/to location information
Bus Schedule Trip timing for bus legs
Route Planning API endpoint that returns legs