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
//! Truck-related costing options.
use serde::Serialize;
#[serde_with::skip_serializing_none]
#[derive(Serialize, Debug, Clone, Default, PartialEq)]
struct TruckCostingOptionsInner {
maneuver_penalty: Option<f32>,
gate_cost: Option<f32>,
gate_penalty: Option<f32>,
private_access_penalty: Option<f32>,
destination_only_penalty: Option<f32>,
toll_booth_cost: Option<f32>,
toll_booth_penalty: Option<f32>,
ferry_cost: Option<f32>,
use_ferry: Option<f32>,
use_highways: Option<f32>,
use_tolls: Option<f32>,
use_living_streets: Option<f32>,
use_tracks: Option<f32>,
service_penalty: Option<f32>,
service_factor: Option<f32>,
country_crossing_cost: Option<f32>,
country_crossing_penalty: Option<f32>,
shortest: Option<bool>,
use_distance: Option<f32>,
disable_hierarchy_pruning: Option<bool>,
top_speed: Option<f32>,
fixed_speed: Option<u32>,
closure_factor: Option<f32>,
ignore_closures: Option<bool>,
ignore_restrictions: Option<bool>,
ignore_oneways: Option<bool>,
ignore_non_vehicular_restrictions: Option<bool>,
ignore_access: Option<bool>,
// -- ↓ truck only ↓ --
length: Option<f32>,
weight: Option<f32>,
axle_load: Option<f32>,
axle_count: Option<u32>,
hazmat: Option<bool>,
hgv_no_access_penalty: Option<f32>,
low_class_penalty: Option<f32>,
use_truck_route: Option<f32>,
}
#[derive(Serialize, Debug, Clone, Default, PartialEq)]
/// Options for truck routing.
pub struct TruckCostingOptions {
truck: TruckCostingOptionsInner,
}
impl TruckCostingOptions {
#[must_use]
/// Creates a new builder of [`TruckCostingOptions`].
pub fn builder() -> Self {
Self::default()
}
/// A cost applied when a [gate](http://wiki.openstreetmap.org/wiki/Tag:barrier%3Dgate) with
/// undefined or private access is encountered.
///
/// This cost is added to the estimated time / elapsed time.
///
/// Default: `30` seconds
pub fn gate_cost(mut self, gate_cost: f32) -> Self {
self.truck.gate_cost = Some(gate_cost);
self
}
/// A penalty applied when a [gate](https://wiki.openstreetmap.org/wiki/Tag:barrier%3Dgate) with
/// no access information is on the road.
///
/// Default: `300` seconds
pub fn gate_penalty(mut self, gate_penalty: f32) -> Self {
self.truck.gate_penalty = Some(gate_penalty);
self
}
/// A penalty applied when a [gate](https://wiki.openstreetmap.org/wiki/Tag:barrier%3Dgate) or
/// [bollard](https://wiki.openstreetmap.org/wiki/Tag:barrier%3Dbollard) with `access=private`
/// is encountered.
///
/// Default: `450` seconds
pub fn private_access_penalty(mut self, private_access_penalty: f32) -> Self {
self.truck.private_access_penalty = Some(private_access_penalty);
self
}
/// A penalty applied when entering a road which is only allowed to enter if necessary to reach
/// the [destination](https://wiki.openstreetmap.org/wiki/Tag:vehicle%3Ddestination).
pub fn destination_only_penalty(mut self, destination_only_penalty: f32) -> Self {
self.truck.destination_only_penalty = Some(destination_only_penalty);
self
}
/// A cost applied when a [toll booth](http://wiki.openstreetmap.org/wiki/Tag:barrier%3Dtoll_booth)
/// is encountered.
///
/// This cost is added to the estimated and elapsed times.
///
/// Default: `15` seconds
pub fn toll_booth_cost(mut self, toll_booth_cost: f32) -> Self {
self.truck.toll_booth_cost = Some(toll_booth_cost);
self
}
/// A penalty applied to the cost when a
/// [toll booth](http://wiki.openstreetmap.org/wiki/Tag:barrier%3Dtoll_booth) is encountered.
///
/// This penalty can be used to create paths that avoid toll roads.
///
/// Default: `0`
pub fn toll_booth_penalty(mut self, toll_booth_penalty: f32) -> Self {
self.truck.toll_booth_penalty = Some(toll_booth_penalty);
self
}
/// A cost applied when entering a ferry.
///
/// This cost is added to the estimated and elapsed times.
///
/// Default: `300` seconds (5 minutes)
pub fn ferry_cost(mut self, ferry_cost: f32) -> Self {
self.truck.ferry_cost = Some(ferry_cost);
self
}
/// This value indicates the willingness to take ferries.
///
/// This is a range of values between `0` and `1`:
/// - Values near `0` attempt to avoid ferries and
/// - values near `1` will favor ferries.
///
/// **Note:** sometimes ferries are required to complete a route so values of `0` are not guaranteed to avoid ferries entirely.
///
/// Default: `0.5`
pub fn use_ferry(mut self, use_ferry: f32) -> Self {
debug_assert!(use_ferry >= 0.0);
debug_assert!(use_ferry <= 1.0);
self.truck.use_ferry = Some(use_ferry);
self
}
/// This value indicates the willingness to take highways.
///
/// This is a range of values between `0` and 1:
/// - Values near `0` attempt to avoid highways and
/// - values near `1` will favor highways.
///
/// **Note:** sometimes highways are required to complete a route so values of `0` are not guaranteed to avoid highways entirely.
///
/// Default: `1.0`
pub fn use_highways(mut self, use_highways: f32) -> Self {
debug_assert!(use_highways >= 0.0);
debug_assert!(use_highways <= 1.0);
self.truck.use_highways = Some(use_highways);
self
}
/// This value indicates the willingness to take roads with tolls.
///
/// This is a range of values between `0` and 1:
/// - Values near `0` attempt to avoid tolls and
/// - values near `1` will not attempt to avoid them.
///
/// **Note:** sometimes roads with tolls are required to complete a route so values of `0` are not guaranteed to avoid them entirely.
///
/// Default: `0.5`
pub fn use_tolls(mut self, use_tolls: f32) -> Self {
debug_assert!(use_tolls >= 0.0);
debug_assert!(use_tolls <= 1.0);
self.truck.use_tolls = Some(use_tolls);
self
}
/// This value indicates the willingness to take living streets.
///
/// This is a range of values between `0` and 1:
/// - Values near `0` attempt to avoid living streets and
/// - values near `1` will favor living streets.
///
/// **Note:** sometimes living streets are required to complete a route so values of `0` are not guaranteed to avoid living streets entirely.
///
/// Default:
/// - `truck`: `0`
/// - `cars`/`buses`/`motor scooters`/`motorcycles`: `0.1`
pub fn use_living_streets(mut self, use_living_streets: f32) -> Self {
debug_assert!(use_living_streets >= 0.0);
debug_assert!(use_living_streets <= 1.0);
self.truck.use_living_streets = Some(use_living_streets);
self
}
/// This value indicates the willingness to take track roads.
///
/// This is a range of values between `0` and 1:
/// - Values near `0` attempt to avoid tracks and
/// - values near `1` will favor tracks a little bit.
///
/// **Note:** sometimes tracks are required to complete a route so values of `0` are not guaranteed to avoid tracks entirely.
///
/// Default:
/// - `0` for autos,
/// - `0.5` for motor scooters and motorcycles.
pub fn use_tracks(mut self, use_tracks: f32) -> Self {
debug_assert!(use_tracks >= 0.0);
debug_assert!(use_tracks <= 1.0);
self.truck.use_tracks = Some(use_tracks);
self
}
/// A penalty applied for transition to generic service road.
///
/// Default: `0`trucks and 15 for cars, buses, motor scooters and motorcycles.
pub fn service_penalty(mut self, service_penalty: f32) -> Self {
self.truck.service_penalty = Some(service_penalty);
self
}
/// A factor that modifies (multiplies) the cost when generic service roads are encountered.
///
/// Default: `1`
pub fn service_factor(mut self, service_factor: f32) -> Self {
self.truck.service_factor = Some(service_factor);
self
}
/// A cost applied when encountering an international border.
///
/// This cost is added to the estimated and elapsed times.
///
/// Default: `600` seconds
pub fn country_crossing_cost(mut self, country_crossing_cost: f32) -> Self {
self.truck.country_crossing_cost = Some(country_crossing_cost);
self
}
/// A penalty applied for a country crossing.
///
/// This penalty can be used to create paths that avoid spanning country boundaries.
///
/// Default: `0`
pub fn country_crossing_penalty(mut self, country_crossing_penalty: f32) -> Self {
self.truck.country_crossing_penalty = Some(country_crossing_penalty);
self
}
/// Changes the metric to quasi-shortest, i.e. **purely distance-based costing**.
///
/// Disables ALL other costings & penalties.
/// Also note, shortest will not disable hierarchy pruning, leading to potentially sub-optimal
/// routes for some costing models.
///
/// Default: `false`
pub fn only_consider_quasi_shortest(mut self) -> Self {
self.truck.shortest = Some(true);
self
}
/// A factor that allows controlling the contribution of distance and time to the route costs.
///
/// The value is in range between `0` and 1, where
/// - `0` only takes time into account (default),
/// - `0.5` will weight them roughly equally
/// - `1` only distance.
///
/// **Note:** this costing is currently only available for [`super::Costing::Auto`].
pub fn use_distance(mut self, use_distance: f32) -> Self {
debug_assert!(use_distance >= 0.0);
debug_assert!(use_distance <= 1.0);
self.truck.use_distance = Some(use_distance);
self
}
/// Disable hierarchies to calculate the actual optimal route.
///
/// **Note:** This could be quite a performance drainer so there is an upper limit of distance.
/// If the upper limit is exceeded, this option will always be `false`.
///
/// Default: `false`
pub fn disable_hierarchy_pruning(mut self) -> Self {
self.truck.disable_hierarchy_pruning = Some(true);
self
}
/// Top speed the vehicle can go.
///
/// Also used to avoid roads with higher speeds than this value.
/// Must be between `10` and `252 KPH`.
///
/// Default:
/// - `truck`: `120 KPH`
/// - `auto`/`bus`: `140 KPH`
pub fn top_speed(mut self, top_speed: f32) -> Self {
debug_assert!(top_speed >= 10.0);
debug_assert!(top_speed <= 252.0);
self.truck.top_speed = Some(top_speed);
self
}
/// Fixed speed the vehicle can go. Used to override the calculated speed.
///
/// Can be useful if speed of vehicle is known.
/// Must be between `1` and `252 KPH`.
///
/// Default: `0KPH` which disables fixed speed and falls back to the standard calculated speed
/// based on the road attribution.
pub fn fixed_speed(mut self, fixed_speed: u32) -> Self {
debug_assert!(fixed_speed >= 1);
debug_assert!(fixed_speed <= 252);
self.truck.fixed_speed = Some(fixed_speed);
self
}
/// A factor that penalizes the cost when traversing a closed edge
///
/// Example:
/// If `search_filter.exclude_closures` is `false` for origin and/or destination
/// location and the route starts/ends on closed edges.
///
/// Its value can range from
/// - `1.0` don't penalize closed edges,
/// - to `10.0` apply high cost penalty to closed edges.
///
/// **Note:** This factor is applicable only for motorized modes of transport, i.e `auto`, `motorcycle`, `motor_scooter`, `bus`, `truck` & `taxi`.
///
/// Default: `9.0`
pub fn closure_factor(mut self, closure_factor: f32) -> Self {
self.truck.closure_factor = Some(closure_factor);
self
}
/// If set ignores all closures, marked due to live traffic closures, during routing.
///
/// **Note:** This option cannot be set if `location.search_filter.exclude_closures` is also
/// specified in the request and will return an error if it is
pub fn ignore_closures(mut self) -> Self {
self.truck.ignore_closures = Some(true);
self
}
/// If set, ignores any restrictions (e.g. turn/dimensional/conditional restrictions).
///
/// Especially useful for matching GPS traces to the road network regardless of restrictions.
///
/// Default: `false`
pub fn ignore_restrictions(mut self) -> Self {
self.truck.ignore_restrictions = Some(true);
self
}
/// If set, ignores one-way restrictions.
///
/// Especially useful for matching GPS traces to the road network ignoring uni-directional traffic rules.
/// Not included in [`Self::ignore_restrictions`] option.
///
/// Default: `false`
pub fn ignore_oneways(mut self) -> Self {
self.truck.ignore_oneways = Some(true);
self
}
/// Similar to [`Self::ignore_restrictions`], but will respect restrictions that impact vehicle safety,
/// such as weight and size restrictions.
///
/// Default: `false`
pub fn ignore_non_vehicular_restrictions(mut self) -> Self {
self.truck.ignore_non_vehicular_restrictions = Some(true);
self
}
/// Ignore mode-specific access tags.
///
/// Especially useful for matching GPS traces to the road network regardless of restrictions.
///
/// Default `false`
pub fn ignore_access(mut self) -> Self {
self.truck.ignore_access = Some(true);
self
}
///The length of the truck (in meters).
///
/// Default: `21.64`
pub fn length(mut self, length: f32) -> Self {
self.truck.length = Some(length);
self
}
///The weight of the truck (in metric tons)
///
/// Default: `21.77`
pub fn weight(mut self, weight: f32) -> Self {
self.truck.weight = Some(weight);
self
}
///The axle load of the truck (in metric tons)
///
/// Default: `9.07`
pub fn axle_load(mut self, axle_load: f32) -> Self {
self.truck.axle_load = Some(axle_load);
self
}
///The axle count of the truck
///
/// Default: `5`
pub fn axle_count(mut self, axle_count: u32) -> Self {
self.truck.axle_count = Some(axle_count);
self
}
/// Indicates that the truck is carrying hazardous materials
///
/// Default: `false`
pub fn carries_hazardous_materials(mut self) -> Self {
self.truck.hazmat = Some(true);
self
}
///A penalty applied to roads with no HGV/truck access.
///
/// If set to a value less than `43200` seconds,
/// HGV will be allowed on these roads and the penalty applies
///
/// Default: `43200`, i.e. trucks are not allowed
pub fn hgv_no_access_penalty(mut self, hgv_no_access_penalty: f32) -> Self {
self.truck.hgv_no_access_penalty = Some(hgv_no_access_penalty);
self
}
///A penalty (in seconds) which is applied when going to residential or service roads
///
/// Default: `30` seconds
pub fn low_class_penalty(mut self, low_class_penalty: f32) -> Self {
self.truck.low_class_penalty = Some(low_class_penalty);
self
}
///This value is a range of values from `0` to `1`:
///- `0` indicates a light preference for streets marked as truck routes, and
/// - `1` indicates that streets not marked as truck routes should be avoided.
///
/// This information is derived from the [`hgv=designated`](https://wiki.openstreetmap.org/wiki/Key:hgv) tag.
/// **Note:** even with values near `1`, there is no guarantee the returned route will include
/// streets marked as truck routes.
///
/// Default: `0`
pub fn use_truck_route(mut self, use_truck_route: f32) -> Self {
self.truck.use_truck_route = Some(use_truck_route);
self
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn serialisation() {
assert_eq!(
serde_json::to_value(TruckCostingOptions::default()).unwrap(),
serde_json::json!({"truck":{}})
)
}
}