From 147094d0592a1289fb1e00f7c5a45339d5c1cd22 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Sun, 18 Feb 2024 11:41:16 -0500 Subject: [PATCH] code done demoing for video --- BBB_frontend/main.js | 1 + Cargo.lock | 28 ++++++++++++++++++ Cargo.toml | 1 + README.md | 7 +++-- src/main.rs | 1 + src/route.rs | 2 +- src/stop.rs | 69 +++++++++++++++++++++++++++----------------- 7 files changed, 79 insertions(+), 30 deletions(-) diff --git a/BBB_frontend/main.js b/BBB_frontend/main.js index dcf0e40..66608c9 100644 --- a/BBB_frontend/main.js +++ b/BBB_frontend/main.js @@ -105,6 +105,7 @@ async function submit(clickEvent){ return {path:google.maps.geometry.encoding.decodePath(v.poly_line), color:v.color} }) .then(o=>{ + //console.log(`"${o.path}"`); let line = new google.maps.Polyline({ path:o.path, strokeColor: o.color, diff --git a/Cargo.lock b/Cargo.lock index 15c1237..97fe92d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,15 @@ version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + [[package]] name = "arrayref" version = "0.3.7" @@ -494,6 +503,7 @@ dependencies = [ "anyhow", "async-std", "chrono", + "geo-types", "reqwest", "serde", "serde_json", @@ -920,6 +930,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "geo-types" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567495020b114f1ce9bed679b29975aa0bfae06ac22beacd5cfde5dabe7b05d6" +dependencies = [ + "approx", + "num-traits", + "serde", +] + [[package]] name = "getrandom" version = "0.1.16" @@ -1247,6 +1268,12 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + [[package]] name = "linux-raw-sys" version = "0.3.8" @@ -1326,6 +1353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", + "libm", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 49825a2..30bdb3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" anyhow = "1.0.79" async-std = { version = "1.12.0", features = ["tokio1", "attributes"] } chrono = "0.4.34" +geo-types = "0.7.12" reqwest = "0.11.24" #sea-orm = "0.12.14" serde = { version = "1.0.196", features = ["derive"] } diff --git a/README.md b/README.md index 35d3870..d7db471 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ to run you need a file called GOOGLE_API_KEY in the dir the app is run in which ## TODO: -- test with BC buses +- show lines for broome county buses - Make buttons change color to indicate toggle - Prevent buses from going backwards - make time estimate accurate (beyond hack divide by 3) @@ -16,11 +16,12 @@ to run you need a file called GOOGLE_API_KEY in the dir the app is run in which - Trim poly lines down to the bit between bus stops - Fix the UI so console and alert aren't needed - refactor to work within docker and setup within docker-compose -- make route checking more advanced with better walking heuristic +- make route checking more advanced with better walking heuristic (ideally taking the river into account) - make route checking more advanced by allowing multiple factors with multiple weights including - walking time (don't remove it) - bus travel time - - layover time + - layover time (both waiting for the initial bus and any intermediary buses) - ultimate arrival time +- make route checking allow for specifying a future time - use a DB via SeaORM to avoid spaming APIs - use info in db to try and predict bus schedules in future diff --git a/src/main.rs b/src/main.rs index 817e583..7edf484 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate reqwest; extern crate tide; extern crate anyhow; extern crate chrono; +extern crate geo_types; //extern crate sea_orm; mod state; diff --git a/src/route.rs b/src/route.rs index cfc0af2..10f5ff9 100644 --- a/src/route.rs +++ b/src/route.rs @@ -83,7 +83,7 @@ pub async fn broome_county_routes_get()->anyhow::Result>{ let poly_line = crate::stop::poly_encode_stops(stops.clone()); Route{ id:route.id, - color:route.color, + color:"#".to_owned()+&route.color, name:route.name, poly_line, short_name: route.short_name, diff --git a/src/stop.rs b/src/stop.rs index b586a9d..4c8bd24 100644 --- a/src/stop.rs +++ b/src/stop.rs @@ -68,39 +68,56 @@ pub async fn bc_stops_get()->anyhow::Result>{ let stops:Vec = serde_json::from_str(body.as_str())?; Ok(stops.into_iter().map(Stop::from).collect()) } -pub fn poly_encode_stops>(stops:I)->String{ +pub fn poly_encode_stops(stops:Vec)->String{ + println!("{:?}",stops); stops.into_iter() .map(|stop|{ - enc_float(stop.lat) + "".to_owned()//enc_float(stop.lat) + &enc_float(stop.lon) }) .reduce(|s1,s2|s1+&s2).unwrap_or("".into()) } -fn enc_float(num:f64)->String{ - let mut working:i32 = (num*1e5).round() as i32; - //hopethis does what's needed - working<<=1; - if num < 0.0 { - working = !working; +fn enc_float(num:f64)->String{ + + let working:i32 = invoke_e5(num); + let working = two_comp(working,num); + let working = lshf(working); + let working = if_negative_invert(working,num); + let chunks = chunks(working); + chunks.iter().map(|v| char::from_u32(*v as u32).unwrap()).collect() +} + +fn invoke_e5(n:f64)->i32{ + (n*1e5).round() as i32 +} + +fn two_comp(n:i32, orig:f64)->i32{ + if orig < 0.0{ + n^-1 + 1 } - let mut bits:[bool;30] = [false;30]; - for i in 0..30{ - bits[i] = working % 2 == 1; - working >>=1; + else { + n } - bits.chunks(5).rev() - .map(|bools|{ - let mut accu:u8 = 0; - for i in 0..5{ - accu += if bools[4-i]{ - 1 - } else {0}; - accu <<=1; - } - accu |= 0x20; - accu +=63; - char::from(accu) - }).collect::() - +} + +fn lshf(n:i32)->i32{ + n << 1 +} +fn if_negative_invert(n:i32, orig:f64)->i32{ + if orig < 0.0{ + n ^ -1 + } + else { + n + } +} +fn chunks(n:i32)->[u8;6]{ + let mut chunks:[u8;6] = [0;6]; + for i in 0..6{ + chunks[i] = (n >> (5*i) ) as u8 & 0x1f; + if i != 5 {chunks[i] |= 0x20;} + chunks[i] += 63; + } + chunks } #[derive(serde::Serialize)]