diff --git a/src/lib/components/lnv/location.svelte.ts b/src/lib/components/lnv/location.svelte.ts index 00db124..612dbb6 100644 --- a/src/lib/components/lnv/location.svelte.ts +++ b/src/lib/components/lnv/location.svelte.ts @@ -75,7 +75,10 @@ function snapLocation() { if (!feature) return; if (!rawLocation.current) return; if (feature.geometry.type === "LineString") { - const loc = nearestPointOnLine(lineString(feature.geometry.coordinates), point([rawLocation.current.lon, rawLocation.current.lat])); + const loc = nearestPointOnLine( + lineString(feature.geometry.coordinates), + point([rawLocation.current.lon, rawLocation.current.lat]), + ); snappedLocation.current = { lat: loc.geometry.coordinates[1], lon: loc.geometry.coordinates[0], @@ -85,7 +88,10 @@ function snapLocation() { let nearestLoc: GeoJSON.Feature | null = null; let minDist = Infinity; for (const coords of feature.geometry.coordinates) { - const loc = nearestPointOnLine(lineString(coords), point([rawLocation.current.lon, rawLocation.current.lat])); + const loc = nearestPointOnLine( + lineString(coords), + point([rawLocation.current.lon, rawLocation.current.lat]), + ); const dist = Math.hypot( loc.geometry.coordinates[0] - rawLocation.current.lon, loc.geometry.coordinates[1] - rawLocation.current.lat, @@ -114,7 +120,7 @@ export function watchLocation() { lat: pos.coords.latitude, lon: pos.coords.longitude, }; - if(!location.useSnapped) { + if (!location.useSnapped) { location.lat = pos.coords.latitude; location.lng = pos.coords.longitude; } @@ -124,25 +130,32 @@ export function watchLocation() { location.heading = pos.coords.heading; location.lastUpdate = new Date(); - const blacklist = ["path", "track", "raceway", "busway", "bus_guideway", "ferry"]; + const blacklist = [ + "path", + "track", + "raceway", + "busway", + "bus_guideway", + "ferry", + ]; getFeature( { lat: rawLocation.current.lat, lon: rawLocation.current.lon }, "transportation", { filter: (f) => { - if(f.properties) { + if (f.properties) { return !blacklist.includes(f.properties["class"]); } return true; }, lastId: lastFeatureId || undefined, - } + }, ).then((feature) => { roadFeature.current = feature; roadMetadata.current = feature ? feature.properties : null; lastFeatureId = feature ? String(feature.id) : null; snapLocation(); - if(location.useSnapped && snappedLocation.current) { + if (location.useSnapped && snappedLocation.current) { location.lat = snappedLocation.current.lat; location.lng = snappedLocation.current.lon; } diff --git a/src/lib/services/TileMeta.ts b/src/lib/services/TileMeta.ts index 249ecbe..c37961d 100644 --- a/src/lib/services/TileMeta.ts +++ b/src/lib/services/TileMeta.ts @@ -7,7 +7,9 @@ import { location } from "$lib/components/lnv/location.svelte"; function getFeatureDistance(f: GeoJSON.Feature, point: [number, number]) { if (f.geometry.type === "LineString") { - return pointToLineDistance(point, lineString(f.geometry.coordinates), { units: "meters" }); + return pointToLineDistance(point, lineString(f.geometry.coordinates), { + units: "meters", + }); } else if (f.geometry.type === "MultiLineString") { // Compute the min distance across all parts return Math.min( @@ -26,11 +28,15 @@ interface GetFeatureOptions { } function getBias() { - if(!location.speed) return 5; + if (!location.speed) return 5; return Math.max(5, Math.min(15, location.speed * 0.5)); // Bias increases with speed, min 5, max 15, 0.5 per km/h } -export async function getFeature(coord: WorldLocation, layer: string, { lastId, filter }: GetFeatureOptions = {}) { +export async function getFeature( + coord: WorldLocation, + layer: string, + { lastId, filter }: GetFeatureOptions = {}, +) { const zxy = coordToTile(coord, 14); const tile = await fetchTile(zxy.z, zxy.x, zxy.y); const layerData = tile.layers[layer]; @@ -41,10 +47,13 @@ export async function getFeature(coord: WorldLocation, layer: string, { lastId, const feature = layerData.feature(i); features.push(feature.toGeoJSON(zxy.x, zxy.y, zxy.z)); } - const filtered = features.filter( - (f) => - f.geometry.type === "LineString" || f.geometry.type == "MultiLineString", - ).filter((f) => (filter ? filter(f) : true)); + const filtered = features + .filter( + (f) => + f.geometry.type === "LineString" || + f.geometry.type == "MultiLineString", + ) + .filter((f) => (filter ? filter(f) : true)); if (filtered.length === 0) return null; const nearest = filtered.reduce((a, b) => { let distA = getFeatureDistance(a, [coord.lon, coord.lat]); @@ -71,7 +80,11 @@ export async function getFeature(coord: WorldLocation, layer: string, { lastId, return nearest; } -export async function getMeta(coord: WorldLocation, layer: string, options?: GetFeatureOptions) { +export async function getMeta( + coord: WorldLocation, + layer: string, + options?: GetFeatureOptions, +) { const nearest = await getFeature(coord, layer, options); return nearest ? nearest.properties : null; }