From 29b57b2c37440c2fa7af93f87557567532908bf4 Mon Sep 17 00:00:00 2001 From: PxlLoewe <72106766+PxlLoewe@users.noreply.github.com> Date: Tue, 17 Jun 2025 21:10:44 -0700 Subject: [PATCH] ficed Bug when wrong Object is selected initialy --- .../app/_components/map/ContextMenu.tsx | 29 +++-------- .../app/_helpers/findClosestPolygon.ts | 50 +++++++++++++++++++ .../_components/pannel/MissionForm.tsx | 2 - 3 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 apps/dispatch/app/_helpers/findClosestPolygon.ts diff --git a/apps/dispatch/app/_components/map/ContextMenu.tsx b/apps/dispatch/app/_components/map/ContextMenu.tsx index 8cb3120b..e8c3bf54 100644 --- a/apps/dispatch/app/_components/map/ContextMenu.tsx +++ b/apps/dispatch/app/_components/map/ContextMenu.tsx @@ -10,6 +10,7 @@ import toast from "react-hot-toast"; import { Popup, useMap } from "react-leaflet"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { editMissionAPI } from "_querys/missions"; +import { findClosestPolygon } from "_helpers/findClosestPolygon"; export const ContextMenu = () => { const map = useMap(); @@ -113,30 +114,16 @@ export const ContextMenu = () => { onClick={async () => { const { parsed } = await getOsmAddress(contextMenu.lat, contextMenu.lng); const objects = await addOSMobjects(true); - const closestToContext = objects.reduce((prev, curr) => { - const prevLat = prev.nodes?.[0]?.lat ?? 0; - const prevLon = prev.nodes?.[0]?.lon ?? 0; - const currLat = curr.nodes?.[0]?.lat ?? 0; - const currLon = curr.nodes?.[0]?.lon ?? 0; - const prevDistance = Math.sqrt( - Math.pow(prevLat - contextMenu.lat, 2) + Math.pow(prevLon - contextMenu.lng, 2), - ); - const currDistance = Math.sqrt( - Math.pow(currLat - contextMenu.lat, 2) + Math.pow(currLon - contextMenu.lng, 2), - ); - return prevDistance < currDistance ? prev : curr; - }, [] as any); + + const closestObject = findClosestPolygon(objects, { + lat: contextMenu.lat, + lon: contextMenu.lng, + }); setOpen(true); - const nodeWay: [number, number][] = []; - - closestToContext.nodes?.forEach((node: { lat: number; lon: number }) => - nodeWay.push([node.lat, node.lon]), - ); - - if (closestToContext) { - toggleSearchElementSelection(closestToContext.wayID, true); + if (closestObject) { + toggleSearchElementSelection(closestObject.wayID, true); } setMissionFormValues({ diff --git a/apps/dispatch/app/_helpers/findClosestPolygon.ts b/apps/dispatch/app/_helpers/findClosestPolygon.ts new file mode 100644 index 00000000..fd190696 --- /dev/null +++ b/apps/dispatch/app/_helpers/findClosestPolygon.ts @@ -0,0 +1,50 @@ +import { OSMWay } from "@repo/db"; +import { centroid, distance } from "@turf/turf"; + +function toPolygonFeature(nodes: { lat: number; lon: number }[]) { + if (!nodes || nodes.length < 3) return null; + + const coords = nodes.map((n) => [n.lon, n.lat]); + const isClosed = + coords.length >= 1 && + coords[0]![0] === coords[coords.length - 1]![0] && + coords[0]![1] === coords[coords.length - 1]![1]; + if (!isClosed) coords.push(coords[0]!); // Polygon schließen + + return { + type: "Feature", + geometry: { + type: "Polygon", + coordinates: [coords], + }, + properties: {}, + }; +} + +// Hauptfunktion: findet das nächstgelegene Polygon zu einem Punkt +export function findClosestPolygon( + ways: OSMWay[], + referencePoint: { + lat: number; + lon: number; + }, +): OSMWay | null { + let closest: OSMWay | null = null; + let minDistance = Infinity; + + for (const way of ways) { + const polygon = toPolygonFeature(way.nodes); + if (!polygon) continue; + + const center = centroid(polygon as any).geometry.coordinates; // [lon, lat] + + const newDistance = distance([referencePoint.lon, referencePoint.lat], center); + + if (newDistance < minDistance) { + minDistance = newDistance; + closest = way; + } + } + + return closest; +} diff --git a/apps/dispatch/app/dispatch/_components/pannel/MissionForm.tsx b/apps/dispatch/app/dispatch/_components/pannel/MissionForm.tsx index 22370230..ee4dbcdd 100644 --- a/apps/dispatch/app/dispatch/_components/pannel/MissionForm.tsx +++ b/apps/dispatch/app/dispatch/_components/pannel/MissionForm.tsx @@ -132,8 +132,6 @@ export const MissionForm = () => { ); }, [searchElements, form, missionFormValues]); - console.log("addressOSMways", form.watch("addressOSMways")); - useEffect(() => { if (missionFormValues) { if (Object.keys(missionFormValues).length === 0) {