Skip to main content

Generated Python code for the Valhalla protobuf API

Project description

Valhalla Proto

Generated protocol buffers code for the Valhalla routing engine.

A small and handy Python package that contains generated code from Valhalla's protobuf definitions that can be used to communicate with either the HTTP server or the Actor directly via Valhalla's Python bindings.

Installation

Installation is as easy as:

pip install valhalla-proto

Note that this package uses python-betterproto2, which gives a much smoother developer Experience than Google's protobuf compiler.

Example

from typing import List, Tuple

import requests

from valhalla_proto.valhalla import (
    Api,
    DirectionsType,
    LatLng,
    OptionsAction,
    Options,
    CostingType,
    OptionsFormat,
    Location,
    CostingOptions,
    Costing,
)
from valhalla_proto._upstream import VALHALLA_COMMIT


def make_options(
    coords: List[Tuple[float, float]],
    action: OptionsAction = OptionsAction.route,
    costing_type: CostingType = CostingType.auto_,
    format: OptionsFormat = OptionsFormat.pbf,
) -> Options:
    costing = Costing(options=CostingOptions())

    locs: List[Location] = []
    for lon, lat in coords:
        locs.append(Location(ll=LatLng(lat=lat, lng=lon)))

    return Options(
        action=action,
        costing_type=costing_type,
        format=format,
        locations=locs,
        costings={costing_type: costing},
        directions_type=DirectionsType.maneuvers,
    )


def process_directions(api: Api):
    if not api.directions:
        print("Error: no directions found")
        return

    if not api.directions.routes:
        print("Error: no route found")
        return

    if not api.directions.routes[0].legs:
        print("Error: no leg found")
        return

    leg = api.directions.routes[0].legs[0]

    if leg.shape:
        print(f"Route shape: {leg.shape}")


def main():
    print(f"Using protobuf code generated from Valhalla commit {VALHALLA_COMMIT}")

    coords = [
        (11.119188, 52.862036),
        (11.118704, 52.862658),
    ]

    options = make_options(
        action=OptionsAction.route,
        coords=coords,
    )

    api_request = Api(options=options)

    resp = requests.post(
        "https://valhalla1.openstreetmap.de/trace_attributes",
        data=api_request.SerializeToString(),
        headers={"Content-Type": "application/x-protobuf"},
    )
    api_response = Api.FromString(resp.content)

    process_directions(api_response)


if __name__ == "__main__":
    main()

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

valhalla_proto-3.7.0.tar.gz (46.2 kB view details)

Uploaded Source

Built Distribution

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

valhalla_proto-3.7.0-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file valhalla_proto-3.7.0.tar.gz.

File metadata

  • Download URL: valhalla_proto-3.7.0.tar.gz
  • Upload date:
  • Size: 46.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for valhalla_proto-3.7.0.tar.gz
Algorithm Hash digest
SHA256 053e61fedae041d7723371984a70ba5cd8a1e76b2d0e6ec3185d393fa2129f6d
MD5 197d602dc9685ab31c66f156f01a9fab
BLAKE2b-256 eaa60071c88e53278d2ea628e6f95ee95550a96729a43a69fa89a69f4760598c

See more details on using hashes here.

File details

Details for the file valhalla_proto-3.7.0-py3-none-any.whl.

File metadata

  • Download URL: valhalla_proto-3.7.0-py3-none-any.whl
  • Upload date:
  • Size: 27.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for valhalla_proto-3.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5eb38b5b5479f33758a70aa6e29fc293a5ce515076097f2d66a24a9581abcf7e
MD5 e5b1cc4451c6fc9cb792f727ab93fe44
BLAKE2b-256 31fb72ed4477c9bc680bb46c8ef58110a60ed6242e96d6925e846a384d23bd0d

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