Python library & CLI to compute Hong Kong (HKeToll) tolls from official datasets
Project description
hktoll — Python Hong Kong (HKeToll) tolls
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
Install
python -m venv .venv && source .venv/bin/activate # optional
pip install -U pip
pip install hktoll
Requires Python 3.9+.
Quick start
CLI
# 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.geojson
Tip: run
hktoll --helpfor all commands and options.
Python API
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
REST API (language‑agnostic)
# 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"
}'
What it does
- 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).
Data sources (official)
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/
Inputs & outputs
- 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.
Examples
See the examples/ folder for ready‑to‑run samples.
Contributing
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.
Attribution & terms
- 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.
License
Keywords
python hong kong tolls, hong-kong, HKeToll, tolls, routing, gis, transport
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hktoll-0.1.1.tar.gz.
File metadata
- Download URL: hktoll-0.1.1.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ede1ee0b661db48426fd4df13d925890e44652bbc3e62f388d5d2e8f2593c74b
|
|
| MD5 |
b0476831988dab8568a982588b28329d
|
|
| BLAKE2b-256 |
ac6ff1db9b3811c6b51ad47bc942e282af2cdc102d108e3b77af28f3c973e8ab
|
File details
Details for the file hktoll-0.1.1-py3-none-any.whl.
File metadata
- Download URL: hktoll-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cae75938b8e3f95c3fcd9b53034af55017584e7debebd4d840a5933892c3cef0
|
|
| MD5 |
fa50e672e0d5ab77c51d1ddc8a3ac17b
|
|
| BLAKE2b-256 |
70f0f65c0e0ffb33682a133d2ad115b06f69437cf8c663e13085629049df8a6b
|