valhalla-client 0.5.1

API client for the Valhalla routing engine
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Models connected to the [`trace_attributes`] map-matching API
//!
//! See <https://valhalla.github.io/valhalla/api/map-matching/api-reference/> for details.

use crate::costing;
use crate::elevation::ShapeFormat;
pub use crate::shapes::ShapePoint;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// A shape point for the trace_attributes request.
#[derive(Serialize, Debug, Clone)]
pub struct TracePoint {
    /// Latitude in degrees
    pub lat: f64,
    /// Longitude in degrees
    pub lon: f64,
}

impl TracePoint {
    /// Create a new trace point
    pub fn new(lat: f64, lon: f64) -> Self {
        Self { lat, lon }
    }
}

/// How to match the shape to the road network.
#[derive(Serialize, Debug, Clone, Default)]
#[serde(rename_all = "snake_case")]
pub enum ShapeMatch {
    /// Try edge walking first, fall back to map matching
    #[default]
    WalkOrSnap,
    /// Use map matching algorithm
    MapSnap,
    /// Use edge walking algorithm (requires very precise input)
    EdgeWalk,
}

/// Filter to include or exclude specific attributes in the response.
#[derive(Serialize, Debug, Clone)]
pub struct Filter {
    /// List of attribute names to include or exclude
    pub attributes: Vec<String>,
    /// Whether to include or exclude the listed attributes
    pub action: FilterAction,
}

/// Whether to include or exclude filtered attributes.
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum FilterAction {
    /// Include only the listed attributes
    Include,
    /// Exclude the listed attributes
    Exclude,
}

/// Options to fine-tune the GPS trace matching algorithm.
#[serde_with::skip_serializing_none]
#[derive(Serialize, Debug, Clone, Default)]
pub struct TraceOptions {
    /// Search radius in meters around each input point within which to search
    /// for candidate edges.
    ///
    /// Default: `25`
    pub search_radius: Option<f64>,
    /// GPS accuracy in meters for the input points.
    ///
    /// Default: `5`
    pub gps_accuracy: Option<f64>,
    /// Distance in meters beyond which a new breakage will be created.
    ///
    /// Default: `2000`
    pub breakage_distance: Option<f64>,
    /// Distance in meters to interpolate between input points.
    ///
    /// Default: `10`
    pub interpolation_distance: Option<f64>,
}

/// Request manifest for the trace_attributes API.
#[serde_with::skip_serializing_none]
#[derive(Serialize, Debug, Clone)]
pub struct Manifest {
    shape: Option<Vec<TracePoint>>,
    encoded_polyline: Option<String>,
    shape_format: Option<ShapeFormat>,
    #[serde(flatten)]
    costing: costing::Costing,
    shape_match: ShapeMatch,
    filters: Option<Filter>,
    trace_options: Option<TraceOptions>,
    units: Option<super::Units>,
    id: Option<String>,
    language: Option<String>,
    durations: Option<Vec<f64>>,
    use_timestamps: Option<bool>,
    begin_time: Option<String>,
}

impl Manifest {
    /// Create a builder with the given shape points and costing.
    pub fn builder(shape: impl IntoIterator<Item = TracePoint>, costing: costing::Costing) -> Self {
        Self {
            shape: Some(shape.into_iter().collect()),
            encoded_polyline: None,
            shape_format: None,
            costing,
            shape_match: ShapeMatch::default(),
            filters: None,
            trace_options: None,
            units: None,
            id: None,
            language: None,
            durations: None,
            use_timestamps: None,
            begin_time: None,
        }
    }

    /// Create a builder with an encoded polyline and costing.
    ///
    /// See [`Self::shape_format`] to set the precision of the polyline.
    pub fn builder_encoded(encoded_polyline: impl ToString, costing: costing::Costing) -> Self {
        Self {
            shape: None,
            encoded_polyline: Some(encoded_polyline.to_string()),
            shape_format: None,
            costing,
            shape_match: ShapeMatch::default(),
            filters: None,
            trace_options: None,
            units: None,
            id: None,
            language: None,
            durations: None,
            use_timestamps: None,
            begin_time: None,
        }
    }

