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.1.tar.gz (30.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.1-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.1.tar.gz.

File metadata

  • Download URL: pynigiri-0.1.1.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pynigiri-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5b3bd44cee72c539042ef032f73d81ef9958f3320b7c7d1ef938492960af02ae
MD5 3c4e3109914815e6d3ba0e39388a35ec
BLAKE2b-256 312bd62cc6f3b66e7fa20c92409e9387826ffb0b8e3051ef19cec59d30cdfbf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pynigiri-0.1.1.tar.gz:

Publisher: wheels.yml on 96hoshi/nigiri

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

File details

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

File metadata

File hashes

Hashes for pynigiri-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a1ba324f328e765e735043551b853e414437c45bb00d07d4579188089622e006
MD5 290dff8aee1dfa80a35b432dba53c8f2
BLAKE2b-256 dfd6e3420c0ba122e7539fc61459c24c55f0330629ae37421dedc8559eddba47

See more details on using hashes here.

Provenance

The following attestation bundles were made for pynigiri-0.1.1-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