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

Made legs contain positions #17

Merged
merged 1 commit into from
Jan 23, 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
2 changes: 1 addition & 1 deletion examples/dk_jets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.iter()
.map(|(_, legs)| {
legs.iter()
.map(|leg| emissions(leg.from.pos(), leg.to.pos(), Class::First) / 1000.0)
.map(|leg| emissions(leg.from().pos(), leg.to().pos(), Class::First) / 1000.0)
.sum::<f64>()
})
.sum::<f64>();
Expand Down
20 changes: 10 additions & 10 deletions examples/period.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
for leg in &legs {
log::info!(
"{},{},{},{},{},{},{},{},{}",
leg.from.datetime(),
leg.from.latitude(),
leg.from.longitude(),
leg.from.altitude(),
leg.to.datetime(),
leg.to.latitude(),
leg.to.longitude(),
leg.to.altitude(),
leg.maximum_altitude
leg.from().datetime(),
leg.from().latitude(),
leg.from().longitude(),
leg.from().altitude(),
leg.to().datetime(),
leg.to().latitude(),
leg.to().longitude(),
leg.to().altitude(),
leg.maximum_altitude(),
);
}

let commercial_to_private_ratio = 10.0;
let commercial_emissions_tons = legs
.iter()
.map(|leg| emissions(leg.from.pos(), leg.to.pos(), Class::First) / 1000.0)
.map(|leg| emissions(leg.from().pos(), leg.to().pos(), Class::First) / 1000.0)
.sum::<f64>();
let emissions_tons = Fact {
claim: (commercial_emissions_tons * commercial_to_private_ratio) as usize,
Expand Down
6 changes: 3 additions & 3 deletions examples/single_day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ async fn flight_date(
log::info!("Number of legs: {}", legs.len());

Ok(legs.into_iter().filter_map(|leg| {
let is_leg = matches!(leg.from, Position::Grounded{..}) & matches!(leg.to, Position::Grounded{..});
let is_leg = matches!(leg.from(), Position::Grounded{..}) & matches!(leg.to(), Position::Grounded{..});
if !is_leg {
log::info!("{:?} -> {:?} skipped", leg.from, leg.to);
log::info!("{:?} -> {:?} skipped", leg.from(), leg.to());
}
is_leg.then_some((leg.from, leg.to))
is_leg.then_some((leg.from().clone(), leg.to().clone()))
}).map(|(from, to)| {
let emissions = emissions(from.pos(), to.pos(), Class::First);

Expand Down
68 changes: 33 additions & 35 deletions src/legs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,58 @@ use crate::Position;
/// between two positions.
#[derive(Debug, Clone)]
pub struct Leg {
pub from: Position,
pub to: Position,
/// in feet
pub maximum_altitude: f64,
/// Sequence of positions defining the leg. Ends may start Flying, when the first/last observed
/// position was flying. Otherwise, first and last are Grounded.
positions: Vec<Position>,
}

impl Leg {
/// Positions of the leg
pub fn positions(&self) -> &[Position] {
&self.positions
}

/// Leg geo distance in km
pub fn distance(&self) -> f64 {
self.from.distace(&self.to)
self.from().distace(&self.to())
}

/// Leg duration
pub fn duration(&self) -> time::Duration {
self.to.datetime() - self.from.datetime()
self.to().datetime() - self.from().datetime()
}

pub fn maximum_altitude(&self) -> f64 {
self.positions
.iter()
.map(|p| p.altitude() as u32)
.max()
.unwrap() as f64
}

pub fn from(&self) -> &Position {
self.positions.first().unwrap()
}

pub fn to(&self) -> &Position {
self.positions.last().unwrap()
}
}

/// Returns a set of [`Leg`]s from a sequence of [`Position`]s.
fn all_legs(mut positions: impl Iterator<Item = Position>) -> Vec<Leg> {
pub fn all_legs(mut positions: impl Iterator<Item = Position>) -> Vec<Leg> {
let Some(mut prev_position) = positions.next() else {
return vec![];
};

let first = prev_position.clone();
let mut sequence: Vec<Position> = vec![];
let mut legs: Vec<Leg> = vec![];
let mut maximum_altitude = first.altitude();
positions.for_each(|position| {
maximum_altitude = position.altitude().max(maximum_altitude);
match (&prev_position, &position) {
(Position::Grounded { .. }, Position::Flying { .. }) => {
// departed, still do not know to where
legs.push(Leg {
from: prev_position.clone(),
to: prev_position.clone(),
maximum_altitude,
});
}
(Position::Flying { .. }, Position::Grounded { .. }) => {
// arrived
if let Some(leg) = legs.last_mut() {
// there is a leg - set its arrival position
leg.to = position.clone();
} else {
// if it was initially flying, need to push to the leg
legs.push(Leg {
from: first.clone(),
to: position.clone(),
maximum_altitude,
});
maximum_altitude = 0.0
}
}
_ => {}
sequence.push(position.clone());
if let (Position::Flying { .. }, Position::Grounded { .. }) = (&prev_position, &position) {
legs.push(Leg {
positions: std::mem::take(&mut sequence),
});
};
prev_position = position;
});
Expand Down
Loading