    /// Set the shape matching mode.
    pub fn shape_match(mut self, shape_match: ShapeMatch) -> Self {
        self.shape_match = shape_match;
        self
    }

    /// Specifies whether the polyline is encoded with
    /// - 6 digit precision ([`ShapeFormat::Polyline6`]) or
    /// - 5 digit precision ([`ShapeFormat::Polyline5`]).
    ///
    /// Default: [`ShapeFormat::Polyline6`]
    pub fn shape_format(mut self, shape_format: ShapeFormat) -> Self {
        debug_assert!(
            self.shape.is_none(),
            "shape is set and setting the shape_format is requested. This combination does not make sense: shapes and encoded_polylines as input are mutually exclusive."
        );
        self.shape_format = Some(shape_format);
        self
    }

    /// Set the attribute filter to include specific edge attributes.
    pub fn include_attributes(
        mut self,
        attributes: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.filters = Some(Filter {
            attributes: attributes.into_iter().map(|a| a.into()).collect(),
            action: FilterAction::Include,
        });
        self
    }

    /// Set the attribute filter to exclude specific edge attributes.
    pub fn exclude_attributes(
        mut self,
        attributes: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.filters = Some(Filter {
            attributes: attributes.into_iter().map(|a| a.into()).collect(),
            action: FilterAction::Exclude,
        });
        self
    }

    /// Set trace matching algorithm options.
    pub fn trace_options(mut self, trace_options: TraceOptions) -> Self {
        self.trace_options = Some(trace_options);
        self
    }

    /// Sets the distance units for output.
    ///
    /// Default: [`super::Units::Metric`]
    pub fn units(mut self, units: super::Units) -> Self {
        self.units = Some(units);
        self
    }

    /// Name of the request.
    ///
    /// If id is specified, the naming will be sent through to the response.
    pub fn id(mut self, id: impl ToString) -> Self {
        self.id = Some(id.to_string());
        self
    }

    /// The language of the narration instructions based on the
    /// [IETF BCP 47](https://en.wikipedia.org/wiki/IETF_language_tag) language tag string.
    ///
    /// Default: `en-US`
    pub fn language(mut self, language: impl ToString) -> Self {
        self.language = Some(language.to_string());
        self
    }

    /// Set durations in seconds between successive input points.
    ///
    /// When provided along with [`Self::use_timestamps`], Valhalla can use timing
    /// information to improve matching accuracy.
    pub fn durations(mut self, durations: impl IntoIterator<Item = f64>) -> Self {
        self.durations = Some(durations.into_iter().collect());
        self
    }

    /// Whether to use timestamps/durations for the trace matching.
    ///
    /// Default: `false`
    pub fn use_timestamps(mut self, use_timestamps: bool) -> Self {
        self.use_timestamps = Some(use_timestamps);
        self
    }

    /// Set the begin time for the trace in the format `YYYY-MM-DDTHH:MM`.
    ///
    /// Used together with [`Self::durations`] and [`Self::use_timestamps`].
    pub fn begin_time(mut self, begin_time: impl ToString) -> Self {
        self.begin_time = Some(begin_time.to_string());
        self
    }
}

/// Surface type of a road edge.
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Surface {
    /// Smooth paved surface
    PavedSmooth,
    /// Paved surface
    Paved,
    /// Rough paved surface
    PavedRough,
    /// Compacted surface
    Compacted,
    /// Dirt surface
    Dirt,
    /// Gravel surface
    Gravel,
    /// Path surface
    Path,
    /// Impassable surface
    Impassable,
}

/// Road classification of an edge.
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RoadClass {
    /// Motorway
    Motorway,
    /// Trunk road
    Trunk,
    /// Primary road
    Primary,
    /// Secondary road
    Secondary,
    /// Tertiary road
    Tertiary,
    /// Unclassified road
    Unclassified,
    /// Residential road
    Residential,
    /// Service or other road
    ServiceOther,
}

