Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better distance & add nearest_stop #35

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading