Skip to content

Commit

Permalink
Bring in the overpass importer
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 11, 2023
1 parent 4cc27bb commit 2082a35
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 63 deletions.
103 changes: 103 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"vite-plugin-wasm-pack": "0.1.11"
},
"dependencies": {
"@mapbox/mapbox-gl-draw": "^1.4.3",
"@turf/bbox": "^6.5.0",
"svelte-maplibre": "^0.7.3"
}
Expand Down
79 changes: 20 additions & 59 deletions web/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<script lang="ts">
import turfBbox from "@turf/bbox";
import init, { MapModel } from "backend";
import { onMount } from "svelte";
import { GeoJSON, LineLayer, MapLibre, Marker, Popup } from "svelte-maplibre";
import inputUrl from "../assets/input.pbf?url";
import { colorScale, limits } from "./colors";
import { Layout, Legend, Loading, SequentialLegend } from "./common";
import { Layout, Legend, SequentialLegend } from "./common";
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";
let model: MapModel | undefined = undefined;
let map;
let loading = false;
let mode = "score";
Expand All @@ -23,35 +20,24 @@
let route_err = "";
let opacity = 100;
let showSeverances = true;
let showBasemap = false;
onMount(async () => {
await init();
try {
loading = true;
let resp = await fetch(inputUrl);
loadModel(await resp.arrayBuffer());
} catch (err) {
window.alert(`Couldn't open from URL ${inputUrl}: ${err}`);
}
loading = false;
});
function lerp(pct, a, b) {
return a + pct * (b - a);
}
let fileInput: HTMLInputElement;
async function loadFile(e: Event) {
try {
loading = true;
loadModel(await fileInput.files![0].arrayBuffer());
} catch (err) {
window.alert(`Couldn't open this file: ${err}`);
function zoomToFit() {
if (map && model) {
// TODO wasteful
let bbox = turfBbox(JSON.parse(model.render()));
map.fitBounds(bbox, { animate: false });
}
loading = false;
}
function loadModel(buffer: Buffer) {
console.time("load");
model = new MapModel(new Uint8Array(buffer));
console.timeEnd("load");
function gotModel(_m) {
if (!model) {
return;
}
console.log("New map model loaded");
zoomToFit();
let bbox = turfBbox(JSON.parse(model.render()));
route_a = {
Expand All @@ -63,18 +49,7 @@
lat: lerp(0.6, bbox[1], bbox[3]),
};
}
function lerp(pct, a, b) {
return a + pct * (b - a);
}
function zoomToFit() {
if (map && model) {
// TODO wasteful
let bbox = turfBbox(JSON.parse(model.render()));
map.fitBounds(bbox, { animate: false });
}
}
$: gotModel(model);
$: if (model && route_a && route_b) {
try {
Expand All @@ -96,9 +71,9 @@

<Layout>
<div slot="left">
<label>
<input bind:this={fileInput} on:change={loadFile} type="file" />
</label>
{#if map}
<MapLoader {map} bind:model />
{/if}
<div><button on:click={zoomToFit}>Zoom to fit</button></div>

<label>
Expand Down Expand Up @@ -141,23 +116,10 @@
{:else if mode == "score"}
<SequentialLegend {colorScale} {limits} />
{/if}

<hr />

<label>
<input type="checkbox" bind:checked={showBasemap} />
Show basemap
</label>
</div>
<div slot="main" style="position:relative; width: 100%; height: 100vh;">
<MapLibre
style={showBasemap
? "https://api.maptiler.com/maps/dataviz/style.json?key=MZEJTanw3WpxRvt7qDfo"
: {
version: 8,
sources: {},
layers: [],
}}
style="https://api.maptiler.com/maps/dataviz/style.json?key=MZEJTanw3WpxRvt7qDfo"
standardControls
hash
bind:map
Expand All @@ -173,4 +135,3 @@
</MapLibre>
</div>
</Layout>
<Loading {loading} />
69 changes: 69 additions & 0 deletions web/src/MapLoader.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<script lang="ts">
import turfBbox from "@turf/bbox";
import init, { MapModel } from "backend";
import { onMount } from "svelte";
import inputUrl from "../assets/input.pbf?url";
import { Loading, OverpassSelector } from "./common";
export let model: MapModel | undefined = undefined;
export let map;
let msg = null;
onMount(async () => {
await init();
try {
msg = `Downloading ${inputUrl}`;
let resp = await fetch(inputUrl);
loadModel(await resp.arrayBuffer());
} catch (err) {
window.alert(`Couldn't open from URL ${inputUrl}: ${err}`);
}
msg = null;
});
let fileInput: HTMLInputElement;
async function loadFile(e: Event) {
try {
loadModel(await fileInput.files![0].arrayBuffer());
} catch (err) {
window.alert(`Couldn't open this file: ${err}`);
}
msg = null;
}
function loadModel(buffer: Buffer) {
msg = "Building map model from OSM input";
console.time("load");
model = new MapModel(new Uint8Array(buffer));
console.timeEnd("load");
}
function gotXml(e: CustomEvent<string>) {
try {
// TODO Can we avoid turning into bytes?
loadModel(new TextEncoder().encode(e.detail));
} catch (err) {
window.alert(`Couldn't import from Overpass: ${err}`);
}
msg = null;
}
</script>

<Loading {msg} />

<div style="border: 1px solid black; padding: 8px;">
<div>
<label>
Load an osm.xml or a .pbf file:
<input bind:this={fileInput} on:change={loadFile} type="file" />
</label>
</div>

<OverpassSelector
{map}
on:gotXml={gotXml}
on:loading={(e) => (msg = e.detail)}
on:error={(e) => window.alert(e.detail)}
/>
</div>
2 changes: 1 addition & 1 deletion web/src/ScoreLayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@
>
</LineLayer>
</GeoJSON>
<RouteLayer {route_gj} route_a={null} route_b={null} />
<RouteLayer {route_gj} route_a={null} route_b={null} {map} />
6 changes: 3 additions & 3 deletions web/src/common/Loading.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
export let loading = false;
export let msg: string | null = null;
</script>

{#if loading}
<div>Loading</div>
{#if msg}
<div>{msg}</div>
{/if}

<style>
Expand Down
Loading

0 comments on commit 2082a35

Please sign in to comment.