Skip to main content

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 GeoJSON LineString / 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 --help for 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 TollEvent objects.
  • 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:

The library downloads and caches these files automatically. See src/hktoll/resources/urls.json for exact endpoints and docs/GETTING_STARTED.md for details.

For background on HKeToll (the free‑flow tolling system): https://www.hketoll.gov.hk/


Inputs & outputs

  • Route: list of (lon, lat) pairs or GeoJSON LineString / FeatureCollection.
  • Vehicle: string identifier (e.g., private_car). Run hktoll route --help to 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

MIT


Keywords

python hong kong tolls, hong-kong, HKeToll, tolls, routing, gis, transport


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hktoll-0.1.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hktoll-0.1.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

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

Hashes for hktoll-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ede1ee0b661db48426fd4df13d925890e44652bbc3e62f388d5d2e8f2593c74b
MD5 b0476831988dab8568a982588b28329d
BLAKE2b-256 ac6ff1db9b3811c6b51ad47bc942e282af2cdc102d108e3b77af28f3c973e8ab

See more details on using hashes here.

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

Hashes for hktoll-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cae75938b8e3f95c3fcd9b53034af55017584e7debebd4d840a5933892c3cef0
MD5 fa50e672e0d5ab77c51d1ddc8a3ac17b
BLAKE2b-256 70f0f65c0e0ffb33682a133d2ad115b06f69437cf8c663e13085629049df8a6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page