#distance #geolocation #haversine #no-std

no-std haversine-redux

Haversine formular library to determine the distances between two coordinates on the earth

3 unstable releases

0.2.1 Jun 9, 2022
0.2.0 Jun 9, 2022
0.1.0 Jun 8, 2022

#1137 in Math

MIT/Apache

8KB
94 lines

Haversine Formular in Rust

Documentation Crates.io License Build

This is a small library to implement the haversine formular in rust. There is already an haversine crate out there (you can find it here), but that library hasn't been updated since 2015 and because of that, it has some issues, especially the missing documentation and a little bit tedious and inefficient API.

Because of that, here's a rewrite, with a more intuitive interface and optional support for #![no_std].

Usage

Add haversine-redux as a dependency to your project Cargo.toml file:

[dependencies]
haversine-redux = "0.2"

Example usage:

use haversine_redux::{Location, Unit};

fn main() {
    let start = Location::new(38.898556, -77.037852);
    let end = Location::new(38.897147, -77.043934);
    
    // Kilometers
    let km = start.distance_to(&end, Unit::Kilometer);
    // OR
    let km = start.kilometers_to(&end);

    // Miles
    let miles = start.distance_to(&end, Unit::Mile);
    // OR
    let miles = start.miles_to(&end);

    // To use the haversine formular with a custom sphere which is not the earth use the CustomSphere unit and add the radius
    let custom = start.distance_to(&end, Unit::CustomSphere(50.0));
}

Optional support for #![no_std]

To enable no_std support, just add the no_std feature to the haversine-redux dependency:

[dependencies]
haversine-redux = {version = "0.2", features = ["no_std"]}

This will load the dependency libm for the implementations of mathematical functions like sin, cos or atan2.

Licensing

This library is dual-licensed under the MIT and the Apache 2.0 License.

Dependencies

~105KB