Skip to content

Commit

Permalink
Made legs contain positions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Jan 22, 2024
1 parent b3b83bd commit 47c8691
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
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

0 comments on commit 47c8691

Please sign in to comment.