solverforge-maps 2.3.4

Generic map and routing utilities for VRP and similar problems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! Geographic utility functions.

use super::coord::Coord;

pub fn haversine_distance(a: Coord, b: Coord) -> f64 {
    const R: f64 = 6_371_000.0;

    let lat1_rad = a.lat.to_radians();
    let lat2_rad = b.lat.to_radians();
    let dlat = (b.lat - a.lat).to_radians();
    let dlng = (b.lng - a.lng).to_radians();

    let h =
        (dlat / 2.0).sin().powi(2) + lat1_rad.cos() * lat2_rad.cos() * (dlng / 2.0).sin().powi(2);
    let c = 2.0 * h.sqrt().asin();

    R * c
}