Traffic

@tomtom-org/maps-sdk/core exports the traffic types that the services, map modules and routing share, so an incident looks the same whichever API returns it.

Where traffic types are used

Context Type Description
Incident Details service TrafficIncident / TrafficIncidentDetails The parsed incidents of a trafficIncidentDetails call
Traffic Incidents map module TrafficIncidentsModuleFeature (map) A live incident tile feature, extending TrafficIncidentBaseProperties
Route sections TrafficSectionProps A stretch of route affected by a traffic incident
Traffic Area Analytics service TrafficAreaAnalytics, AreaAnalyticsMetricKey A region's historical traffic report and its metric names (areaAnalyticsMetricKeys)

Shared types

TrafficIncidentCategory

The kind of incident, used by the Incident Details service, the incident tiles and route sections:

import type { TrafficIncidentCategory } from '@tomtom-org/maps-sdk/core';

// 'accident' | 'animals-on-road' | 'broken-down-vehicle' | 'danger' |
// 'flooding' | 'fog' | 'frost' | 'jam' | 'lane-closed' | 'narrow-lanes' |
// 'other' | 'rain' | 'road-closed' | 'roadworks' | 'wind'
const category: TrafficIncidentCategory = incident.properties.category;
import { fullTrafficIncidentCategories } from '@tomtom-org/maps-sdk/core';

// ['accident', 'broken-down-vehicle', 'danger', 'flooding', ..., 'animals-on-road', 'narrow-lanes']
console.log(fullTrafficIncidentCategories);

DelayMagnitude

The severity of a delay, in TrafficIncidentProperties.magnitudeOfDelay, TrafficSectionProps.magnitudeOfDelay and the values of the incidents module's magnitudes filter:

import type { DelayMagnitude } from '@tomtom-org/maps-sdk/core';

// 'unknown' | 'minor' | 'moderate' | 'major' | 'indefinite'
const severity: DelayMagnitude = incident.properties.magnitudeOfDelay;
Value Meaning
unknown Severity cannot be determined
minor Small delay (a few minutes)
moderate Noticeable delay (several minutes)
major Significant delay (10+ minutes)
indefinite Unknown or extremely long delay (e.g. road closure)

indexedMagnitudes lists every magnitude in the order above, which matches the Traffic API's numeric magnitudeOfDelay codes. For example, index 2 is moderate. The SDK's results already carry the names. Use it to build a magnitude filter or check whether all are selected:

import { indexedMagnitudes } from '@tomtom-org/maps-sdk/core';

const allSelected = selectedMagnitudes.length === indexedMagnitudes.length;

TrafficIncidentTimeValidity

Whether an incident is active now or planned, used by the Incident Details service and the incident tiles:

import type { TrafficIncidentTimeValidity } from '@tomtom-org/maps-sdk/core';

// 'present' | 'future'
const validity: TrafficIncidentTimeValidity = incident.properties.timeValidity;

trafficIncidentTimeValidities lists both, present first, for a picker over the service's timeValidityFilter or the incidents module's timeValidity.

Incident types

TrafficIncidentBaseProperties

The nine properties the Incident Details response and the incident tile features share:

import type { TrafficIncidentBaseProperties } from '@tomtom-org/maps-sdk/core';

type TrafficIncidentBaseProperties = {
    id: string;
    category: TrafficIncidentCategory;
    magnitudeOfDelay: DelayMagnitude;
    delayInSeconds?: number;
    startTime?: Date;
    endTime?: Date;
    probabilityOfOccurrence?: TrafficIncidentProbability; // 'certain' | 'probable' | 'risk_of' | 'improbable'
    numberOfReports?: number;
    lastReportTime?: Date;
};

TrafficIncidentProperties

TrafficIncidentBaseProperties plus what only the Incident Details response carries:

import type { TrafficIncidentProperties } from '@tomtom-org/maps-sdk/core';
Field Type Description
events TrafficIncidentEvent[] One or more event descriptions, each with its description, code and category
from string? Start of the affected road stretch
to string? End of the affected road stretch
lengthInMeters number? Length of the affected road section
roadNumbers string[]? Road numbers (e.g. ['A10'])
timeValidity TrafficIncidentTimeValidity Always present in the service response

TrafficIncident and TrafficIncidentDetails

import type { TrafficIncident, TrafficIncidentDetails } from '@tomtom-org/maps-sdk/core';

// TrafficIncident — a GeoJSON Feature<Point | LineString, TrafficIncidentProperties>
// TrafficIncidentDetails — a GeoJSON FeatureCollection of them, what trafficIncidentDetails returns

Routing traffic types

TrafficSectionProps

A route's traffic sections share TrafficIncidentCategory and DelayMagnitude with the incident types above:

import type { TrafficSectionProps } from '@tomtom-org/maps-sdk/core';

const trafficSections: TrafficSectionProps[] = route.properties.sections.traffic ?? [];

for (const section of trafficSections) {
    console.log(section.categories);          // e.g. ['jam']
    console.log(section.magnitudeOfDelay);    // e.g. 'moderate'
    console.log(section.delayInSeconds);      // e.g. 420
    console.log(section.effectiveSpeedInKmh); // e.g. 18
}

Related