113 lines
4.5 KiB
TypeScript
113 lines
4.5 KiB
TypeScript
import type { Diagnostic, JunctionCandidate, Position, WorkbenchState } from '../types/state';
|
|
|
|
export type JunctionRef = { type: 'node' | 'cluster'; id: string };
|
|
export type JunctionHotspotStatus = 'editable' | 'readonly' | 'candidate';
|
|
export interface JunctionDescriptor {
|
|
ref: JunctionRef;
|
|
status: JunctionHotspotStatus;
|
|
coordinate: Position;
|
|
radiusMeters: number;
|
|
nodeIds: string[];
|
|
approachRoadIds: string[];
|
|
diagnosticIds: string[];
|
|
source: 'junction-plan' | 'cluster-config' | 'candidate';
|
|
}
|
|
|
|
function average(points: Position[]): Position | null {
|
|
if (!points.length) return null;
|
|
return [
|
|
points.reduce((sum, point) => sum + point[0], 0) / points.length,
|
|
points.reduce((sum, point) => sum + point[1], 0) / points.length,
|
|
];
|
|
}
|
|
|
|
function candidateDescriptor(candidate: JunctionCandidate, diagnostics: Diagnostic): JunctionDescriptor | null {
|
|
const coordinate =
|
|
diagnostics.geometry?.type === 'Point' && Array.isArray(diagnostics.geometry.coordinates)
|
|
? (diagnostics.geometry.coordinates as Position)
|
|
: null;
|
|
if (!coordinate || coordinate.length < 2) return null;
|
|
return {
|
|
ref: { type: 'cluster', id: candidate.id },
|
|
status: 'candidate',
|
|
coordinate,
|
|
radiusMeters: Math.max(8, Number(candidate.coreRadiusMeters) || 12),
|
|
nodeIds: candidate.nodeIds.map(String).sort(),
|
|
approachRoadIds: [],
|
|
diagnosticIds: [diagnostics.id],
|
|
source: 'candidate',
|
|
};
|
|
}
|
|
|
|
/** Derive stable, read-only hotspots from the compiled state. */
|
|
export function junctionDescriptors(state: WorkbenchState): JunctionDescriptor[] {
|
|
const diagnostics = state.compiled.diagnostics || [];
|
|
const result: JunctionDescriptor[] = [];
|
|
const endpointsByNode = new Map<string, typeof state.compiled.model.endpoints>();
|
|
for (const endpoint of state.compiled.model.endpoints || []) {
|
|
const list = endpointsByNode.get(String(endpoint.nodeId)) || [];
|
|
list.push(endpoint);
|
|
endpointsByNode.set(String(endpoint.nodeId), list);
|
|
}
|
|
for (const [nodeId, endpoints] of endpointsByNode) {
|
|
if (endpoints.length < 2) continue;
|
|
const coordinate = average(endpoints.map((endpoint) => endpoint.coordinate));
|
|
if (!coordinate) continue;
|
|
const related = diagnostics.filter(
|
|
(item) =>
|
|
item.subjectId === nodeId || (item.endpointId && endpoints.some((endpoint) => endpoint.id === item.endpointId)),
|
|
);
|
|
result.push({
|
|
ref: { type: 'node', id: nodeId },
|
|
status: 'editable',
|
|
coordinate,
|
|
radiusMeters: 10,
|
|
nodeIds: [nodeId],
|
|
approachRoadIds: [...new Set(endpoints.map((endpoint) => endpoint.roadId))].sort(),
|
|
diagnosticIds: related.map((item) => item.id).sort(),
|
|
source: 'junction-plan',
|
|
});
|
|
}
|
|
const configured = new Map<string, { nodeIds: string[]; coordinate: Position; approachRoadIds: string[] }>();
|
|
for (const feature of state.layers.nativeIntersectionSurface?.features || []) {
|
|
const clusterId = feature.properties?.cluster_id;
|
|
if (typeof clusterId !== 'string' || !clusterId) continue;
|
|
const geometry = feature.geometry;
|
|
const coordinates =
|
|
geometry && geometry.type === 'Polygon' && Array.isArray(geometry.coordinates)
|
|
? (geometry.coordinates as Position[][])[0]
|
|
: [];
|
|
const center = average(coordinates || []);
|
|
if (!center) continue;
|
|
const current = configured.get(clusterId) || { nodeIds: [], coordinate: center, approachRoadIds: [] };
|
|
current.coordinate = center;
|
|
const nodeId = feature.properties?.node_id;
|
|
if (typeof nodeId === 'string') current.nodeIds.push(nodeId);
|
|
configured.set(clusterId, current);
|
|
}
|
|
for (const [clusterId, value] of configured) {
|
|
const nodeIds = [...new Set(value.nodeIds)].sort();
|
|
result.push({
|
|
ref: { type: 'cluster', id: clusterId },
|
|
status: 'readonly',
|
|
coordinate: value.coordinate,
|
|
radiusMeters: 16,
|
|
nodeIds,
|
|
approachRoadIds: value.approachRoadIds,
|
|
diagnosticIds: [],
|
|
source: 'cluster-config',
|
|
});
|
|
}
|
|
for (const candidate of state.debug?.junctionCandidates || []) {
|
|
const diagnostic = diagnostics.find((item) => item.id === candidate.id);
|
|
if (!diagnostic) continue;
|
|
const descriptor = candidateDescriptor(candidate, diagnostic);
|
|
if (descriptor) result.push(descriptor);
|
|
}
|
|
return result.sort((a, b) => `${a.ref.type}:${a.ref.id}`.localeCompare(`${b.ref.type}:${b.ref.id}`));
|
|
}
|
|
|
|
export function hotspotAt(descriptors: JunctionDescriptor[], ref: JunctionRef): JunctionDescriptor | null {
|
|
return descriptors.find((item) => item.ref.type === ref.type && item.ref.id === ref.id) || null;
|
|
}
|