#distance #bearing #geolocation #earth

haversine-rs

Provides some helpers functions to calculate the distance between two points on Earth using the Haversine formula

5 unstable releases

0.3.0 May 22, 2023
0.2.2 May 21, 2023
0.2.1 May 21, 2023
0.2.0 May 21, 2023
0.1.0 May 21, 2023

#280 in Geospatial

Download history 748/week @ 2025-07-23 882/week @ 2025-07-30 667/week @ 2025-08-06 646/week @ 2025-08-13 519/week @ 2025-08-20 640/week @ 2025-08-27 718/week @ 2025-09-03 799/week @ 2025-09-10 735/week @ 2025-09-17 747/week @ 2025-09-24 798/week @ 2025-10-01 801/week @ 2025-10-08 763/week @ 2025-10-15 881/week @ 2025-10-22 669/week @ 2025-10-29 673/week @ 2025-11-05

3,162 downloads per month
Used in 4 crates (2 directly)

MIT license

11KB
94 lines

haversine-rs

Provides some helpers functions to calculate the distance between two points on Earth using the Haversine formula. Also can find the bearing between two points, and get a point at a given distance and bearing from a given point.

Installation

Add this to your Cargo.toml:

[dependencies]
haversine-rs = "0.3.0"

Usage

use haversine_rs::point::Point;
use haversine_rs::units::Unit;
use haversine_rs::distance;
use haversine_rs::bearing;
use haversine_rs::find_point;

fn main() {
    let p1 = Point::new(40.7767644, -73.9761399);
    let p2= Point::new(40.771209, -73.9673991);

    let distance = distance(p1, p2, Unit::Miles);
    let bearing = bearing(p1, p2);

    let p3 = find_point(p1, 1.0, 90.0, Unit::Miles);

    println!("Distance: {} miles", distance);
    println!("Bearing: {} degrees", bearing);
    println!("Point at 1 mile and 90 degrees: {:?}", p3);
}

Custom Earth radius

use haversine_rs::point::Point;
use haversine_rs::units::Unit;
use haversine_rs::distance;
use haversine_rs::bearing;
use haversine_rs::find_point;

fn main() {
    let p1 = Point::new(40.7767644, -73.9761399);
    let p2= Point::new(40.771209, -73.9673991);

    let distance = distance(p1, p2, Unit::Custom(3950.0));
    let bearing = bearing(p1, p2);

    let p3 = find_point(p1, 1.0, 90.0, Unit::Custom(3950.0));

    println!("Distance: {} miles", distance);
    println!("Bearing: {} degrees", bearing);
    println!("Point at 1 mile and 90 degrees: {:?}", p3);
}

Distance between multiple points

use haversine_rs::point::Point;
use haversine_rs::units::Unit;
use haversine_rs::distance_vec;

fn main() {
    let p1 = Point::new(40.7767644, -73.9761399);
    let p2 = Point::new(40.773987, -73.971769);
    let p3 = Point::new(40.771209, -73.9673991);

    let distance = distance_vec(vec![point_x, point_y, point_z], Unit::Miles);

    println!("Distance: {} miles", distance);
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

No runtime deps