/// Use type of an edge.
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum EdgeUse {
    /// Standard road
    Road,
    /// Ramp (highway on/off)
    Ramp,
    /// Turn channel
    TurnChannel,
    /// Track
    Track,
    /// Driveway
    Driveway,
    /// Alley
    Alley,
    /// Parking aisle
    ParkingAisle,
    /// Emergency access
    EmergencyAccess,
    /// Drive through
    DriveThrough,
    /// Cul-de-sac
    Culdesac,
    /// Cycleway
    Cycleway,
    /// Mountain bike trail
    MountainBike,
    /// Sidewalk
    Sidewalk,
    /// Footway
    Footway,
    /// Steps/stairs
    Steps,
    /// Ferry
    Ferry,
    /// Rail ferry
    #[serde(rename = "rail-ferry")]
    RailFerry,
    /// Service road
    ServiceRoad,
    /// Path
    Path,
    /// Living street
    LivingStreet,
    /// Pedestrian crossing
    PedestrianCrossing,
    /// Other use
    Other,
}

/// A matched edge in the trace_attributes response.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Edge {
    /// Road surface type
    #[serde(default)]
    pub surface: Option<Surface>,
    /// Road classification
    #[serde(default)]
    pub road_class: Option<RoadClass>,
    /// Edge use type
    #[serde(default)]
    pub r#use: Option<EdgeUse>,
    /// Length of the edge in the response units (km or miles)
    #[serde(default)]
    pub length: Option<f64>,
    /// Road names
    #[serde(default)]
    pub names: Option<Vec<String>>,
    /// Index into the response shape where this edge begins
    #[serde(default)]
    pub begin_shape_index: Option<u32>,
    /// Index into the response shape where this edge ends
    #[serde(default)]
    pub end_shape_index: Option<u32>,
    /// OSM way ID
    #[serde(default)]
    pub way_id: Option<u64>,
    /// Percentage along the edge where the source point lies (first edge only)
    #[serde(default)]
    pub source_percent_along: Option<f64>,
    /// Percentage along the edge where the target point lies (last edge only)
    #[serde(default)]
    pub target_percent_along: Option<f64>,
}

/// A matched point in the trace_attributes response.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct MatchedPoint {
    /// Latitude of the matched point
    pub lat: f64,
    /// Longitude of the matched point
    pub lon: f64,
    /// Match type
    #[serde(default)]
    pub r#type: Option<String>,
    /// Index of the edge this point was matched to
    #[serde(default)]
    pub edge_index: Option<u32>,
    /// Distance along the edge
    #[serde(default)]
    pub distance_along_edge: Option<f64>,
}

