rapidgeo-distance
Geographic and planar distance calculations.
All coordinates use longitude, latitude ordering (lng, lat).
Installation
[]
= "0.2"
# Or with optional features
= { = "0.2", = ["batch", "vincenty"] }
Quick Start
use ;
let sf = new_deg; // San Francisco
let nyc = new_deg; // New York City
// Haversine: ±0.5% accuracy for distances <1000km
let distance = haversine;
println!;
// Vincenty: Sub-meter accuracy globally (requires "vincenty" feature)
let precise = vincenty_distance_m?;
println!;
// Euclidean: Fast but inaccurate for large distances
let euclidean = distance_euclid;
println!;
What This Crate Does
This crate calculates distances between geographic coordinates using three approaches:
- Geodesic algorithms - Account for Earth's shape (haversine, Vincenty)
- Euclidean distance - Treats coordinates as flat plane points
- Batch operations - Process many points efficiently with optional parallelization
All geodesic calculations use the WGS84 ellipsoid.
API Overview
Core Types
// All functions work with LngLat coordinates
let point = new_deg;
let = point.to_radians;
Coordinate Format Detection
The crate automatically detects and converts coordinate data from various formats (tuples, flat arrays, GeoJSON) to the internal LngLat representation:
use ;
// Automatically detects lng,lat vs lat,lng ordering
let coords = vec!;
let input = Tuples;
let lnglat_coords = coords_to_lnglat_vec;
See Coordinate Format Documentation for detailed examples of supported formats.
Geodesic Distances (Earth-Aware)
use ;
// Haversine: Fast, good accuracy for distances <1000km
let distance_m = haversine;
// Vincenty: Slower, very accurate, may fail for antipodal points
match vincenty_distance_m
Euclidean Distances (Flat Plane)
use ;
// Basic distance in degrees (not meters)
let dist_deg = distance_euclid;
// Squared distance (avoids sqrt for performance)
let dist_sq = distance_squared;
// Point to line segment distance
let segment = ;
let distance = point_to_segment;
Point-to-Segment Distances
use ;
let segment = ;
// ENU projection (good for small areas)
let distance = point_to_segment_enu_m;
// Great circle method (accurate but slower)
let distance = great_circle_point_to_seg;
Batch Operations
use ;
let path = vec!;
// Process consecutive pairs
let distances: = pairwise_haversine.collect;
// Total path length
let total = path_length_haversine;
// Write to pre-allocated buffer (no allocation)
let mut buffer = vec!;
pairwise_haversine_into;
Parallel Processing (requires "batch" feature)
use ;
let large_dataset = load_many_points;
// Parallel processing (beneficial for >1000 points)
let distances = pairwise_haversine_par;
let total = path_length_haversine_par;
Algorithm Selection
| Algorithm | Speed | Accuracy | Best For |
|---|---|---|---|
| Haversine | 46ns | ±0.5% | Distances <1000km |
| Vincenty | 271ns | ±1mm | High precision, any distance |
| Euclidean | 1ns | Poor at scale | Small areas, relative comparisons |
Accuracy Details
Haversine: Uses spherical approximation with ellipsoidal correction for the WGS84 ellipsoid. Good tradeoff for accuracy vs speed.
Vincenty: Implements Vincenty's inverse formula on WGS84 ellipsoid. May fail to converge for nearly antipodal points.
Euclidean: Simple Pythagorean theorem in degree space. Ignores Earth curvature. Error increases with distance and latitude.
Features
- Default: Haversine and Euclidean functions
vincenty: Enables high-precision Vincenty calculationsbatch: Enables Rayon-based parallel processing
Performance Notes
Serial vs Parallel: Parallel functions are faster for large datasets (>1000 points) but have overhead. Use serial for small datasets.
Memory Allocation: Functions ending in _into write to pre-allocated buffers, avoiding allocation overhead.
Benchmarks: On Intel i9-10900F:
- Euclidean: ~1ns per calculation
- Haversine: ~46ns per calculation
- Vincenty: ~271ns per calculation
- Pre-allocated buffers: ~60% faster than allocating
Coordinate System
All coordinates use longitude, latitude ordering:
- Longitude: -180.0 to +180.0° (West to East)
- Latitude: -90.0 to +90.0° (South to North)
let coord = new_deg; // Note: lng first
Limitations
- Vincenty may fail for nearly antipodal points
- Euclidean accuracy degrades significantly with distance and latitude
- Parallel functions require the
batchfeature - All geodesic calculations assume WGS84 ellipsoid
- Point-to-segment functions assume segments shorter than hemisphere
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.