Skip to content

Commit

Permalink
Use SplitComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jan 19, 2024
1 parent 66a8034 commit 4f16a63
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 252 deletions.
61 changes: 6 additions & 55 deletions web/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import type { FeatureCollection } from "geojson";
import type { LngLat, Map } from "maplibre-gl";
import { MapLibre } from "svelte-maplibre";
import { colorScale, kindToColor, limits } from "./colors";
import { Layout, Legend, SequentialLegend } from "./common";
import { kindToColor } from "./colors";
import { Layout, Legend } from "./common";
import PolygonToolLayer from "./common/draw_polygon/PolygonToolLayer.svelte";
import Directions from "./Directions.svelte";
import MapLoader from "./MapLoader.svelte";
import NetworkLayer from "./NetworkLayer.svelte";
import RouteLayer from "./RouteLayer.svelte";
import ScoreLayer from "./ScoreLayer.svelte";
import RouteMode from "./RouteMode.svelte";
import ScoreMode from "./ScoreMode.svelte";
import {
mapContents,
map as mapStore,
Expand All @@ -27,18 +25,9 @@
mapStore.set(map);
}
// TODO all gonna move
let route_a: LngLat | null = null;
let route_b: LngLat | null = null;
let route_gj: FeatureCollection | null = null;
let route_err = "";
let opacity = 100;
let showSeverances = true;
function lerp(pct: number, a: number, b: number): number {
return a + pct * (b - a);
}
function zoomToFit() {
if (map && $model) {
// TODO wasteful
Expand All @@ -53,35 +42,9 @@
}
console.log("New map model loaded");
zoomToFit();
let bbox = turfBbox(JSON.parse($model.render()));
route_a = {
lng: lerp(0.4, bbox[0], bbox[2]),
lat: lerp(0.4, bbox[1], bbox[3]),
};
route_b = {
lng: lerp(0.6, bbox[0], bbox[2]),
lat: lerp(0.6, bbox[1], bbox[3]),
};
}
$: gotModel($model);
$: if ($model && route_a && route_b) {
try {
route_gj = JSON.parse(
$model.compareRoute({
x1: route_a.lng,
y1: route_a.lat,
x2: route_b.lng,
y2: route_b.lat,
})
);
route_err = "";
} catch (err) {
route_gj = null;
route_err = err.toString();
}
}
let sidebarDiv: HTMLDivElement;
let mapDiv: HTMLDivElement;
$: if (sidebarDiv && $sidebarContents) {
Expand Down Expand Up @@ -132,17 +95,6 @@
<input type="range" min="0" max="100" bind:value={opacity} />
</label>
</div>

{#if $mode == "route"}
{#if route_err}
<p>{route_err}</p>
{/if}
{#if route_gj}
<Directions {route_gj} />
{/if}
{:else if $mode == "score"}
<SequentialLegend {colorScale} {limits} />
{/if}
</div>
<div slot="main" style="position:relative; width: 100%; height: 100vh;">
<MapLibre
Expand All @@ -156,10 +108,9 @@
<PolygonToolLayer />
{#if $model}
{#if $mode == "route"}
<NetworkLayer {showSeverances} {opacity} />
<RouteLayer bind:route_a bind:route_b {route_gj} />
<RouteMode {showSeverances} {opacity} />
{:else if $mode == "score"}
<ScoreLayer {showSeverances} {opacity} />
<ScoreMode {showSeverances} {opacity} />
{/if}
{/if}
</MapLibre>
Expand Down
41 changes: 0 additions & 41 deletions web/src/NetworkLayer.svelte

This file was deleted.

51 changes: 0 additions & 51 deletions web/src/RouteLayer.svelte

This file was deleted.

139 changes: 139 additions & 0 deletions web/src/RouteMode.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<script lang="ts">
import turfBbox from "@turf/bbox";
import type { FeatureCollection } from "geojson";
import type { LngLat, Map, MapMouseEvent } from "maplibre-gl";
import { onDestroy, onMount } from "svelte";
import {
GeoJSON,
hoverStateFilter,
LineLayer,
Marker,
Popup,
} from "svelte-maplibre";
import { kindToColor } from "./colors";
import { constructMatchExpression, PropertiesTable } from "./common";
import Directions from "./Directions.svelte";
import SplitComponent from "./SplitComponent.svelte";
import { map, model } from "./stores";
// TODO Use filter expressions?
export let showSeverances: boolean;
export let opacity: number;
// TODO Maybe need to do this when model changes
let bbox = turfBbox(JSON.parse($model.render()));
let route_a: LngLat = {
lng: lerp(0.4, bbox[0], bbox[2]),
lat: lerp(0.4, bbox[1], bbox[3]),
};
let route_b: LngLat = {
lng: lerp(0.6, bbox[0], bbox[2]),
lat: lerp(0.6, bbox[1], bbox[3]),
};
// TODO or empty
let route_gj: FeatureCollection | null = null;
let route_err = "";
$: if (route_a && route_b) {
try {
route_gj = JSON.parse(
$model.compareRoute({
x1: route_a.lng,
y1: route_a.lat,
x2: route_b.lng,
y2: route_b.lat,
})
);
route_err = "";
} catch (err) {
route_gj = null;
route_err = err.toString();
}
}
onMount(() => {
$map?.on("contextmenu", onRightClick);
});
onDestroy(() => {
$map?.off("contextmenu", onRightClick);
});
function onRightClick(e: MapMouseEvent) {
// Move the first marker, for convenience
route_a = e.lngLat;
}
function lerp(pct: number, a: number, b: number): number {
return a + pct * (b - a);
}
</script>

<SplitComponent>
<div slot="sidebar">
{#if route_err}
<p>{route_err}</p>
{/if}
{#if route_gj}
<Directions {route_gj} />
{/if}
</div>
<div slot="map">
<GeoJSON data={JSON.parse($model.render())} generateId>
<LineLayer
id="network"
paint={{
"line-width": hoverStateFilter(5, 7),
"line-color": constructMatchExpression(
["get", "kind"],
kindToColor,
"yellow"
),
"line-opacity": showSeverances
? opacity / 100
: constructMatchExpression(
["get", "kind"],
{
Severance: 0.0,
},
opacity / 100.0
),
}}
manageHoverState
on:click={(e) =>
window.open(e.detail.features[0].properties.way, "_blank")}
hoverCursor="pointer"
>
<Popup openOn="hover" let:data>
<PropertiesTable properties={data.properties} />
</Popup>
</LineLayer>
</GeoJSON>

<Marker bind:lngLat={route_a} draggable><span class="dot">A</span></Marker>
<Marker bind:lngLat={route_b} draggable><span class="dot">B</span></Marker>
{#if route_gj}
<GeoJSON data={route_gj}>
<LineLayer
id="route"
beforeId="network"
paint={{
"line-width": 20,
"line-color": "cyan",
"line-opacity": 0.5,
}}
/>
</GeoJSON>
{/if}
</div>
</SplitComponent>

<style>
.dot {
width: 30px;
height: 30px;
border-radius: 50%;
display: inline-block;
background-color: grey;
text-align: center;
}
</style>
Loading

0 comments on commit 4f16a63

Please sign in to comment.