/// Response from the trace_attributes API.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Response {
    /// Matched edges with attributes
    #[serde(default)]
    pub edges: Vec<Edge>,
    /// Matched input points
    #[serde(default)]
    pub matched_points: Vec<MatchedPoint>,
    /// Encoded polyline of the matched path
    #[serde(default)]
    pub shape: Option<String>,
    /// Units used in the response
    #[serde(default)]
    pub units: Option<String>,
    /// Name of the request (echoed from the request)
    #[serde(default)]
    pub id: Option<String>,
    /// This array may contain warning objects informing about deprecated
    /// request parameters, clamped values, etc.
    #[serde(default)]
    pub warnings: Vec<Value>,
}

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

    #[test]
    fn test_serialize_manifest() {
        let manifest = Manifest::builder(
            [TracePoint::new(48.1, 11.5), TracePoint::new(48.2, 11.6)],
            costing::Costing::Auto(Default::default()),
        );
        let value = serde_json::to_value(&manifest).unwrap();
        assert_eq!(
            value,
            serde_json::json!({
                "shape": [{"lat": 48.1, "lon": 11.5}, {"lat": 48.2, "lon": 11.6}],
                "costing": "auto",
                "costing_options": {"auto": {}},
                "shape_match": "walk_or_snap"
            })
        );
    }

    #[test]
    fn test_serialize_manifest_encoded_polyline() {
        let manifest =
            Manifest::builder_encoded("some_polyline", costing::Costing::Auto(Default::default()))
                .shape_format(ShapeFormat::Polyline5);
        let value = serde_json::to_value(&manifest).unwrap();
        assert_eq!(
            value,
            serde_json::json!({
                "encoded_polyline": "some_polyline",
                "shape_format": "polyline5",
                "costing": "auto",
                "costing_options": {"auto": {}},
                "shape_match": "walk_or_snap"
            })
        );
    }

    #[test]
    fn test_serialize_manifest_with_filter() {
        let manifest = Manifest::builder(
            [TracePoint::new(48.1, 11.5)],
            costing::Costing::Pedestrian(Default::default()),
        )
        .shape_match(ShapeMatch::MapSnap)
        .include_attributes(["edge.surface", "edge.road_class"]);
        let value = serde_json::to_value(&manifest).unwrap();
        assert_eq!(
            value,
            serde_json::json!({
                "shape": [{"lat": 48.1, "lon": 11.5}],
                "costing": "pedestrian",
                "costing_options": {"pedestrian": {}},
                "shape_match": "map_snap",
                "filters": {
                    "attributes": ["edge.surface", "edge.road_class"],
                    "action": "include"
                }
            })
        );
    }

    #[test]
    fn test_serialize_manifest_exclude_attributes() {
        let manifest = Manifest::builder(
            [TracePoint::new(48.1, 11.5)],
            costing::Costing::Auto(Default::default()),
        )
        .exclude_attributes(["edge.names"]);
        let value = serde_json::to_value(&manifest).unwrap();
        assert_eq!(
            value["filters"],
            serde_json::json!({
                "attributes": ["edge.names"],
                "action": "exclude"
            })
        );
    }

    #[test]
    fn test_serialize_manifest_with_all_options() {
        let manifest = Manifest::builder(
            [TracePoint::new(48.1, 11.5)],
            costing::Costing::Auto(Default::default()),
        )
        .units(super::super::Units::Imperial)
        .id("my-trace")
        .language("de-DE")
        .trace_options(TraceOptions {
            search_radius: Some(50.0),
            gps_accuracy: Some(10.0),
            breakage_distance: Some(3000.0),
            interpolation_distance: Some(20.0),
        })
        .durations(vec![0.0, 5.0, 10.0])
        .use_timestamps(true)
        .begin_time("2025-01-15T08:30");
        let value = serde_json::to_value(&manifest).unwrap();
        assert_eq!(value["units"], serde_json::json!("miles"));
        assert_eq!(value["id"], serde_json::json!("my-trace"));
        assert_eq!(value["language"], serde_json::json!("de-DE"));
        assert_eq!(
            value["trace_options"]["search_radius"],
            serde_json::json!(50.0)
        );
        assert_eq!(
            value["trace_options"]["gps_accuracy"],
            serde_json::json!(10.0)
        );
        assert_eq!(
            value["trace_options"]["breakage_distance"],
            serde_json::json!(3000.0)
        );
        assert_eq!(
            value["trace_options"]["interpolation_distance"],
            serde_json::json!(20.0)
        );
        assert_eq!(value["durations"], serde_json::json!([0.0, 5.0, 10.0]));
        assert_eq!(value["use_timestamps"], serde_json::json!(true));
        assert_eq!(value["begin_time"], serde_json::json!("2025-01-15T08:30"));
    }

    #[test]
    fn test_serialize_trace_options_skips_none() {
        let manifest = Manifest::builder(
            [TracePoint::new(48.1, 11.5)],
            costing::Costing::Auto(Default::default()),
        )
        .trace_options(TraceOptions {
            search_radius: Some(50.0),
            ..Default::default()
        });
        let value = serde_json::to_value(&manifest).unwrap();
        assert_eq!(
            value["trace_options"],
            serde_json::json!({"search_radius": 50.0})
        );
    }

    #[test]
    fn test_deserialize_response() {
        let json = serde_json::json!({
            "edges": [{
                "surface": "paved",
                "road_class": "primary",
                "use": "road",
                "length": 0.123,
                "names": ["Main Street"],
                "begin_shape_index": 0,
                "end_shape_index": 5,
                "way_id": 12345,
                "source_percent_along": 0.1,
                "target_percent_along": 0.9
            }],
            "matched_points": [{
                "lat": 48.1,
                "lon": 11.5,
                "type": "matched",
                "edge_index": 0,
                "distance_along_edge": 0.5
            }],
            "shape": "encoded_shape_string",
            "units": "km",
            "id": "my-trace",
            "warnings": [{"message": "some warning"}]
        });
        let response: Response = serde_json::from_value(json).unwrap();
        assert_eq!(response.edges.len(), 1);
        assert_eq!(response.edges[0].surface, Some(Surface::Paved));
        assert_eq!(response.edges[0].road_class, Some(RoadClass::Primary));
        assert_eq!(response.edges[0].r#use, Some(EdgeUse::Road));
        assert_eq!(response.edges[0].length, Some(0.123));
        assert_eq!(
            response.edges[0].names,
            Some(vec!["Main Street".to_string()])
        );
        assert_eq!(response.edges[0].begin_shape_index, Some(0));
        assert_eq!(response.edges[0].end_shape_index, Some(5));
        assert_eq!(response.edges[0].way_id, Some(12345));
        assert_eq!(response.edges[0].source_percent_along, Some(0.1));
        assert_eq!(response.edges[0].target_percent_along, Some(0.9));
        assert_eq!(response.matched_points.len(), 1);
        assert_eq!(response.matched_points[0].lat, 48.1);
        assert_eq!(response.matched_points[0].lon, 11.5);
        assert_eq!(
            response.matched_points[0].r#type,
            Some("matched".to_string())
        );
        assert_eq!(response.matched_points[0].edge_index, Some(0));
        assert_eq!(response.matched_points[0].distance_along_edge, Some(0.5));
        assert_eq!(response.shape, Some("encoded_shape_string".to_string()));
        assert_eq!(response.units, Some("km".to_string()));
        assert_eq!(response.id, Some("my-trace".to_string()));
        assert_eq!(response.warnings.len(), 1);
    }

    #[test]
    fn test_deserialize_response_with_defaults() {
        let json = serde_json::json!({});
        let response: Response = serde_json::from_value(json).unwrap();
        assert_eq!(response.edges.len(), 0);
        assert_eq!(response.matched_points.len(), 0);
        assert_eq!(response.shape, None);
        assert_eq!(response.units, None);
        assert_eq!(response.id, None);
        assert_eq!(response.warnings.len(), 0);
    }

    #[test]
    fn test_deserialize_edge_with_defaults() {
        let json = serde_json::json!({});
        let edge: Edge = serde_json::from_value(json).unwrap();
        assert_eq!(edge.surface, None);
        assert_eq!(edge.road_class, None);
        assert_eq!(edge.r#use, None);
        assert_eq!(edge.length, None);
        assert_eq!(edge.names, None);
        assert_eq!(edge.way_id, None);
    }

    #[test]
    fn test_serialize_shape_match_variants() {
        assert_eq!(
            serde_json::to_value(ShapeMatch::WalkOrSnap).unwrap(),
            serde_json::json!("walk_or_snap")
        );
        assert_eq!(
            serde_json::to_value(ShapeMatch::MapSnap).unwrap(),
            serde_json::json!("map_snap")
        );
        assert_eq!(
            serde_json::to_value(ShapeMatch::EdgeWalk).unwrap(),
            serde_json::json!("edge_walk")
        );
    }
}