libits-client 3.1.0

library to connect on an ITS MQTT server
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
/*
 * Software Name : libits-client
 * SPDX-FileCopyrightText: Copyright (c) Orange SA
 * SPDX-License-Identifier: MIT
 *
 * This software is distributed under the MIT license,
 * see the "LICENSE.txt" file for more details or https://opensource.org/license/MIT/
 *
 * Authors: see CONTRIBUTORS.md
 */
use geo::prelude::*;
use geo::{LineString, Point};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result};
use std::hash::{Hash, Hasher};

const EARTH_RADIUS: f64 = 6_371_000.;
const EARTH_FLATTENING: f64 = 1. / 298.257223563;
const EQUATORIAL_RADIUS: f64 = 6_378_137.0;
const POLAR_RADIUS: f64 = 6_356_752.3;

/// Describes a geodesic position using SI units
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct Position {
    /// Latitude in radians
    pub latitude: f64,
    /// Longitude in radians
    pub longitude: f64,
    /// Altitude in meters
    pub altitude: f64,
}

impl Display for Position {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            "{{lat:{} ({}°), lon:{} ({}°), alt:{}}}",
            self.latitude,
            self.latitude.to_degrees(),
            self.longitude,
            self.longitude.to_degrees(),
            self.altitude,
        )
    }
}

impl Hash for Position {
    fn hash<H: Hasher>(&self, state: &mut H) {
        ((self.latitude * 1e8).round() as i64).hash(state);
        ((self.latitude * 1e8).round() as i64).hash(state);
        ((self.latitude * 1e3).round() as i64).hash(state);
    }
}

impl Eq for Position {}

pub fn position_from_degrees(lat: f64, lon: f64, alt: f64) -> Position {
    Position {
        latitude: lat.to_radians(),
        longitude: lon.to_radians(),
        altitude: alt,
    }
}

/// Returns the bearing from one Position to another
pub fn bearing(from: &Position, to: &Position) -> f64 {
    let (φ1, λ1) = (from.latitude, from.longitude);
    let (φ2, λ2) = (to.latitude, to.longitude);
    let δλ = λ2 - λ1;

    f64::atan2(
        δλ.sin() * φ2.cos(),
        φ1.cos() * φ2.sin() - φ1.sin() * φ2.cos() * δλ.cos(),
    )
}

pub fn haversine_distance(first: &Position, second: &Position) -> f64 {
    let longitude_distance = second.longitude - first.longitude;
    let latitude_distance = second.latitude - first.latitude;

    let a = (latitude_distance / 2.0).sin() * (latitude_distance / 2.0).sin()
        + first.latitude.cos()
            * second.latitude.cos()
            * (longitude_distance / 2.0).sin()
            * (longitude_distance / 2.0).sin();

    let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());

    EARTH_RADIUS * c
}

/// φ is a latitude, λ is a longitude, θ is the bearing (clockwise from north), δ is the angular distance d/R; d being the distance travelled, R the earth’s radius
pub fn haversine_destination(position: &Position, bearing: f64, distance: f64) -> Position {
    let φ1 = position.latitude;
    let λ1 = position.longitude;
    let δ = distance / EARTH_RADIUS;

    let φ2 = f64::asin(φ1.sin() * δ.cos() + φ1.cos() * δ.sin() * bearing.cos());
    let λ2 = λ1
        + f64::atan2(
            bearing.sin() * δ.sin() * φ1.cos(),
            δ.cos() - φ1.sin() * φ2.sin(),
        );

    Position {
        latitude: φ2,
        longitude: λ2,
        altitude: position.altitude,
    }
}

