graphways 0.3.0

Fast OpenStreetMap reachability, routing, and isochrones from Python, powered by Rust — no routing server required.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::simplify::simplify_graph;
use crate::utils::{calculate_distance, calculate_travel_time};
use petgraph::graph::{DiGraph, NodeIndex};
use rstar::{PointDistance, RTree, RTreeObject, AABB};
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Debug, Deserialize)]
pub struct XmlData {
    #[serde(rename = "node", default)]
    pub nodes: Vec<XmlNode>,
    #[serde(rename = "way", default)]
    pub ways: Vec<XmlWay>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct XmlNode {
    #[serde(rename = "@id")]
    pub id: i64,
    #[serde(rename = "@lat")]
    pub lat: f64,
    #[serde(rename = "@lon")]
    pub lon: f64,
    #[serde(rename = "tag", default)]
    pub tags: Vec<XmlTag>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct XmlWay {
    #[serde(rename = "@id")]
    pub id: i64,
    #[serde(rename = "nd", default)]
    pub nodes: Vec<XmlNodeRef>,
    #[serde(rename = "tag", default)]
    pub tags: Vec<XmlTag>,
    #[serde(default)]
    pub length: f64,
    #[serde(default)]
    pub speed_kph: f64,
    #[serde(default)]
    pub walk_travel_time: f64,
    #[serde(default)]
    pub bike_travel_time: f64,
    #[serde(default)]
    pub drive_travel_time: f64,
}

impl XmlWay {
    pub fn filter_useful_tags(mut self) -> Self {
        const USEFUL_TAGS: &[&str] = &["highway", "name", "ref", "bridge", "tunnel", "service"];
        // Linear search on 15-element static slice — no HashSet allocation needed.
        self.tags
            .retain(|tag| USEFUL_TAGS.iter().any(|&k| k == tag.key.as_str()));
        self
    }

    /// Return the travel time (seconds) for the given network type.
    /// Centralises the walk / bike / drive dispatch so call sites don't
    /// repeat the same match expression.
    #[inline]
    pub fn travel_time(&self, network_type: crate::overpass::NetworkType) -> f64 {
        match network_type {
            crate::overpass::NetworkType::Walk => self.walk_travel_time,
            crate::overpass::NetworkType::Bike => self.bike_travel_time,
            _ => self.drive_travel_time,
        }
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct XmlNodeRef {
    #[serde(rename = "@ref")]
    pub node_id: i64,
}

#[derive(Debug, Deserialize, Clone)]
pub struct XmlTag {
    #[serde(rename = "@k")]
    pub key: String,
    #[serde(rename = "@v")]
    pub value: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
    Bidirectional,
    OneWayForward,
    OneWayReverse,
}

// Function to parse the XML response
pub fn parse_xml(xml_data: &str) -> Result<XmlData, quick_xml::DeError> {
    let root: XmlData = quick_xml::de::from_str(xml_data)?;
    Ok(root)
}

fn find_tag<'a>(tags: &'a [XmlTag], key: &str) -> Option<&'a XmlTag> {
    tags.iter().find(|tag| tag.key == key)
}

fn assess_path_directionality(path: &XmlWay) -> Direction {
    let oneway_tag = find_tag(&path.tags, "oneway");
    let junction_tag = find_tag(&path.tags, "junction");

    if oneway_tag.map_or(false, |tag| matches!(tag.value.as_str(), "-1" | "reverse")) {
        return Direction::OneWayReverse;
    }

    if oneway_tag.map_or(false, |tag| {
        matches!(tag.value.as_str(), "yes" | "true" | "1")
    }) {
        return Direction::OneWayForward;
    }

    // Roundabouts are considered one-way implicitly
    let is_roundabout = junction_tag.map_or(false, |tag| tag.value == "roundabout");
    if is_roundabout {
        Direction::OneWayForward
    } else {
        Direction::Bidirectional
    }
}

fn highway_speed_kph(highway: &str) -> Option<f64> {
    match highway {
        "motorway" => Some(110.0),
        "motorway_link" => Some(60.0),
        "trunk" => Some(90.0),
        "trunk_link" => Some(45.0),
        "primary" => Some(65.0),
        "primary_link" => Some(45.0),
        "secondary" => Some(55.0),
        "secondary_link" => Some(40.0),
        "tertiary" => Some(45.0),
        "tertiary_link" => Some(35.0),
        "unclassified" => Some(45.0),
        "residential" => Some(30.0),
        "living_street" => Some(10.0),
        "service" => Some(20.0),
        "track" => Some(20.0),
        "road" => Some(50.0),
        _ => None,
    }
}

fn way_speed_kph(way: &XmlWay) -> f64 {
    const FALLBACK_SPEED_KPH: f64 = 50.0;

    if let Some(maxspeed) =
        find_tag(&way.tags, "maxspeed").and_then(|tag| clean_maxspeed(&tag.value))
    {
        return maxspeed;
    }

    find_tag(&way.tags, "highway")
        .and_then(|tag| highway_speed_kph(&tag.value))
        .unwrap_or(FALLBACK_SPEED_KPH)
}

fn edge_way_from_template(template: &XmlWay, length: f64, speed_kph: f64) -> XmlWay {
    XmlWay {
        id: template.id,
        nodes: Vec::new(),
        tags: template.tags.clone(),
        length,
        speed_kph,
        walk_travel_time: calculate_travel_time(length, 5.0),
        bike_travel_time: calculate_travel_time(length, 15.0),
        drive_travel_time: calculate_travel_time(length, speed_kph),
    }
}

// Function to create the network graph
pub fn create_graph(
    nodes: Vec<XmlNode>,
    ways: Vec<XmlWay>,
    retain_all: bool,
    bidirectional: bool,
) -> DiGraph<XmlNode, XmlWay> {
    let mut graph = DiGraph::<XmlNode, XmlWay>::new();
    let mut node_index_map = HashMap::new();

    // Add nodes to the graph and keep track of their indices
    for node in nodes {
        let id = node.id;
        let node_index = graph.add_node(node); // move — no clone needed, nodes is already owned
        node_index_map.insert(id, node_index);
    }

    // Add edges to the graph
    for mut way in ways {
        // Extract node refs before consuming `way` so that edge weights are stored
        // without the construction-only node list (saves memory for every edge in the graph).
        let node_refs = std::mem::take(&mut way.nodes);
        let path_direction = assess_path_directionality(&way);
        let speed_kph = way_speed_kph(&way);
        let filtered_way = way.filter_useful_tags();

        for window in node_refs.windows(2) {
            if let [start_ref, end_ref] = window {
                let start_index = node_index_map[&start_ref.node_id];
                let end_index = node_index_map[&end_ref.node_id];
                let length = {
                    let start_node = &graph[start_index];
                    let end_node = &graph[end_index];
                    calculate_distance(start_node.lat, start_node.lon, end_node.lat, end_node.lon)
                };
                let edge_way = edge_way_from_template(&filtered_way, length, speed_kph);

                match path_direction {
                    Direction::OneWayForward => {
                        graph.add_edge(start_index, end_index, edge_way);
                    }
                    Direction::OneWayReverse => {
                        graph.add_edge(end_index, start_index, edge_way);
                    }
                    Direction::Bidirectional => {
                        graph.add_edge(start_index, end_index, edge_way.clone());
                        graph.add_edge(end_index, start_index, edge_way);
                    }
                }

                if bidirectional && path_direction != Direction::Bidirectional {
                    let reverse_way = edge_way_from_template(&filtered_way, length, speed_kph);
                    match path_direction {
                        Direction::OneWayForward => {
                            graph.add_edge(end_index, start_index, reverse_way);
                        }
                        Direction::OneWayReverse => {
                            graph.add_edge(start_index, end_index, reverse_way);
                        }
                        Direction::Bidirectional => {}
                    }
                }
            }
        }
    }

    // Simplify graph topology for faster downstream calculations
    // Consolidates distance and speed from
    if !retain_all {
        graph = simplify_graph(&graph)
    }
    // ... other future logic

    graph
}

fn clean_maxspeed(maxspeed: &str) -> Option<f64> {
    let mph_to_kph = 1.60934;
    let trimmed = maxspeed.trim();
    let numeric_prefix: String = trimmed
        .chars()
        .take_while(|c| c.is_ascii_digit() || *c == '.')
        .collect();
    let speed = numeric_prefix.parse::<f64>().ok()?;
    if speed <= 0.0 {
        return None;
    }

    if trimmed.to_ascii_lowercase().contains("mph") {
        Some(speed * mph_to_kph)
    } else {
        Some(speed)
    }
}

pub fn node_to_latlon(graph: &DiGraph<XmlNode, XmlWay>, node_index: NodeIndex) -> (f64, f64) {
    let node = &graph[node_index];
    (node.lat, node.lon)
}

/// R-tree entry pairing a node's coordinates with its NodeIndex.
#[derive(Clone)]
struct NodeEntry {
    point: [f64; 2],
    index: NodeIndex,
}

impl RTreeObject for NodeEntry {
    type Envelope = AABB<[f64; 2]>;
    fn envelope(&self) -> Self::Envelope {
        AABB::from_point(self.point)
    }
}

fn spatial_index_point(lat: f64, lon: f64) -> [f64; 2] {
    let meters_per_degree = 111_320.0;
    [
        lat * meters_per_degree,
        lon * meters_per_degree * lat.to_radians().cos(),
    ]
}

impl PointDistance for NodeEntry {
    fn distance_2(&self, point: &[f64; 2]) -> f64 {
        let dlat = self.point[0] - point[0];
        let dlon = self.point[1] - point[1];
        dlat * dlat + dlon * dlon
    }
}

#[derive(Debug, Clone, Copy)]
pub struct SnapResult {
    pub input_lat: f64,
    pub input_lon: f64,
    pub node_index: NodeIndex,
    pub node_id: i64,
    pub node_lat: f64,
    pub node_lon: f64,
    pub distance_m: f64,
}

#[derive(Debug, Clone, Copy)]
pub struct SnappedPoi {
    pub poi_id: i64,
    pub snap: SnapResult,
}

/// A graph bundled with a spatial index for O(log n) nearest-node queries
/// and an optional pre-computed POI snap map for O(1) POI filtering.
///
/// Build once via `SpatialGraph::new`, reuse for all queries. All inner
/// fields are reference-counted so cloning is O(1).
#[derive(Clone)]
pub struct SpatialGraph {
    pub graph: Arc<DiGraph<XmlNode, XmlWay>>,
    tree: Arc<RTree<NodeEntry>>,
    /// POI OSM node id → snapped graph node diagnostics, computed once at startup via
    /// `snap_pois`. `None` until called; `Some` map used by POI filtering
    /// for O(1) lookup instead of an R-tree query on every request.
    pub poi_snaps: Option<Arc<HashMap<i64, SnappedPoi>>>,
}

impl SpatialGraph {
    pub fn new(graph: DiGraph<XmlNode, XmlWay>) -> Self {
        let entries: Vec<NodeEntry> = graph
            .node_indices()
            .map(|i| NodeEntry {
                point: spatial_index_point(graph[i].lat, graph[i].lon),
                index: i,
            })
            .collect();
        let tree = Arc::new(RTree::bulk_load(entries));
        let graph = Arc::new(graph);
        Self {
            graph,
            tree,
            poi_snaps: None,
        }
    }

    pub(crate) fn from_parsed_osm(
        data: XmlData,
        network_type: crate::overpass::NetworkType,
        retain_all: bool,
    ) -> Self {
        let bidirectional = matches!(network_type, crate::overpass::NetworkType::Walk);
        let graph = create_graph(data.nodes, data.ways, retain_all, bidirectional);
        Self::new(graph)
    }

    /// Parse an OSM XML response and build a [`SpatialGraph`].
    pub fn from_osm(
        xml: &str,
        network_type: crate::overpass::NetworkType,
        retain_all: Option<bool>,
    ) -> Result<Self, quick_xml::DeError> {
        let data = parse_xml(xml)?;
        Ok(Self::from_parsed_osm(
            data,
            network_type,
            retain_all.unwrap_or(false),
        ))
    }

    /// Pre-snap a set of POI nodes to their nearest graph nodes, storing the
    /// result for O(1) lookup at request time.
    ///
    /// `pois` is the POI list returned by [`crate::pbf::read_pbf`]. Call once
    /// at startup after `new`.
    pub fn snap_pois(&mut self, pois: &[crate::poi::Poi]) {
        let snaps: HashMap<i64, SnappedPoi> = pois
            .iter()
            .filter_map(|poi| {
                self.snap_point(poi.lat, poi.lon).map(|snap| {
                    (
                        poi.id,
                        SnappedPoi {
                            poi_id: poi.id,
                            snap,
                        },
                    )
                })
            })
            .collect();
        self.poi_snaps = Some(Arc::new(snaps));
    }

    pub fn nearest_node(&self, lat: f64, lon: f64) -> Option<NodeIndex> {
        self.tree
            .nearest_neighbor(&spatial_index_point(lat, lon))
            .map(|e| e.index)
    }

    pub fn snap_point(&self, lat: f64, lon: f64) -> Option<SnapResult> {
        self.nearest_node(lat, lon).map(|node_index| {
            let node = &self.graph[node_index];
            SnapResult {
                input_lat: lat,
                input_lon: lon,
                node_index,
                node_id: node.id,
                node_lat: node.lat,
                node_lon: node.lon,
                distance_m: calculate_distance(lat, lon, node.lat, node.lon),
            }
        })
    }
}

// Keep the free function for backwards compatibility but delegate to SpatialGraph
// NOTE: this clones the entire graph to build a temporary R-tree — prefer
// constructing a `SpatialGraph` directly and reusing it.
#[deprecated(
    since = "0.0.0",
    note = "construct a SpatialGraph and call nearest_node instead"
)]
pub fn latlon_to_node(graph: &DiGraph<XmlNode, XmlWay>, lat: f64, lon: f64) -> Option<NodeIndex> {
    SpatialGraph::new(graph.clone()).nearest_node(lat, lon)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_node(id: i64, lat: f64, lon: f64) -> XmlNode {
        XmlNode {
            id,
            lat,
            lon,
            tags: vec![],
        }
    }

    fn make_way_raw(node_ids: Vec<i64>, tags: Vec<(&str, &str)>) -> XmlWay {
        XmlWay {
            id: 1,
            nodes: node_ids
                .into_iter()
                .map(|id| XmlNodeRef { node_id: id })
                .collect(),
            tags: tags
                .into_iter()
                .map(|(k, v)| XmlTag {
                    key: k.into(),
                    value: v.into(),
                })
                .collect(),
            length: 0.0,
            speed_kph: 0.0,
            walk_travel_time: 0.0,
            bike_travel_time: 0.0,
            drive_travel_time: 0.0,
        }
    }

    #[test]
    fn test_graph_respects_maxspeed_tag() {
        let nodes = vec![make_node(1, 0.0, 0.0), make_node(2, 0.001, 0.0)];
        let way = make_way_raw(
            vec![1, 2],
            vec![("highway", "residential"), ("maxspeed", "30")],
        );
        let graph = create_graph(
            vec![nodes[0].clone(), nodes[1].clone()],
            vec![way],
            true,
            false,
        );
        assert_eq!(graph.edge_weights().next().unwrap().speed_kph, 30.0);
    }

    #[test]
    fn test_graph_parses_mph_maxspeed_tag() {
        let nodes = vec![make_node(1, 0.0, 0.0), make_node(2, 0.001, 0.0)];
        let way = make_way_raw(
            vec![1, 2],
            vec![("highway", "residential"), ("maxspeed", "30 mph")],
        );
        let graph = create_graph(
            vec![nodes[0].clone(), nodes[1].clone()],
            vec![way],
            true,
            false,
        );
        let speed = graph.edge_weights().next().unwrap().speed_kph;
        assert!((speed - 48.2802).abs() < 1e-4);
    }

    #[test]
    fn test_graph_falls_back_when_maxspeed_is_non_numeric() {
        let nodes = vec![make_node(1, 0.0, 0.0), make_node(2, 0.001, 0.0)];
        let way = make_way_raw(
            vec![1, 2],
            vec![("highway", "residential"), ("maxspeed", "signals")],
        );
        let graph = create_graph(
            vec![nodes[0].clone(), nodes[1].clone()],
            vec![way],
            true,
            false,
        );
        assert_eq!(graph.edge_weights().next().unwrap().speed_kph, 30.0);
    }

    #[test]
    fn test_oneway_produces_single_edge() {
        let nodes = vec![make_node(1, 0.0, 0.0), make_node(2, 0.001, 0.0)];
        let way = make_way_raw(
            vec![1, 2],
            vec![("highway", "residential"), ("oneway", "yes")],
        );
        let graph = create_graph(
            vec![nodes[0].clone(), nodes[1].clone()],
            vec![way],
            true,
            false,
        );
        assert_eq!(graph.edge_count(), 1);
    }

    #[test]
    fn test_bidirectional_produces_two_edges() {
        let nodes = vec![make_node(1, 0.0, 0.0), make_node(2, 0.001, 0.0)];
        let way = make_way_raw(vec![1, 2], vec![("highway", "residential")]);
        let graph = create_graph(
            vec![nodes[0].clone(), nodes[1].clone()],
            vec![way],
            true,
            false,
        );
        assert_eq!(graph.edge_count(), 2);
    }

    #[test]
    fn test_nearest_node_finds_closest() {
        let mut graph = DiGraph::new();
        graph.add_node(make_node(1, 48.0, 11.0));
        graph.add_node(make_node(2, 52.0, 13.0));
        let sg = SpatialGraph::new(graph);
        let idx = sg.nearest_node(48.001, 11.001).unwrap();
        assert_eq!(sg.graph[idx].id, 1);
    }

    #[test]
    fn test_snap_point_returns_diagnostics() {
        let mut graph = DiGraph::new();
        graph.add_node(make_node(1, 48.0, 11.0));
        let sg = SpatialGraph::new(graph);

        let snap = sg.snap_point(48.001, 11.001).unwrap();

        assert_eq!(snap.node_id, 1);
        assert_eq!(snap.node_lat, 48.0);
        assert_eq!(snap.node_lon, 11.0);
        assert!(snap.distance_m > 0.0);
    }
}