Skip to content

Commit f13da79

Browse files
committed
Tease apart RenderNeighbourhood, so we can also show cells in shortcut mode
1 parent 5d52ae1 commit f13da79

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

web/src/RenderNeighbourhood.svelte

+5-37
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,17 @@
22
import type { Feature, FeatureCollection } from "geojson";
33
import { FillLayer, GeoJSON, LineLayer } from "svelte-maplibre";
44
import { showBasemap } from "./stores";
5+
import { setCellColors } from "./cells";
56
67
export let gjInput: FeatureCollection;
78
// When disabled, can't click lines or filters, no slots, no hoverCursor
89
export let interactive = true;
910
export let onClickLine = (f: Feature) => {};
1011
11-
let gj: FeatureCollection;
12-
let maxShortcuts: number;
13-
// TODO if we could set both reactively, thatd be ideal
14-
$: render(gjInput);
15-
16-
function render(x: FeatureCollection) {
17-
// A qualitative palette from colorbrewer2.org, skipping the red hue (used
18-
// for levels of shortcutting) and grey (too close to the basemap)
19-
let cell_colors = [
20-
"#8dd3c7",
21-
"#ffffb3",
22-
"#bebada",
23-
"#80b1d3",
24-
"#fdb462",
25-
"#b3de69",
26-
"#fccde5",
27-
"#bc80bd",
28-
"#ccebc5",
29-
"#ffed6f",
30-
];
31-
32-
maxShortcuts = Math.max(
33-
...gjInput.features.map((f) => f.properties!.shortcuts ?? 0),
34-
);
35-
36-
for (let f of gjInput.features) {
37-
f.properties ??= {};
38-
if (f.properties.cell_color == "disconnected") {
39-
f.properties.color = "red";
40-
} else if (Object.hasOwn(f.properties, "cell_color")) {
41-
f.properties.color =
42-
cell_colors[f.properties.cell_color % cell_colors.length];
43-
}
44-
}
45-
46-
gj = gjInput;
47-
}
12+
$: gj = setCellColors(gjInput);
13+
$: maxShortcuts = Math.max(
14+
...gjInput.features.map((f) => f.properties!.shortcuts ?? 0),
15+
);
4816
</script>
4917

5018
<GeoJSON data={gj} generateId>

web/src/ViewShortcutsMode.svelte

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<script lang="ts">
2+
import { setCellColors } from "./cells";
23
import type { Feature, FeatureCollection } from "geojson";
34
import { onDestroy, onMount } from "svelte";
4-
import { GeoJSON, LineLayer } from "svelte-maplibre";
5+
import { FillLayer, GeoJSON, LineLayer } from "svelte-maplibre";
56
import { notNull, Popup } from "./common";
67
import ModalFilterLayer from "./ModalFilterLayer.svelte";
78
import RenderNeighbourhood from "./RenderNeighbourhood.svelte";
89
import SplitComponent from "./SplitComponent.svelte";
9-
import { app, map, mode } from "./stores";
10+
import { app, map, mode, showBasemap } from "./stores";
1011
1112
type State =
1213
| {
@@ -122,6 +123,19 @@
122123
</div>
123124
</RenderNeighbourhood>
124125
{:else if state.state == "chose-road"}
126+
<GeoJSON
127+
data={setCellColors(JSON.parse(notNull($app).renderNeighbourhood()))}
128+
>
129+
<FillLayer
130+
beforeId={$showBasemap ? "Building" : undefined}
131+
filter={["==", ["get", "kind"], "cell"]}
132+
paint={{
133+
"fill-color": ["get", "color"],
134+
"fill-opacity": 0.3,
135+
}}
136+
/>
137+
</GeoJSON>
138+
125139
<GeoJSON data={state.gj.features[state.shortcutIndex]}>
126140
<LineLayer
127141
paint={{

web/src/cells.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { FeatureCollection } from "geojson";
2+
3+
// Sets a 'color' property on any cell polygons. Idempotent.
4+
export function setCellColors(gj: FeatureCollection): FeatureCollection {
5+
// A qualitative palette from colorbrewer2.org, skipping the red hue (used
6+
// for levels of shortcutting) and grey (too close to the basemap)
7+
let cell_colors = [
8+
"#8dd3c7",
9+
"#ffffb3",
10+
"#bebada",
11+
"#80b1d3",
12+
"#fdb462",
13+
"#b3de69",
14+
"#fccde5",
15+
"#bc80bd",
16+
"#ccebc5",
17+
"#ffed6f",
18+
];
19+
20+
for (let f of gj.features) {
21+
f.properties ??= {};
22+
if (f.properties.cell_color == "disconnected") {
23+
f.properties.color = "red";
24+
} else if (Object.hasOwn(f.properties, "cell_color")) {
25+
f.properties.color =
26+
cell_colors[f.properties.cell_color % cell_colors.length];
27+
}
28+
}
29+
30+
return gj;
31+
}

0 commit comments

Comments
 (0)