From a069380439b4579d13c95353e6acf66a8d460256 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 23 May 2019 14:00:30 -0700 Subject: [PATCH] importing basic info from PSRC trips --- popdat/src/main.rs | 6 +++++- popdat/src/psrc.rs | 51 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/popdat/src/main.rs b/popdat/src/main.rs index 86ba627665..832367e402 100644 --- a/popdat/src/main.rs +++ b/popdat/src/main.rs @@ -4,7 +4,11 @@ fn main() { "/home/dabreegster/Downloads/psrc/2014/landuse/parcels_urbansim.txt", ) .unwrap(); - println!("{} matches", parcels.len()); + println!("{} parcels", parcels.len()); + let trips = + popdat::psrc::import_trips("/home/dabreegster/Downloads/psrc/trips_2014.csv", parcels) + .unwrap(); + println!("{} trips", trips.len()); let popdat = popdat::PopDat::import_all(&mut abstutil::Timer::new("importing popdat")); abstutil::write_binary("../data/shapes/popdat", &popdat).unwrap(); diff --git a/popdat/src/psrc.rs b/popdat/src/psrc.rs index c4be3f8ba9..28b213dbe3 100644 --- a/popdat/src/psrc.rs +++ b/popdat/src/psrc.rs @@ -1,8 +1,54 @@ -use geom::{GPSBounds, LonLat}; +use geom::{Duration, GPSBounds, LonLat}; use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Write}; +pub struct Trip { + pub from: LonLat, + pub to: LonLat, + // Relative to midnight + pub depart_at: Duration, + // TODO Also scrape mode, maybe interesting extra stuff like purpose of the trip +} + +pub fn import_trips( + path: &str, + parcels: HashMap, +) -> Result, failure::Error> { + let mut trips = Vec::new(); + for rec in csv::Reader::from_reader(BufReader::new(File::open(path)?)).records() { + let rec = rec?; + + // opcl + let from = if let Some(pt) = parcels.get(rec[15].trim_end_matches(".0")) { + *pt + } else { + continue; + }; + // dpcl + let to = if let Some(pt) = parcels.get(rec[6].trim_end_matches(".0")) { + *pt + } else { + continue; + }; + + // deptm + let mins: usize = rec[4].trim_end_matches(".0").parse()?; + let depart_at = Duration::minutes(mins); + + trips.push(Trip { + from, + to, + depart_at, + }); + // TODO Read all trips + if trips.len() == 10 { + break; + } + } + Ok(trips) +} + pub fn import_parcels(path: &str) -> Result, failure::Error> { let mut coords = BufWriter::new(File::create("/tmp/parcels")?); let mut parcel_ids = Vec::new(); @@ -16,7 +62,7 @@ pub fn import_parcels(path: &str) -> Result, failure::Er parcel_ids.push(rec[15].to_string()); coords.write_fmt(format_args!("{} {}\n", &rec[25], &rec[26]))?; // TODO convert it all - if parcel_ids.len() == 100 { + if parcel_ids.len() == 10000 { break; } } @@ -48,7 +94,6 @@ pub fn import_parcels(path: &str) -> Result, failure::Er let lat: f64 = pieces[1].parse()?; let pt = LonLat::new(lon, lat); if bounds.contains(pt) { - println!("parcel {} is at {}", id, pt); result.insert(id, pt); } }