/// Destination computation from origin, bearing and distance using Vincenty formulae
///
///  Vincenty formulae written following:
/// - <http://www.geomidpoint.com/destination/calculation.html>
/// - <https://en.wikipedia.org/wiki/Vincenty%27s_formulae>
pub fn vincenty_destination(anchor: &Position, bearing: f64, distance: f64) -> Position {
    let sin_brg = bearing.sin();
    let cos_brg = bearing.cos();
    let tu1 = (1. - EARTH_FLATTENING) * anchor.latitude.tan();
    let cu1 = 1. / (1. + tu1 * tu1).sqrt();
    let su1 = tu1 * cu1;
    let σ = f64::atan2(tu1, cos_brg);
    let sin_α = cu1 * sin_brg;
    let cos_2_α = 1. - sin_α * sin_α;
    let u_2 = cos_2_α * (EQUATORIAL_RADIUS * EQUATORIAL_RADIUS - POLAR_RADIUS * POLAR_RADIUS)
        / (POLAR_RADIUS * POLAR_RADIUS);
    let a = 1. + u_2 / 16384. * (4096. + u_2 * (-768. + u_2 * (320. - 175. * u_2)));
    let b = u_2 / 1024. * (256. + u_2 * (-128. + u_2 * (74. - 47. * u_2)));
    let mut s1 = distance / (POLAR_RADIUS * a);

    let (ss1, cs1, cs1m) = loop {
        let cs1m = (2. * σ + s1).cos();
        let ss1 = s1.sin();
        let cs1 = s1.cos();
        let ds1 = b
            * ss1
            * (cs1m
                + b / 4.
                    * (cs1 * (-1. + 2. * cs1m * cs1m)
                        - b / 6. * cs1m * (-3. + 4. * ss1 * ss1) * (-3. + 4. * cs1m * cs1m)));
        let s1p = s1;
        s1 = distance / (POLAR_RADIUS * a) + ds1;

        if (s1 - s1p).abs() <= 1e-12 {
            break (ss1, cs1, cs1m);
        }
    };

    let t = su1 * ss1 - cu1 * cs1 * cos_brg;
    let lat2 = f64::atan2(
        su1 * cs1 + cu1 * ss1 * cos_brg,
        (1. - EARTH_FLATTENING) * (sin_α * sin_α + t * t).sqrt(),
    );
    let l2 = f64::atan2(ss1 * sin_brg, cu1 * cs1 - su1 * ss1 * cos_brg);
    let c = EARTH_FLATTENING / 16. * cos_2_α * (4. + EARTH_FLATTENING * (4. - 3. * cos_2_α));
    let l = l2
        - (1. - c)
            * EARTH_FLATTENING
            * sin_α
            * (s1 + c * ss1 * (cs1m + c * cs1 * (-1. + 2. * cs1m * cs1m)));
    let lon2 = anchor.longitude + l;

    Position {
        latitude: lat2,
        longitude: lon2,
        altitude: anchor.altitude,
    }
}

/// Returns the relative position of ENU coordinates from an anchor position
pub fn enu_destination(
    anchor: &Position,
    easting_offset: f64,
    northing_offset: f64,
    up_offset: f64,
) -> Position {
    let (latitude, longitude, altitude) = map_3d::enu2geodetic(
        easting_offset,
        northing_offset,
        up_offset,
        anchor.latitude,
        anchor.longitude,
        anchor.altitude,
        map_3d::Ellipsoid::WGS84,
    );

    Position {
        latitude,
        longitude,
        altitude,
    }
}

/// Returns the minimal distance from a Position to a list of Positions
///
/// FIXME this function requires testing and consolidation (follow up in issue [97][1])
///
/// [1]: https://github.com/Orange-OpenSource/its-client/issues/97
pub fn distance_to_line(position: &Position, line: &[Position]) -> f64 {
    let mut coordinates: Vec<geo::Coord<f64>> = Vec::new();

    for position in line {
        coordinates.push(
            geo::coord! { x: position.latitude.to_degrees(), y: position.longitude.to_degrees() },
        );
    }
    let lane_line = LineString::new(coordinates);

    let reference_point = Point::new(
        position.latitude.to_degrees(),
        position.longitude.to_degrees(),
    );

    Euclidean::distance(&reference_point, &lane_line)
}

#[cfg(test)]
mod tests {
    use crate::mobility::position::{
        bearing, enu_destination, haversine_destination, haversine_distance, position_from_degrees,
        vincenty_destination,
    };

