Skip to content

Commit

Permalink
Make a backend call just for amenities
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jun 2, 2024
1 parent 44ab987 commit b3b110f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
10 changes: 10 additions & 0 deletions backend/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ impl Graph {
Ok(out)
}

pub fn render_amenities(&self) -> Result<String> {
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<String> {
let (boundary, _) = self.mercator.to_wgs84(&self.boundary_polygon).into_inner();
Expand Down
6 changes: 6 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsValue> {
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<String, JsValue> {
Expand Down
2 changes: 1 addition & 1 deletion web/src/ScoreMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
</GeoJSON>
{/if}

{#await notNull($backend).renderDebug() then data}
{#await notNull($backend).renderAmenities() then data}
<GeoJSON {data}>
<SymbolLayer
filter={["==", ["get", "amenity_kind"], "bicycle_parking"]}
Expand Down
14 changes: 6 additions & 8 deletions web/src/common/PickAmenityKinds.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
$: enabled = getEnabled(kinds);
onMount(async () => {
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 });
}
}
Expand Down
10 changes: 9 additions & 1 deletion web/src/worker.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -68,6 +68,14 @@ export class Backend {
return JSON.parse(this.inner.renderDebug());
}

renderAmenities(): FeatureCollection<Point, Amenity> {
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 };
Expand Down

0 comments on commit b3b110f

Please sign in to comment.