feat: add speed limit display
Some checks failed
TrafficCue CI / check (push) Failing after 1m53s
TrafficCue CI / build (push) Successful in 10m10s
TrafficCue CI / build-android (push) Successful in 30m40s

This commit is contained in:
2025-10-20 17:51:06 +02:00
parent b46646a462
commit 4ef2667c4e
5 changed files with 366 additions and 1 deletions

View File

@ -0,0 +1,49 @@
import { map } from "$lib/components/lnv/map.svelte";
import { lineString, pointToLineDistance } from "@turf/turf";
function getFeatureDistance(f: GeoJSON.Feature, point: [number, number]) {
if (f.geometry.type === "LineString") {
return pointToLineDistance(point, lineString(f.geometry.coordinates));
} else if (f.geometry.type === "MultiLineString") {
// Compute the min distance across all parts
return Math.min(
...f.geometry.coordinates.map((coords) =>
pointToLineDistance(point, lineString(coords))
)
);
} else {
return Infinity;
}
}
export function getTransportationMeta(coord: WorldLocation) {
if(!map.value) return null;
const features = map.value.querySourceFeatures("openmaptiles", {
sourceLayer: "transportation"
});
const filtered = features.filter((f) => f.geometry.type === "LineString" || f.geometry.type == "MultiLineString");
if(filtered.length === 0) return null;
const nearest = filtered.reduce((a, b) => {
const distA = getFeatureDistance(a, [coord.lon, coord.lat]);
const distB = getFeatureDistance(b, [coord.lon, coord.lat]);
return distA < distB ? a : b;
});
return nearest ? nearest.properties : null;
}
const IMPLICIT_SPEEDS: Record<string, number> = {
"DE:urban": 50,
"DE:rural": 100, // TODO: 80 (hgv weight > 3.5t, or trailer), 60 (weight > 7.5t)
"DE:living_street": 7,
"DE:bicycle_road": 30
}
export function getSpeed(maxspeed: string): number | null {
if(!isNaN(parseInt(maxspeed))) return parseInt(maxspeed);
if(maxspeed.endsWith(" mph")) {
const val = parseInt(maxspeed.replace(" mph", ""));
if(!isNaN(val)) return Math.round(val * 1.60934); // Convert to km/h
}
if(maxspeed === "walk") return 7; // https://wiki.openstreetmap.org/wiki/Proposed_features/maxspeed_walk
return IMPLICIT_SPEEDS[maxspeed as keyof typeof IMPLICIT_SPEEDS] || null;
}