From 8717d73d924180bec05dfc8d37062652dea78c68 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 6 Nov 2023 13:14:10 +0000 Subject: [PATCH] Make a cost function by LTS, which is really what we want in most cases so far --- Cargo.lock | 1 + od2net/src/config.rs | 8 +++ od2net/src/network/create_from_osm.rs | 2 +- od2net/src/plugins/cost.rs | 22 +++++++ viewer/src/CostFunction.svelte | 30 ++++++++- viewer/src/EdgeCostApp.svelte | 94 +++++++++++++++------------ viewer/src/SidebarControls.svelte | 12 ++-- viewer/src/common.ts | 7 ++ wasm-od2net/Cargo.toml | 1 + wasm-od2net/src/lib.rs | 1 + 10 files changed, 126 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b01d89..5c49f3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1122,6 +1122,7 @@ dependencies = [ "rstar", "serde", "serde-wasm-bindgen 0.6.1", + "serde_json", "wasm-bindgen", "web-sys", ] diff --git a/od2net/src/config.rs b/od2net/src/config.rs index a3e8c75..9223776 100644 --- a/od2net/src/config.rs +++ b/od2net/src/config.rs @@ -62,6 +62,14 @@ pub enum CostFunction { Distance, /// Heavily penalize main roads AvoidMainRoads, + /// Multiply distance by a factor for each LTS classification + ByLTS { + lts1: f64, + lts2: f64, + lts3: f64, + lts4: f64, + // TODO Incorporate nearby_amenities. Maybe a list of ranges and then a multiplier? + }, /// Multiply distance by a factor based on the OSM highway tag. If the type isn't present, it /// won't be allowed at all. OsmHighwayType(HashMap), diff --git a/od2net/src/network/create_from_osm.rs b/od2net/src/network/create_from_osm.rs index 4e013d5..e7253d5 100644 --- a/od2net/src/network/create_from_osm.rs +++ b/od2net/src/network/create_from_osm.rs @@ -173,7 +173,7 @@ fn scrape_elements( greenspace_polygons.push(polygon); } - // TODO Improve filtering + // Include everything here, and let LTS::NotAllowed later filter some out if tags.has("highway") { ways.insert( way.id(), diff --git a/od2net/src/plugins/cost.rs b/od2net/src/plugins/cost.rs index 79e067d..a603f61 100644 --- a/od2net/src/plugins/cost.rs +++ b/od2net/src/plugins/cost.rs @@ -17,6 +17,15 @@ pub fn calculate_batch(cost: &CostFunction, input_batch: Vec<&Edge>) -> Vec input_batch + .into_iter() + .map(|e| by_lts(e, *lts1, *lts2, *lts3, *lts4)) + .collect(), CostFunction::ExternalCommand(command) => external_command(command, input_batch).unwrap(), } } @@ -57,6 +66,19 @@ fn osm_highway_type(edge: &Edge, weights: &HashMap) -> Option Option { + let weight = match edge.lts { + LTS::NotAllowed => { + return None; + } + LTS::LTS1 => lts1, + LTS::LTS2 => lts2, + LTS::LTS3 => lts3, + LTS::LTS4 => lts4, + }; + Some((weight * edge.length_meters).round() as usize) +} + fn external_command(command: &str, input_batch: Vec<&Edge>) -> Result>> { let args: Vec<&str> = command.split(" ").collect(); diff --git a/viewer/src/CostFunction.svelte b/viewer/src/CostFunction.svelte index e9382b4..fc5a299 100644 --- a/viewer/src/CostFunction.svelte +++ b/viewer/src/CostFunction.svelte @@ -1,9 +1,18 @@