feat: initial map matching
Some checks failed
TrafficCue CI / check (push) Failing after 1m38s

This commit is contained in:
2025-10-21 20:08:08 +02:00
parent 5ec74129e2
commit aa0bcdd091
3 changed files with 87 additions and 4 deletions

View File

@ -19,7 +19,7 @@ function getFeatureDistance(f: GeoJSON.Feature, point: [number, number]) {
}
}
export async function getMeta(coord: WorldLocation, layer: string) {
export async function getFeature(coord: WorldLocation, layer: string, filter?: (feature: GeoJSON.Feature) => boolean) {
const zxy = coordToTile(coord, 14);
const tile = await fetchTile(zxy.z, zxy.x, zxy.y);
const layerData = tile.layers[layer];
@ -33,13 +33,18 @@ export async function getMeta(coord: WorldLocation, layer: string) {
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) => {
const distA = getFeatureDistance(a, [coord.lon, coord.lat]);
const distB = getFeatureDistance(b, [coord.lon, coord.lat]);
return distA < distB ? a : b;
});
return nearest;
}
export async function getMeta(coord: WorldLocation, layer: string, filter?: (feature: GeoJSON.Feature) => boolean) {
const nearest = await getFeature(coord, layer, filter);
return nearest ? nearest.properties : null;
}