Skip to content

Commit

Permalink
Organize code into core graph stuff, with analysis-ish layer on top
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Aug 16, 2024
1 parent fb3100e commit 1b326e2
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 404 deletions.
5 changes: 2 additions & 3 deletions backend/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use anyhow::Result;
use chrono::NaiveTime;
use geojson::{Feature, GeoJson, Geometry};

use crate::route::PathStep;
use crate::{Graph, Mode};
use crate::graph::{Graph, Mode, PathStep};

pub fn buffer_route(
graph: &Graph,
Expand All @@ -19,7 +18,7 @@ pub fn buffer_route(
let mut route_roads = HashSet::new();
let mut starts = HashSet::new();
for step in steps {
if let crate::route::PathStep::Road { road, .. } = step {
if let PathStep::Road { road, .. } = step {
route_roads.insert(road);
let road = &graph.roads[road.0];
starts.insert(road.src_i);
Expand Down
File renamed without changes.
File renamed without changes.
72 changes: 72 additions & 0 deletions backend/src/graph/isochrone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use chrono::NaiveTime;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::time::Duration;

use utils::PriorityQueueItem;

use super::costs::cost;
use crate::graph::{Graph, IntersectionID, Mode, RoadID};

impl Graph {
// TODO Doesn't account for start/end distance along roads
pub fn get_costs(
&self,
starts: Vec<IntersectionID>,
mode: Mode,
public_transit: bool,
start_time: NaiveTime,
end_time: NaiveTime,
) -> HashMap<RoadID, Duration> {
let mut visited: HashSet<IntersectionID> = HashSet::new();
let mut cost_per_road: HashMap<RoadID, Duration> = HashMap::new();
let mut queue: BinaryHeap<PriorityQueueItem<NaiveTime, IntersectionID>> = BinaryHeap::new();

for start in starts {
queue.push(PriorityQueueItem::new(start_time, start));
}

while let Some(current) = queue.pop() {
if visited.contains(&current.value) {
continue;
}
visited.insert(current.value);
if current.cost > end_time {
continue;
}

for r in &self.intersections[current.value.0].roads {
let road = &self.roads[r.0];
let total_cost = current.cost + cost(road, mode);
cost_per_road
.entry(*r)
.or_insert((total_cost - start_time).to_std().unwrap());

if road.src_i == current.value && road.allows_forwards(mode) {
queue.push(PriorityQueueItem::new(total_cost, road.dst_i));
}
if road.dst_i == current.value && road.allows_backwards(mode) {
queue.push(PriorityQueueItem::new(total_cost, road.src_i));
}

if public_transit {
for stop1 in &road.stops {
// Find all trips leaving from this step before the end_time
for next_step in self.gtfs.trips_from(
*stop1,
current.cost,
(end_time - current.cost).to_std().unwrap(),
) {
// TODO Awkwardly, arrive at both intersections for the next stop's road
let stop2_road = &self.roads[self.gtfs.stops[next_step.stop2.0].road.0];
for i in [stop2_road.src_i, stop2_road.dst_i] {
queue.push(PriorityQueueItem::new(next_step.time2, i));
}
}
}
}
}
}

cost_per_road
}
}
30 changes: 28 additions & 2 deletions backend/src/graph.rs → backend/src/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
mod amenity;
mod costs;
mod isochrone;
mod route;
mod scrape;
mod transit_route;

use anyhow::Result;
use enum_map::{Enum, EnumMap};
use geo::{Coord, LineLocatePoint, LineString, Point, Polygon};
Expand All @@ -6,9 +13,10 @@ use rstar::{primitives::GeomWithData, RTree};
use serde::{Deserialize, Serialize};
use utils::Mercator;

use crate::amenity::Amenity;
use self::amenity::Amenity;
use self::route::Router;
use crate::gtfs::TripID;
use crate::gtfs::{GtfsModel, StopID};
use crate::route::Router;

#[derive(Serialize, Deserialize)]
pub struct Graph {
Expand Down Expand Up @@ -212,3 +220,21 @@ pub struct Position {
pub fraction_along: f64,
pub intersection: IntersectionID,
}

pub enum GtfsSource {
Dir(String),
Geomedea(String),
None,
}

pub enum PathStep {
Road {
road: RoadID,
forwards: bool,
},
Transit {
stop1: StopID,
trip: TripID,
stop2: StopID,
},
}
17 changes: 2 additions & 15 deletions backend/src/route.rs → backend/src/graph/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@ use itertools::Itertools;
use serde::{Deserialize, Serialize};
use utils::{deserialize_nodemap, LineSplit, NodeMap};

use crate::costs::cost;
use crate::graph::{Graph, IntersectionID, Mode, Position, Road, RoadID};
use crate::gtfs::{StopID, TripID};

pub enum PathStep {
Road {
road: RoadID,
forwards: bool,
},
Transit {
stop1: StopID,
trip: TripID,
stop2: StopID,
},
}
use super::costs::cost;
use crate::graph::{Graph, IntersectionID, Mode, PathStep, Position, Road};

#[derive(Serialize, Deserialize)]
pub struct Router {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/scrape.rs → backend/src/graph/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use osm_reader::OsmID;
use rstar::RTree;
use utils::Tags;

use crate::amenity::Amenity;
use super::amenity::Amenity;
use super::route::Router;
use crate::graph::{
AmenityID, Direction, EdgeLocation, Graph, Intersection, IntersectionID, Mode, Road, RoadID,
AmenityID, Direction, EdgeLocation, Graph, GtfsSource, Intersection, IntersectionID, Mode,
Road, RoadID,
};
use crate::gtfs::{GtfsModel, StopID};
use crate::route::Router;
use crate::timer::Timer;
use crate::GtfsSource;

struct ReadAmenities {
amenities: Vec<Amenity>,
Expand Down
Loading

0 comments on commit 1b326e2

Please sign in to comment.