feat(workbench): add junction hotspot entry points
This commit is contained in:
1 parent
bd7b049930
commit
7d9b0f46d4
6 files changed
+288
-6
No files matched your search
@@ -0,0 +1,86 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { junctionDescriptors, hotspotAt } from './hotspots';
|
||||
import type { WorkbenchState } from '../types/state';
|
||||
|
||||
const state = (overrides: Partial<WorkbenchState> = {}): WorkbenchState => ({
|
||||
areaId: 'a',
|
||||
compiled: {
|
||||
model: {
|
||||
roads: [],
|
||||
endpoints: [
|
||||
{ id: 'e2', roadId: 'r2', side: 'start', nodeId: 'n1', coordinate: [2, 2] },
|
||||
{ id: 'e1', roadId: 'r1', side: 'end', nodeId: 'n1', coordinate: [0, 0] },
|
||||
],
|
||||
connections: [],
|
||||
},
|
||||
diagnostics: [],
|
||||
},
|
||||
overrides: { schema: 'x', overrides: [] },
|
||||
trafficSignals: { provenance: 'x', assemblies: { type: 'FeatureCollection', features: [] } },
|
||||
trafficRuntime: { signals: [] },
|
||||
comparison: {},
|
||||
layers: {},
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe('junction hotspots', () => {
|
||||
it('derives stable editable node descriptor', () => {
|
||||
const first = junctionDescriptors(state());
|
||||
const second = junctionDescriptors(state());
|
||||
expect(first).toEqual(second);
|
||||
expect(first[0]).toMatchObject({ ref: { type: 'node', id: 'n1' }, status: 'editable', coordinate: [1, 1] });
|
||||
});
|
||||
it('marks configured clusters readonly and candidates as candidate', () => {
|
||||
const value = state({
|
||||
layers: {
|
||||
nativeIntersectionSurface: {
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{
|
||||
type: 'Feature',
|
||||
geometry: {
|
||||
type: 'Polygon',
|
||||
coordinates: [
|
||||
[
|
||||
[0, 0],
|
||||
[1, 0],
|
||||
[1, 1],
|
||||
[0, 0],
|
||||
],
|
||||
],
|
||||
},
|
||||
properties: { cluster_id: 'c1' },
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
debug: {
|
||||
junctionCandidates: [
|
||||
{
|
||||
index: 1,
|
||||
id: 'd1',
|
||||
nodeIds: ['n1', 'n2'],
|
||||
nodeCount: 2,
|
||||
diameterMeters: 10,
|
||||
template: 'complex-junction-v1',
|
||||
coreRadiusMeters: 8,
|
||||
},
|
||||
],
|
||||
},
|
||||
compiled: {
|
||||
...state().compiled,
|
||||
diagnostics: [
|
||||
{
|
||||
id: 'd1',
|
||||
rule: 'complex-junction-candidate',
|
||||
message: 'candidate',
|
||||
geometry: { type: 'Point', coordinates: [3, 3] },
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
const descriptors = junctionDescriptors(value);
|
||||
expect(hotspotAt(descriptors, { type: 'cluster', id: 'c1' })?.status).toBe('readonly');
|
||||
expect(hotspotAt(descriptors, { type: 'cluster', id: 'd1' })?.status).toBe('candidate');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user