ficed Bug when wrong Object is selected initialy
This commit is contained in:
@@ -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({
|
||||
|
||||
50
apps/dispatch/app/_helpers/findClosestPolygon.ts
Normal file
50
apps/dispatch/app/_helpers/findClosestPolygon.ts
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user