Skip to content

Commit

Permalink
Use the TS wrapper to conveniently do setCellColors
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jan 4, 2025
1 parent e013d99 commit d87345f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 55 deletions.
2 changes: 1 addition & 1 deletion web/src/DebugMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<div slot="map">
<RenderNeighbourhood
gjInput={notNull($backend).renderNeighbourhood()}
gj={notNull($backend).renderNeighbourhood()}
interactive
onClickLine={(f, _) => window.open(notNull(f.properties).way, "_blank")}
>
Expand Down
3 changes: 1 addition & 2 deletions web/src/ImpactOneDestinationMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
} from "svelte-utils/map";
import { SplitComponent } from "svelte-utils/top_bar_layout";
import BackButton from "./BackButton.svelte";
import { setCellColors } from "./cells";
import { layerId, Link } from "./common";
import ModalFilterLayer from "./ModalFilterLayer.svelte";
import {
Expand Down Expand Up @@ -96,7 +95,7 @@
</div>

<div slot="map">
<GeoJSON data={setCellColors(notNull($backend).renderNeighbourhood())}>
<GeoJSON data={notNull($backend).renderNeighbourhood()}>
<FillLayer
{...layerId("cells")}
filter={["==", ["get", "kind"], "cell"]}
Expand Down
6 changes: 2 additions & 4 deletions web/src/RenderNeighbourhood.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
hoverStateFilter,
LineLayer,
} from "svelte-maplibre";
import { setCellColors } from "./cells";
import { layerId } from "./common";
import OneWayLayer from "./OneWayLayer.svelte";
import type { RenderNeighbourhoodOutput } from "./wasm";
export let gjInput: RenderNeighbourhoodOutput;
export let gj: RenderNeighbourhoodOutput;
// When disabled, can't click lines or filters, no slots, no hoverCursor
export let interactive = true;
export let onClickLine = (f: Feature, pt: LngLat) => {};
$: gj = setCellColors(gjInput);
$: maxShortcuts =
Math.max(
...gjInput.features.map((f) =>
...gj.features.map((f) =>
f.properties.kind == "interior_road" ? f.properties.shortcuts : 0,
),
) ?? 0;
Expand Down
2 changes: 1 addition & 1 deletion web/src/RouteMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<div slot="map">
{#if prevMode == "neighbourhood"}
<RenderNeighbourhood
gjInput={notNull($backend).renderNeighbourhood()}
gj={notNull($backend).renderNeighbourhood()}
interactive={false}
/>
{/if}
Expand Down
5 changes: 2 additions & 3 deletions web/src/ViewShortcutsMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { Popup } from "svelte-utils/map";
import { SplitComponent } from "svelte-utils/top_bar_layout";
import BackButton from "./BackButton.svelte";
import { setCellColors } from "./cells";
import { layerId, Link } from "./common";
import ModalFilterLayer from "./ModalFilterLayer.svelte";
import RenderNeighbourhood from "./RenderNeighbourhood.svelte";
Expand Down Expand Up @@ -150,7 +149,7 @@
<div slot="map">
{#if state.state == "neutral"}
<RenderNeighbourhood
gjInput={notNull($backend).renderNeighbourhood()}
gj={notNull($backend).renderNeighbourhood()}
onClickLine={choseRoad}
>
<div slot="line-popup">
Expand All @@ -162,7 +161,7 @@
</div>
</RenderNeighbourhood>
{:else if state.state == "chose-road"}
<GeoJSON data={setCellColors(notNull($backend).renderNeighbourhood())}>
<GeoJSON data={notNull($backend).renderNeighbourhood()}>
<FillLayer
{...layerId("cells")}
filter={["==", ["get", "kind"], "cell"]}
Expand Down
35 changes: 0 additions & 35 deletions web/src/cells.ts

This file was deleted.

16 changes: 8 additions & 8 deletions web/src/edit/NeighbourhoodMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
{ name: string; waypoints: Waypoint[] }
> | null;
let gjInput: RenderNeighbourhoodOutput;
let gj: RenderNeighbourhoodOutput;
let allShortcuts = $backend!.getAllShortcuts();
$: rerender($mutationCounter);
$: numDisconnectedCells = gjInput.features.filter(
$: numDisconnectedCells = gj.features.filter(
(f) =>
f.properties.kind == "cell" && f.properties.cell_color == "disconnected",
).length;
Expand All @@ -58,12 +58,12 @@
});
function rerender(_x: number) {
gjInput = $backend!.renderNeighbourhood();
gj = $backend!.renderNeighbourhood();
// @ts-expect-error TS can't figure out that we're narrowing the case here
boundary = gjInput.features.find((f) => f.properties.kind == "boundary")!;
boundary = gj.features.find((f) => f.properties.kind == "boundary")!;
undoLength = gjInput.undo_length;
redoLength = gjInput.redo_length;
undoLength = gj.undo_length;
redoLength = gj.redo_length;
allShortcuts = $backend!.getAllShortcuts();
Expand Down Expand Up @@ -206,7 +206,7 @@
<div slot="sidebar">
<p>
Editing neighbourhood <u>{notNull(boundary).properties.name}</u>
, with an area of {gjInput.area_km2.toFixed(1)} km²
, with an area of {gj.area_km2.toFixed(1)} km²
</p>

{#if numDisconnectedCells > 0}
Expand Down Expand Up @@ -287,7 +287,7 @@

<div slot="map">
<RenderNeighbourhood
{gjInput}
{gj}
interactive={action == "filter" || action == "oneway"}
{onClickLine}
>
Expand Down
38 changes: 37 additions & 1 deletion web/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ export class Backend {
return JSON.parse(this.inner.renderModalFilters());
}

// This adds a 'color' property to all cells. It's nicer to keep this on the
// frontend, since it's about styling.
renderNeighbourhood(): RenderNeighbourhoodOutput {
return JSON.parse(this.inner.renderNeighbourhood());
return setCellColors(JSON.parse(this.inner.renderNeighbourhood()));
}

renderAutoBoundaries(): FeatureCollection {
Expand Down Expand Up @@ -181,3 +183,37 @@ export type AllShortcuts = FeatureCollection<
LineString,
{ directness: number; length_meters: number }
>;

// Sets a 'color' property on any cell polygons. Idempotent.
function setCellColors(
gj: RenderNeighbourhoodOutput,
): RenderNeighbourhoodOutput {
// A qualitative palette from colorbrewer2.org, skipping the red hue (used
// for levels of shortcutting) and grey (too close to the basemap)
let cell_colors = [
"#8dd3c7",
"#ffffb3",
"#bebada",
"#80b1d3",
"#fdb462",
"#b3de69",
"#fccde5",
"#bc80bd",
"#ccebc5",
"#ffed6f",
];

for (let f of gj.features) {
if (f.properties.kind != "cell") {
continue;
}
if (f.properties.cell_color == "disconnected") {
f.properties.color = "red";
} else {
f.properties.color =
cell_colors[f.properties.cell_color % cell_colors.length];
}
}

return gj;
}

0 comments on commit d87345f

Please sign in to comment.