diff --git a/backend/src/graph.rs b/backend/src/graph.rs index 4f6f74d..abf6615 100644 --- a/backend/src/graph.rs +++ b/backend/src/graph.rs @@ -105,6 +105,16 @@ impl Graph { Ok(out) } + pub fn render_amenities(&self) -> Result { + let mut features = Vec::new(); + for a in &self.amenities { + features.push(a.to_gj(&self.mercator)); + } + let gj = GeoJson::from(features); + let out = serde_json::to_string(&gj)?; + Ok(out) + } + /// Return a polygon covering the world, minus a hole for the boundary, in WGS84 pub fn get_inverted_boundary(&self) -> Result { let (boundary, _) = self.mercator.to_wgs84(&self.boundary_polygon).into_inner(); diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 795ba27..8c31b35 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -65,6 +65,12 @@ impl MapModel { self.graph.render_debug().map_err(err_to_js) } + /// Returns a GeoJSON string showing all amenities + #[wasm_bindgen(js_name = renderAmenities)] + pub fn render_amenities(&self) -> Result { + self.graph.render_amenities().map_err(err_to_js) + } + /// Return a polygon covering the world, minus a hole for the boundary, in WGS84 #[wasm_bindgen(js_name = getInvertedBoundary)] pub fn get_inverted_boundary(&self) -> Result { diff --git a/web/src/ScoreMode.svelte b/web/src/ScoreMode.svelte index 5624f97..47d9111 100644 --- a/web/src/ScoreMode.svelte +++ b/web/src/ScoreMode.svelte @@ -149,7 +149,7 @@ {/if} - {#await notNull($backend).renderDebug() then data} + {#await notNull($backend).renderAmenities() then data} { - let gj = await $backend!.renderDebug(); + let gj = await $backend!.renderAmenities(); for (let f of gj.features) { - let kind: string | undefined = f.properties!.amenity_kind; - if (kind) { - if (kinds.has(kind)) { - kinds.get(kind)!.num += 1; - } else { - kinds.set(kind, { enabled: false, num: 1 }); - } + let kind = f.properties.amenity_kind; + if (kinds.has(kind)) { + kinds.get(kind)!.num += 1; + } else { + kinds.set(kind, { enabled: false, num: 1 }); } } diff --git a/web/src/worker.ts b/web/src/worker.ts index 443b739..78de3ec 100644 --- a/web/src/worker.ts +++ b/web/src/worker.ts @@ -1,6 +1,6 @@ import * as Comlink from "comlink"; import init, { MapModel } from "backend"; -import type { TravelMode, ScoreProps } from "./stores"; +import type { TravelMode, ScoreProps, Amenity } from "./stores"; import type { Position, Feature, @@ -68,6 +68,14 @@ export class Backend { return JSON.parse(this.inner.renderDebug()); } + renderAmenities(): FeatureCollection { + if (!this.inner) { + throw new Error("Backend used without a file loaded"); + } + + return JSON.parse(this.inner.renderAmenities()); + } + isochrone(req: { // TODO LngLatLike doesn't work? start: { lng: number; lat: number };