routx/
lib.rs

1// (c) Copyright 2025 MikoĊ‚aj Kuranowski
2// SPDX-License-Identifier: MIT
3
4//! Simple routing over [OpenStreetMap](https://www.openstreetmap.org/) data.
5//!
6//! Routx converts OSM data into a standard weighted directed graph representation,
7//! and runs A* to find shortest paths between nodes. Interpretation of OSM data
8//! is customizable via [profiles](crate::osm::Profile). Routx supports one-way streets,
9//! access tags (on ways only) and turn restrictions.
10//!
11//! # Example
12//!
13//! ```no_run
14//! let mut g = routx::Graph::new();
15//! let osm_options = routx::osm::Options {
16//!     profile: &routx::osm::CAR_PROFILE,
17//!     file_format: routx::osm::FileFormat::Unknown,
18//!     bbox: [0.0; 4],
19//! };
20//! routx::osm::add_features_from_file(
21//!     &mut g,
22//!     &osm_options,
23//!     "path/to/monaco.osm.pbf",
24//! ).expect("failed to load monaco.osm");
25//!
26//! let start_node = g.find_nearest_node(43.7384, 7.4246).unwrap();
27//! let end_node = g.find_nearest_node(43.7478, 7.4323).unwrap();
28//! let route = routx::find_route_without_turn_around(&g, start_node.id, end_node.id, routx::DEFAULT_STEP_LIMIT)
29//!     .expect("failed to find route");
30//!
31//! println!("Route: {:?}", route);
32//! ```
33
34mod astar;
35pub mod c;
36mod distance;
37mod graph;
38mod kd;
39pub mod osm;
40
41pub use astar::{find_route, find_route_without_turn_around, AStarError, DEFAULT_STEP_LIMIT};
42pub use distance::earth_distance;
43pub use graph::Graph;
44pub use kd::KDTree;
45
46/// Represents an element of the [Graph].
47///
48/// Due to turn restriction processing, one OpenStreetMap node
49/// may be represented by multiple Node instances. If that is the
50/// case, a "canonical" node (not bound by any turn restrictions) will
51/// have `id == osm_id`.
52///
53/// Nodes with `id == 0`, `osm_id == 0` or `osm_id >= 0x0008_0000_0000_0000`
54/// are disallowed. Zero IDs are used by the C bindings to signify absence of nodes,
55/// while large IDs are reserved for turn restriction processing.
56#[derive(Debug, Clone, Copy, PartialEq)]
57#[repr(C)]
58pub struct Node {
59    pub id: i64,
60    pub osm_id: i64,
61    pub lat: f32,
62    pub lon: f32,
63}
64
65impl Node {
66    pub const ZERO: Self = Self {
67        id: 0,
68        osm_id: 0,
69        lat: 0.0,
70        lon: 0.0,
71    };
72}
73
74/// Represents an outgoing (one-way) connection from a specific [Node].
75///
76/// `cost` must be greater than the crow-flies distance between the two nodes.
77///
78/// Due to implementation details, `to` might not exist in the [Graph].
79/// Users must silently ignore such edges.
80#[derive(Debug, Clone, Copy, PartialEq)]
81#[repr(C)]
82pub struct Edge {
83    pub to: i64,
84    pub cost: f32,
85}