Skip to main content

Python bindings for the nigiri transit routing library

Project description

PyNigiri - Python Bindings for Nigiri Transit Routing Library

Complete Python bindings for the nigiri C++ transit routing library.

Status

Production Ready - All core functionality is available and tested.

  • ✅ 23 unit tests passing
  • ✅ Clean integer-based time API
  • ✅ Routing verified working with test GTFS data

Features

  • Data Loading: Load GTFS, GTFS-RT, HRD, and NeTEx transit data
  • Routing: Fast RAPTOR-based public transit routing
  • Real-time Updates: Apply GTFS-RT trip updates and alerts
  • All Transport Types: Support for all public transit modes (bus, train, tram, ferry, etc.)

Building

The bindings are built as part of the main nigiri build system:

cd nigiri
cmake -B build -DPYTHON_BINDING=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON
cmake --build build --target pynigiri -j8

The compiled module will be at: build/python/pynigiri.cpython-*.so

Usage

import sys
sys.path.insert(0, 'build/python')
import pynigiri as ng
from datetime import datetime, date

# Load GTFS data (use current year for your data)
current_year = date.today().year
sources = [ng.TimetableSource("gtfs", "/path/to/gtfs")]
timetable = ng.load_timetable(sources, f"{current_year}-01-01", f"{current_year}-12-31")

# Find locations
start_loc = timetable.find_location("STATION_A_ID")
dest_loc = timetable.find_location("STATION_B_ID")

# Create routing query
query = ng.Query()

# Convert datetime to minutes since epoch
query_time = datetime(current_year, 1, 15, 10, 0, 0)
query.start_time = int(query_time.timestamp()) // 60

# Set start/destination with integer offsets (0 minutes offset)
query.start = [ng.Offset(start_loc, 0, 0)]
query.destination = [ng.Offset(dest_loc, 0, 0)]
query.max_transfers = 6
query.max_travel_time = 600  # 10 hours in minutes
query.start_match_mode = ng.LocationMatchMode.EQUIVALENT
query.dest_match_mode = ng.LocationMatchMode.EQUIVALENT

# Run routing
journeys = ng.route(timetable, query)

# Process results
for journey in journeys:
    print(f"Transfers: {journey.transfers}")
    print(f"Travel time: {journey.travel_time()} minutes")
    for leg in journey.legs:
        # Use getattr for 'from' (Python keyword)
        from_loc = getattr(leg, 'from')
        from_name = timetable.get_location_name(from_loc)
        to_name = timetable.get_location_name(leg.to)
        
        # Convert minute timestamps to datetime for display
        dep_time = datetime.fromtimestamp(leg.dep_time * 60)
        arr_time = datetime.fromtimestamp(leg.arr_time * 60)
        print(f"  {from_name} -> {to_name}")
        print(f"    {dep_time.strftime('%H:%M')} -> {arr_time.strftime('%H:%M')}")

Available Types

Enums

  • Clasz: Transport class (REGIONAL, LONG_DISTANCE, SUBWAY, TRAM, BUS, etc.)
  • LocationType: Location types (STATION, TRACK, GENERATED_TRACK)
  • EventType: Event types (DEP, ARR)
  • Direction: Search direction (FORWARD, BACKWARD)
  • LocationMatchMode: Location matching modes for routing
    • EXACT: Match only the exact platform/stop specified
    • EQUIVALENT: Match all equivalent stops at a station (recommended for most routing)
    • ONLY_CHILDREN: Match all child stops of a parent station

Core Types

  • Timetable: Main timetable data structure
  • Query: Routing query configuration
  • Journey: Routing result with legs
  • LoaderConfig: Configuration for data loading
  • RtTimetable: Real-time timetable

Functions

  • load_timetable(): Load transit data
  • route(): Perform routing query
  • gtfsrt_update_from_bytes(): Apply GTFS-RT updates from bytes
  • gtfsrt_update_from_string(): Apply GTFS-RT updates from string
  • gtfsrt_update_from_file(): Apply GTFS-RT updates from file

Testing

Run the test suite to verify the bindings work correctly:

cd nigiri/python
pytest tests/

Expected output:

======================== 23 passed ========================

All tests should pass without any warnings or issues.

Installation via pip

Install the package using pip:

cd python
pip install .

For development (editable install):

pip install -e .

Implementation Notes

  • Built with pybind11 v2.11.1
  • Requires C++23 compiler (GCC 13+ or Clang 16+)
  • Uses CMake for build configuration
  • All static libraries compiled with -fPIC for shared library compatibility
  • Strong type wrappers for type safety (LocationIdx, TransportIdx, etc.)

Files

  • src/main.cc: Module entry point
  • src/types.cc: Core types and enums
  • src/timetable.cc: Timetable access
  • src/loader.cc: Data loading
  • src/routing.cc: Routing algorithms
  • src/rt.cc: Real-time updates
  • pybind_common.h: Common headers

License

Same as nigiri - MIT License

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

pynigiri-0.1.0.tar.gz (49.3 kB view details)

Uploaded Source

Built Distribution

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

pynigiri-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file pynigiri-0.1.0.tar.gz.

File metadata

  • Download URL: pynigiri-0.1.0.tar.gz
  • Upload date:
  • Size: 49.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for pynigiri-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d7b2d770e9a5cea9e7752e0ce07f73c583c698e6129010803e2fcb2a734e3fdb
MD5 11e87b74b025fd0f5d813c01daa24849
BLAKE2b-256 19aca98d44d261c299b79a58c1ff86eed7580bd8ce022a780472da4c32de4489

See more details on using hashes here.

File details

Details for the file pynigiri-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pynigiri-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c4928784506b1150e3ee695fcbb74b8f1db873effbf14a4e99babcb7ccdb46d9
MD5 a94068d321875477ef3341a1230242b7
BLAKE2b-256 66284dabb3e142c8e52e586120fbb9d06df6a5e046eb0160c5846935ea0aeef7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pynigiri-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on 96hoshi/nigiri

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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