1.0.0 • Published 1 year ago

@maptrip/truck-tolls-germany v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Tolls for trucks in Germany

A TypeScript package to stay up to date on toll rates for trucks traveling routes within Germany. This package includes predefined variables (enums) which reflect the current rates from Toll Collect GmbH.

Stay up to date on the latest toll rates for trucks within Germany using this typescript package with predefined variables.

Table of Contents

Installation

Install the library from npm:

npm install @maptrip/truck-tolls-germany

Usage

To fetch the toll rate, you need values for the CO2 Class, Axle & Weight Class, and Euro Class of the truck. These values are provided using TypeScript enums and can be imported from the package.

Enums and their values:

enum CO2Class {
    One = 1,
    Two,
    Three,
    Four
}

enum AxleWeightClass {
    Over_3_5_Tons = "3,5-7,49",
    Over_7_5_Tons = "7,5-11,99",
    Over_12_Tons = "12-18",
    Over_18_Tons_3_Axles = "18&3",
    Over_18_Tons_4_Axles = "18&4",
    Over_18_Tons_5_Axles = "18&5",
}

enum EuroClass {
    Euro1AndZero = "euro1AndZero",
    Euro2 = "euro2",
    Euro3 = "euro3",
    Euro4 = "euro4",
    Euro5_EEV = "euro5_EEVClass1",
    Euro6 = "euro6",
}

Fetching Toll Rates

The TollRateProvider has one method getTollRate, which requires a value for each above enums. This method returns the toll rate in cents per kilometer (cent/km). You can use this rate in combination with the total kilometers of toll roads along a route to calculate the total toll cost.

import {
  AxleWeightClass,
  CO2Class,
  EuroClass,
  TollRateProvider,
} from "@maptrip/truck-tolls-germany";

const tollRateProvider = new TollRateProvider();

const truckOneTollRate = tollRateProvider.getTollRate(CO2Class.One, AxleWeightClass.Over_18_Tons_3_Axles, EuroClass.Euro6)

// returns number 30.3
// multiply this with your total toll roads length to get the total toll cost

Example with MapTrip Server API

To get the total kilometers of toll roads along a route, use the MapTrip API /route endpoint. The value is returned in meters.

As of the first published version of this package, 1.0.0, the provider parameter for the /route endpoint must be either provider=HERE or provider=TomTom. OpenStreetMap does not provide toll information for routes within Germany. OSM German toll data is planned for integration into the MapTrip Server API for a future release.

function getTollsFromRoute(start, destination, token){
  //Use HERE or TomTom for the provider to get tolls in Germany. 
    const url = `https://api.maptrip.de/v1/route?provider=HERE&from=${encodeURIComponent(
      start
    )}&to=${encodeURIComponent(start)}&vehicle=truck`;

    try {
        const response = await fetch(url, {
            headers: {
                Accept: "application/json",
                Authorization: token  //MapTrip token
            },
        });
        const data = await response.json()

        // Return the toll data in km 
        const tollRoadLengthInKm = (data[0].summary.toll / 1000).toFixed()
        return tollRoadLengthInKm
    } catch (error) {
      console.error("Error fetching data", error);
      throw error;
    }
}

async function calculateTotalTollCosts(cO2Class, axleWeightClass, euroClass, start, destination, token){
  try {
    const tollsInKm = await getTollsFromRoute(start, destination, token);
    const tollRateProvider = new TollRateProvider();
    const tollRate = tollRateProvider.getTollRate(cO2Class, axleWeightClass, euroClass);

    // divide by 100 to get a total in euros rather than cents
    const totalInEuro = ((tollsInKm * tollRate) / 100).toFixed();
  } catch (error) {
    console.error("Error calculating total toll costs", error);
    throw error;
  }

}

Questions

Any questions or comments, please direct a request to support@infoware.de

History

  • 1.0.0 Package released. Toll information up to date as of July 1, 2024.
1.0.0

1 year ago