hktoll is a Python library & CLI to compute Hong Kong (HKeToll) tolls from official Transport Department datasets. Give it a route (lon/lat pairs or GeoJSON) and a timestamp; get back ordered toll events and totals (HKD).
適用於香港(HKeToll)收費計算的 Python 程式庫。
- Input: route polyline
[(lon, lat), ...]or GeoJSONLineString/FeatureCollection - Output: ordered toll events (facility, time band, amount) + totals (HKD)
- Interfaces: Python API, CLI, and a tiny REST server
python -m venv .venv && source .venv/bin/activate # optional
pip install -U pip
pip install hktollRequires Python 3.9+.
# Route via coordinates (lon,lat;lon,lat;...)
hktoll route \
--coords "114.1582,22.2799;114.1640,22.2801;114.1721,22.2975" \
--vehicle private_car \
--when "2025-09-17T08:30:00+08:00" \
-o out.json
# Annotate a GeoJSON FeatureCollection of LineStrings in-place
hktoll annotate-geojson examples/sample_route.geojson -o annotated.geojsonTip: run
hktoll --helpfor all commands and options.
from datetime import datetime
from hktoll import compute_tolls, annotate_geojson_with_tolls, TollEvent
route = [(114.1582, 22.2799), (114.1640, 22.2801), (114.1721, 22.2975)]
events, total = compute_tolls(route, vehicle="private_car", when=datetime.now())
print(total, [e.dict() for e in events]) # total in HKD# Start the server
hktoll serve --host 0.0.0.0 --port 8000
# Call it
curl -X POST http://localhost:8000/v1/tolls/route \
-H "content-type: application/json" \
-d '{
"coords": [[114.1582,22.2799],[114.1640,22.2801],[114.1721,22.2975]],
"vehicle": "private_car",
"when": "2025-09-17T08:30:00+08:00"
}'- Computes toll events and totals for Hong Kong tunnels/bridges, including time‑varying toll bands at harbour crossings.
- Accepts routes as lon/lat pairs or GeoJSON; returns structured
TollEventobjects. - Ships a CLI and a tiny HTTP server for language‑agnostic use.
- Uses an adapter layout so other regions can be added later (e.g., ERP, AutoPASS, Salik).
hktoll consumes Transport Department resources from the Road Network (2nd Generation) dataset and related materials:
-
Toll rates of tunnel and bridge (flat) —
TUN_BRIDGE_TOLL.csvhttps://data.gov.hk/en-data/dataset/hk-td-tis_15-road-network-v2/resource/3673f74a-ab49-4f5c-9df2-2b79569d5500 -
Toll rates of tunnel and bridge (time‑varying) —
TUN_BRIDGE_TV_TOLL.csvhttps://data.gov.hk/en-data/dataset/hk-td-tis_15-road-network-v2/resource/9d127737-e1e4-4081-8b58-f749bb0fe3b7 -
Zebra crossing, yellow box, toll plaza and cul‑de‑sac —
TRAFFIC_FEATURES.kmzhttps://data.gov.hk/en-data/dataset/hk-td-tis_15-road-network-v2/resource/57c24df1-722d-47d7-a458-553548938f41
The library downloads and caches these files automatically. See
src/hktoll/resources/urls.jsonfor exact endpoints anddocs/GETTING_STARTED.mdfor details.
For background on HKeToll (the free‑flow tolling system): https://www.hketoll.gov.hk/
- Route: list of
(lon, lat)pairs or GeoJSONLineString/FeatureCollection. - Vehicle: string identifier (e.g.,
private_car). Runhktoll route --helpto see the available values. - When: timestamp (ISO 8601, timezone aware) used to apply correct time‑varying toll band.
Returns:
- TollEvents: list of
TollEvent - Total: Decimal value of total tolls
Each TollEvent includes at minimum: facility, time_band (if applicable), amount_hkd, and timestamp.
See the examples/ folder for ready‑to‑run samples.
PRs and issues welcome! Please read the CODE_OF_CONDUCT.md and CONTRIBUTING.md. If you use hktoll in a paper or product, consider citing it via CITATION.cff.
- Data © Transport Department, HKSAR Government; sourced via DATA.GOV.HK.
- Reuse of data is subject to DATA.GOV.HK Terms and Conditions.
- hktoll is an independent open‑source project and is not affiliated with the Government.
python hong kong tolls, hong-kong, HKeToll, tolls, routing, gis, transport