    macro_rules! test_haversine_distance {
        ($test_name:ident, $f:expr, $s:expr, $e:expr) => {
            #[test]
            fn $test_name() {
                let distance = haversine_distance(&$f, &$s);
                let distance_delta = (distance - $e).abs();

                let epsilon = 1e-2;
                assert!(
                    distance_delta < epsilon,
                    "{} !< {}",
                    distance_delta,
                    epsilon
                );
            }
        };
    }
    test_haversine_distance!(
        haversine_distance_100_meters,
        position_from_degrees(48.6244870, 2.2436370, 0.),
        position_from_degrees(48.6237420, 2.2428750, 0.),
        100.
    );
    test_haversine_distance!(
        haversine_distance_30_meters,
        position_from_degrees(48.6250049, 2.2412209, 0.),
        position_from_degrees(48.6251958, 2.2415093, 0.),
        30.
    );

    macro_rules! test_enu_destination {
        ($test_name:ident, $anchor:expr, $e:expr, $n:expr, $u:expr, $exp_dst:expr) => {
            #[test]
            fn $test_name() {
                let offset_destination = enu_destination(&$anchor, $e, $n, $u);
                let lat_abs_diff =
                    (offset_destination.latitude.abs() - $exp_dst.latitude.abs()).abs();
                let lon_abs_diff =
                    (offset_destination.longitude.abs() - $exp_dst.longitude.abs()).abs();

                assert!(lat_abs_diff < 1e-8);
                assert!(lon_abs_diff < 1e-8);
            }
        };
    }
    test_enu_destination!(
        enu_destination_hunder_meters_north,
        position_from_degrees(43.63816914950018, 1.4031882, 0.),
        0.,
        100.,
        0.,
        position_from_degrees(43.63906919748, 1.4031882, 0.)
    );
    test_enu_destination!(
        enu_destination_hundred_meters_east,
        position_from_degrees(43.63816914950018, 1.4031882, 0.),
        100.,
        0.,
        0.,
        position_from_degrees(43.63816914950018, 1.40442743, 0.)
    );

    macro_rules! test_bearing {
        ($test_name:ident, $dst:expr, $exp_bearing:expr) => {
            #[test]
            fn $test_name() {
                let anchor = position_from_degrees(48.62519582726, 2.24150938995, 0.);
                let epsilon = 1e-2;

                let bearing = bearing(&anchor, &$dst);
                let bearing_in_degrees = (bearing.to_degrees() + 360.) % 360.;
                let bearing_delta = ($exp_bearing - bearing_in_degrees).abs();

                assert!(bearing_delta < epsilon, "{} !< {}", bearing_delta, epsilon);
            }
        };
    }
    test_bearing!(
        bearing_north_0_deg,
        position_from_degrees(48.80504512538, 2.24150940001, 0.),
        0.
    );
    test_bearing!(
        bearing_east_90_deg,
        position_from_degrees(48.62487660338, 2.5128078045, 0.),
        90.
    );
    test_bearing!(
        bearing_south_180_deg,
        position_from_degrees(48.44534088416, 2.24150940001, 0.),
        180.
    );
    test_bearing!(
        bearing_west_270_deg,
        position_from_degrees(48.62487660336, 1.9702109754, 0.),
        270.
    );
    test_bearing!(
        bearing_south_west_225_deg,
        position_from_degrees(48.62500535973, 2.24122119038, 0.),
        225.
    );
    test_bearing!(
        bearing_south_east_40_deg,
        position_from_degrees(48.76266875163, 2.41667377595, 0.),
        40.
    );
    test_bearing!(
        bearing_north_east_40_deg,
        position_from_degrees(47.12910495406, 4.26723335764, 0.),
        137.
    );
    test_bearing!(
        bearing_north_west_330_deg,
        position_from_degrees(48.78075523914, 2.1051415518, 0.),
        330.
    );

