minimal routing code

This commit is contained in:
Nicholas Orlowsky 2025-11-19 18:32:06 -05:00
parent 2d8f131b91
commit a7d323056a
No known key found for this signature in database
GPG key ID: A9F3BA4C0AA7A70B
6 changed files with 151 additions and 20 deletions

View file

@ -1,8 +1,6 @@
use std::{collections::HashMap, env, hash::Hash, io::Cursor, path::PathBuf, sync::{Arc, Mutex}, thread, time::Duration};
use std::{collections::{HashMap, HashSet}, env, hash::Hash, io::Cursor, path::PathBuf, sync::{Arc, Mutex}, thread, time::Duration};
use anyhow::anyhow;
use gtfs_structures::Trip;
use libseptastic::agency;
use log::{info, error};
use serde::{Deserialize, Serialize};
use zip::ZipArchive;
@ -27,7 +25,9 @@ struct GtfsFile {
struct TransitData {
pub routes: HashMap<String, libseptastic::route::Route>,
pub agencies: HashMap<String, libseptastic::agency::Agency>,
pub trips: HashMap<String, Vec<libseptastic::stop_schedule::Trip>>
pub trips: HashMap<String, Vec<libseptastic::stop_schedule::Trip>>,
pub stops: HashMap<String, libseptastic::stop::Stop>,
pub route_id_by_stops: HashMap<String, HashSet<String>>
}
struct GtfsPullServiceState {
@ -43,7 +43,7 @@ pub struct GtfsPullService {
impl TransitData {
pub fn new() -> Self {
return TransitData { routes: HashMap::new(), agencies: HashMap::new(), trips: HashMap::new() }
return TransitData { routes: HashMap::new(), agencies: HashMap::new(), trips: HashMap::new(), stops: HashMap::new(), route_id_by_stops: HashMap::new() }
}
}
@ -102,6 +102,26 @@ impl GtfsPullService {
}
}
pub fn get_all_routes(&self) -> HashMap<String, libseptastic::route::Route> {
let l_state = self.state.lock().unwrap();
l_state.transit_data.routes.clone()
}
pub fn get_all_stops(&self) -> HashMap<String, libseptastic::stop::Stop> {
let l_state = self.state.lock().unwrap();
l_state.transit_data.stops.clone()
}
pub fn get_all_trips(&self) -> HashMap<String, Vec<libseptastic::stop_schedule::Trip>> {
let l_state = self.state.lock().unwrap();
l_state.transit_data.trips.clone()
}
pub fn get_routes_at_stop(&self, id: String) -> HashSet<String> {
let l_state = self.state.lock().unwrap();
l_state.transit_data.route_id_by_stops.get(&id).unwrap_or(&HashSet::new()).clone()
}
pub fn get_schedule(&self, route_id: String) -> anyhow::Result<Vec<libseptastic::stop_schedule::Trip>> {
let l_state = self.state.lock().unwrap();
if let Some(trips) = l_state.transit_data.trips.get(&route_id) {
@ -189,12 +209,25 @@ impl GtfsPullService {
});
}
for stop in &gtfs.stops {
l_state.transit_data.stops.insert(stop.1.id.clone(), libseptastic::stop::Stop {
id: stop.1.id.clone(),
name: stop.1.name.clone().unwrap(),
lat: stop.1.latitude.unwrap(),
lng: stop.1.longitude.unwrap(),
stop_type: libseptastic::stop::StopType::Normal
});
}
for trip in &gtfs.trips {
let global_rt_id = match &hack_agency {
Some(a) => format!("{}_{}", a.id, trip.1.route_id.clone()),
None => format!("{}", trip.1.route_id.clone())
};
let sched = trip.1.stop_times.iter().map(|s| libseptastic::stop_schedule::StopSchedule{
let sched = trip.1.stop_times.iter().map(|s| {
l_state.transit_data.route_id_by_stops.entry(s.stop.id.clone()).or_insert(HashSet::new()).insert(trip.1.route_id.clone());
libseptastic::stop_schedule::StopSchedule{
arrival_time: i64::from(s.arrival_time.unwrap()),
stop_sequence: i64::from(s.stop_sequence),
stop: libseptastic::stop::Stop {
@ -203,7 +236,7 @@ impl GtfsPullService {
lng: s.stop.longitude.unwrap(),
id: s.stop.id.parse().unwrap(),
stop_type: libseptastic::stop::StopType::Normal
}
}}
}).collect();
let trip = libseptastic::stop_schedule::Trip{