Skip to content

Commit

Permalink
Better distance & add nearest_stop (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumlamm committed May 24, 2024
1 parent 15a0f0c commit 15bc16d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions robusta/src/kvv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,16 @@ pub fn train_positions(departures_per_line: &LineDepartures, render_time: DateTi
.flat_map(|(journey_ref, departures)| train_position_per_route(render_time, journey_ref, departures, stops))
.collect()
}

pub fn nearest_stop(pos: Point) -> &'static Stop {
let stops = KVV_STOPS.get().expect("KVV_STOPS not initialized");
stops
.iter()
.min_by_key(|stop| {
pos.distance(Point {
latitude: stop.lat as f32,
longitude: stop.lon as f32,
}) as u64
})
.expect("no stops")
}
9 changes: 8 additions & 1 deletion robusta/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ pub struct Point {
}

impl Point {
/// The approximate distance between two points in meters, using equirectangular projection.
pub fn distance(self, other: Self) -> f32 {
f32::hypot(other.latitude - self.latitude, other.longitude - self.longitude)
// the radius of the earth in meters
const EARTH_RADIUS: f32 = 6371008.8;

let delta_lat = (other.latitude - self.latitude).to_radians();
let delta_lon = (other.longitude - self.longitude).to_radians();
let mean_lat = (self.latitude + other.latitude) / 2.0;
EARTH_RADIUS * f32::hypot(delta_lat, f32::cos(mean_lat) * delta_lon)
}

/// Linear interpolation.
Expand Down

0 comments on commit 15bc16d

Please sign in to comment.