feat: add speed limit display
This commit is contained in:
49
src/lib/services/TileMeta.ts
Normal file
49
src/lib/services/TileMeta.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user