    macro_rules! test_vincenty_destination {
        ($test_name:ident, $bearing:expr, $distance:expr, $exp_dst:expr) => {
            #[test]
            fn $test_name() {
                let position = position_from_degrees(48.62519582726, 2.24150938995, 0.);
                let epsilon = 1e-7;
                let _position_deg_precision = 10e7;

                let destination = vincenty_destination(&position, $bearing.to_radians(), $distance);
                let lat_delta =
                    (destination.latitude.to_degrees() - $exp_dst.latitude.to_degrees()).abs();
                let lon_delta =
                    (destination.longitude.to_degrees() - $exp_dst.longitude.to_degrees()).abs();

                println!("Expected: {}", $exp_dst);
                println!("Actual: {}", destination);

                assert!(
                    lat_delta < epsilon,
                    "{} !< {} (expected: {}, actual: {})",
                    lat_delta,
                    epsilon,
                    $exp_dst.latitude.to_degrees(),
                    destination.latitude.to_degrees()
                );

                assert!(
                    lon_delta < epsilon,
                    "{} !< {} (expected: {}, actual: {})",
                    lon_delta,
                    epsilon,
                    $exp_dst.longitude.to_degrees(),
                    destination.longitude.to_degrees()
                );
            }
        };
    }
    test_vincenty_destination!(
        vincenty_north_bearing_360_deg_100m_destination,
        360f64,
        100.,
        position_from_degrees(48.62609508779, 2.24150940001, 0.)
    );
    test_vincenty_destination!(
        vincenty_north_bearing_0_deg_100m_destination,
        0f64,
        100.,
        position_from_degrees(48.62609508779, 2.24150940001, 0.)
    );
    test_vincenty_destination!(
        vincenty_south_bearing_180_deg_100m_destination,
        180f64,
        100.,
        position_from_degrees(48.62429656659, 2.24150940001, 0.)
    );
    test_vincenty_destination!(
        vincenty_south_bearing_minus_180_deg_100m_destination,
        -180f64,
        100.,
        position_from_degrees(48.62429656659, 2.24150940001, 0.)
    );
    test_vincenty_destination!(
        vincenty_east_bearing_90_deg_100m_destination,
        90f64,
        100.,
        position_from_degrees(48.62519580005, 2.24286588773, 0.)
    );
    test_vincenty_destination!(
        vincenty_east_bearing_minus_270_deg_100m_destination,
        -270f64,
        100.,
        position_from_degrees(48.62519580005, 2.24286588773, 0.)
    );
    test_vincenty_destination!(
        vincenty_west_bearing_270_deg_100m_destination,
        270f64,
        100.,
        position_from_degrees(48.62519580005, 2.24015289217, 0.)
    );
    test_vincenty_destination!(
        vincenty_west_bearing_minus_90_deg_100m_destination,
        -90f64,
        100.,
        position_from_degrees(48.62519580005, 2.24015289217, 0.)
    );

    macro_rules! test_haversine_destination {
        ($test_name:ident, $bearing:expr, $distance:expr, $exp_dst:expr) => {
            #[test]
            fn $test_name() {
                let position = position_from_degrees(48.62519582726, 2.24150938995, 0.);
                let epsilon = 1e-7;
                let _position_deg_precision = 10e7;

                let destination =
                    haversine_destination(&position, $bearing.to_radians(), $distance);
                let lat_delta =
                    (destination.latitude.to_degrees() - $exp_dst.latitude.to_degrees()).abs();
                let lon_delta =
                    (destination.longitude.to_degrees() - $exp_dst.longitude.to_degrees()).abs();

                println!("Expected: {}", $exp_dst);
                println!("Actual: {}", destination);

                assert!(
                    lat_delta < epsilon,
                    "{} !< {} (expected: {}, actual: {})",
                    lat_delta,
                    epsilon,
                    $exp_dst.latitude.to_degrees(),
                    destination.latitude.to_degrees()
                );

                assert!(
                    lon_delta < epsilon,
                    "{} !< {} (expected: {}, actual: {})",
                    lon_delta,
                    epsilon,
                    $exp_dst.longitude.to_degrees(),
                    destination.longitude.to_degrees()
                );
            }
        };
    }
    test_haversine_destination!(
        haversine_north_bearing_360_deg_100m_destination,
        360f64,
        100.,
        position_from_degrees(48.62609508779, 2.24150940001, 0.)
    );
    test_haversine_destination!(
        haversine_north_bearing_0_deg_100m_destination,
        0f64,
        100.,
        position_from_degrees(48.62609508779, 2.24150940001, 0.)
    );
    test_haversine_destination!(
        haversine_south_bearing_180_deg_100m_destination,
        180f64,
        100.,
        position_from_degrees(48.62429656659, 2.24150940001, 0.)
    );
    test_haversine_destination!(
        haversine_south_bearing_minus_180_deg_100m_destination,
        -180f64,
        100.,
        position_from_degrees(48.62429656659, 2.24150940001, 0.)
    );
}