Skip to content

Commit

Permalink
Update cost in the edge cost page
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Nov 5, 2023
1 parent 0011689 commit de701af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
60 changes: 40 additions & 20 deletions viewer/src/EdgeCostApp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
} from "svelte-maplibre";
import init, { JsNetwork } from "wasm-od2net";
import { colorByLts, colors, colorScale, makeColorRamp } from "./common";
import CostFunction from "./CostFunction.svelte";
import Header from "./Header.svelte";
import Layout from "./Layout.svelte";
import Legend from "./Legend.svelte";
Expand All @@ -28,7 +29,9 @@
type: "FeatureCollection",
features: [],
};
let colorBy: "lts" | "cost" | "nearby_amenities" = "lts";
// TODO When we load a network.bin, overwrite this
let cost = "Distance";
let colorBy: "lts" | "cost" | "nearby_amenities" = "cost";
// Note the 0th entry is "not allowed"; it won't be filled out at all
let percentByLength = [0, 0, 0, 0, 0];
Expand All @@ -46,26 +49,29 @@
],
{ padding: 20, animate: false }
);
gj = JSON.parse(network.debugNetwork());
let allSum = 0;
let ltsSum = [0, 0, 0, 0, 0];
for (let f of gj.features) {
// A "not allowed" edge without a cost or length
if (!f.properties.length) {
continue;
}
allSum += f.properties.length;
ltsSum[f.properties.lts] += f.properties.length;
}
percentByLength = ltsSum.map((x) => (x / allSum) * 100);
console.log({ allSum, ltsSum, percentByLength });
updateGj();
} catch (err) {
window.alert(`Problem loading network file: ${err}`);
}
}
function updateGj() {
gj = JSON.parse(network.debugNetwork());
let allSum = 0;
let ltsSum = [0, 0, 0, 0, 0];
for (let f of gj.features) {
// A "not allowed" edge without a cost or length
if (!f.properties.length) {
continue;
}
allSum += f.properties.length;
ltsSum[f.properties.lts] += f.properties.length;
}
percentByLength = ltsSum.map((x) => (x / allSum) * 100);
}
function openOSM(feature) {
let id = feature.properties.way;
window.open(`http://openstreetmap.org/way/${id}`, "_blank");
Expand Down Expand Up @@ -108,6 +114,14 @@
}
return result;
}
function updateCost(cost) {
if (network) {
network.updateCostFunction(cost);
updateGj();
}
}
$: updateCost(cost);
</script>

<Layout>
Expand All @@ -117,12 +131,16 @@
Open a <i>.bin</i> network file or an <i>.osm.pbf</i>
<input bind:this={fileInput} on:change={fileLoaded} type="file" />
</label>
<hr />
<div>
<select bind:value={colorBy}>
<option value="lts">LTS</option>
<option value="cost">Edge cost (relative to length)</option>
<option value="nearby_amenities">Nearby amenities</option>
</select>
<label>
Color edges by:
<select bind:value={colorBy}>
<option value="lts">LTS</option>
<option value="cost">Edge cost (relative to length)</option>
<option value="nearby_amenities">Nearby amenities</option>
</select>
</label>
</div>
{#if colorBy == "lts"}
<Legend
Expand Down Expand Up @@ -154,6 +172,8 @@
{:else}
<SequentialLegend {colorScale} limits={limitsFor(colorBy)} />
{/if}
<hr />
<CostFunction bind:cost />
</div>
<div slot="main" style="position:relative; width: 100%; height: 100vh;">
<MapLibre
Expand Down
10 changes: 10 additions & 0 deletions wasm-od2net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ impl JsNetwork {
Ok(gj_string)
}

#[wasm_bindgen(js_name = updateCostFunction)]
pub fn update_cost_function(&mut self, input: JsValue) -> Result<(), JsValue> {
let cost: CostFunction = serde_wasm_bindgen::from_value(input)?;
self.last_cost = cost;
self.network.recalculate_cost(&self.last_cost);
// Doesn't touch the CH, because this is only meant to be used in the edge cost app, which
// doesn't use the CH
Ok(())
}

#[wasm_bindgen(js_name = getBounds)]
pub fn get_bounds(&self) -> Vec<f64> {
let mut bounds = vec![f64::MAX, f64::MAX, f64::MIN, f64::MIN];
Expand Down

0 comments on commit de701af

Please sign in to comment.