fix: require source evidence for complex crosswalks

This commit is contained in:
que01 committed 2026-09-11 11:24:19 +08:00
1 parent 858db529ae
commit 3a8af43140
9 files changed
+320 -53

No files matched your search

+21 -9
View File
@@ -422,17 +422,28 @@ assert.equal(
false,
);
assert.ok(complexGeometry.diagnostics.some((item) => item.rule === 'complex-junction-corner-facilities-omitted'));
const inferredComplexCrosswalk = complexGeometry.crosswalks.features.find(
(feature) => feature.properties?.kind === 'complex-crosswalk',
assert.equal(
complexGeometry.crosswalks.features.some((feature) => feature.properties?.kind === 'complex-crosswalk'),
false,
'complex approaches without source crossing evidence must not synthesize zebra crossings',
);
assert.equal(inferredComplexCrosswalk.properties.facility_evidence, 'inferred-road-envelope');
assert.ok(complexGeometry.diagnostics.some((item) => item.rule === 'complex-junction-crossing-inferred'));
assert.ok(complexGeometry.diagnostics.some((item) => item.rule === 'complex-junction-crossing-source-missing'));
assert.match(
complexGeometry.diagnostics.find((item) => item.rule === 'complex-junction-generated').message,
/0 组斑马线/,
);
const inferredStopLine = complexGeometry.vehicleStopLines.features.find(
(feature) => feature.properties?.kind === 'complex-stop-line',
);
assert.equal(inferredStopLine.properties.facility_evidence, 'unknown-no-crossing-source');
const sourceCrossingModel = structuredClone(complexModel);
const sourceRoad = sourceCrossingModel.roads.find((road) => road.id === inferredComplexCrosswalk.properties.road_id);
const sourceRoad = sourceCrossingModel.roads.find((road) => road.id === inferredStopLine.properties.road_id);
sourceCrossingModel.crossings = [
{
id: 'known-complex-crossing',
coordinate: inferredComplexCrosswalk.properties.frame_center,
coordinate: inferredStopLine.geometry.coordinates[0]
.slice(0, -1)
.reduce((center, point) => [center[0] + point[0] / 4, center[1] + point[1] / 4], [0, 0]),
osmWayIds: sourceRoad.osmWayIds,
},
];
@@ -455,8 +466,9 @@ const sourceCrossingGeometry = compiler.nativeRoad.compileGeometry(
},
);
const sourcedComplexCrosswalk = sourceCrossingGeometry.crosswalks.features.find(
(feature) => feature.properties?.crossing_key === inferredComplexCrosswalk.properties.crossing_key,
(feature) => feature.properties?.crossing_key === inferredStopLine.properties.crossing_key,
);
assert.ok(sourcedComplexCrosswalk, 'an OSM crossing node enables the matching arm crosswalk');
assert.equal(sourcedComplexCrosswalk.properties.facility_evidence, 'source-crossing-node');
assert.deepEqual(sourcedComplexCrosswalk.properties.source_crossing_node_ids, ['known-complex-crossing']);
const sourceCenter = sourceCrossingGeometry.roadSurface.features.find(
@@ -469,14 +481,14 @@ const sourceCrossingRadius = Math.hypot(
);
assert.ok(Math.abs(sourcedComplexCrosswalk.properties.radial_distance_m - sourceCrossingRadius) < 0.1);
const sourcedStopLine = sourceCrossingGeometry.vehicleStopLines.features.find(
(feature) => feature.properties?.crossing_key === inferredComplexCrosswalk.properties.crossing_key,
(feature) => feature.properties?.crossing_key === inferredStopLine.properties.crossing_key,
);
if (sourcedStopLine) assert.equal(sourcedStopLine.properties.facility_evidence, 'source-crossing-node');
const sourceStructure = sourceCrossingGeometry.roadSurface.features.find(
(feature) => feature.properties?.junction_structure,
)?.properties.junction_structure;
assert.equal(
sourceStructure.facilities.find((facility) => facility.key === inferredComplexCrosswalk.properties.object_key)
sourceStructure.facilities.find((facility) => facility.key === sourcedComplexCrosswalk.properties.object_key)
.provenance.facilityEvidence,
'source-crossing-node',
);
+81 -1
View File
@@ -254,7 +254,83 @@ const complexInput = {
stagingDir: path.join(workspace, 'outputs', '_complex'),
options: { edgeLines: false, junctionTemplates: { enabled: true, clusters: [cluster] } },
};
const complexArea = compileInput(complexInput).area;
const unsourcedComplexArea = compileInput(complexInput).area;
const unsourcedState = editState(unsourcedComplexArea, selectionFrom({ cluster: cluster.id }));
const unsourcedPreview = editPreview(unsourcedComplexArea, {
document: { ...unsourcedState.document, constraints: [], operations: [] },
selection: { cluster: cluster.id },
});
assert.equal(
unsourcedPreview.layers.crosswalks.features.some((feature) => feature.properties.kind === 'complex-crosswalk'),
false,
'the default fixture must not synthesize complex crossings without source evidence',
);
for (const response of [unsourcedState, unsourcedPreview])
assert.equal(
response.handles.handles.some((handle) => handle.kind === 'cluster-crossing-inset'),
false,
);
assert.ok(unsourcedPreview.diagnostics.some((item) => item.rule === 'complex-junction-crossing-source-missing'));
// Supply an actual crossing node on the source way near its inferred stop line.
// Keep this input separate so the default fixture continues to test unknown evidence.
const inferredStopLine = unsourcedPreview.layers.vehicleStopLines.features.find(
(feature) => feature.properties.kind === 'complex-stop-line',
);
assert.ok(inferredStopLine);
const crossingCoordinate = inferredStopLine.geometry.coordinates[0]
.slice(0, -1)
.reduce((center, point) => [center[0] + point[0] / 4, center[1] + point[1] / 4], [0, 0]);
const originalXml = fs.readFileSync(input.osmFile, 'utf8');
const sourceModel = compileRoadModel(originalXml, loadOverrides(input.overridesFile));
const sourceRoad = sourceModel.roads.find((road) => road.id === inferredStopLine.properties.road_id);
const distanceSquared = (point) =>
((point[0] - crossingCoordinate[0]) * Math.cos((point[1] * Math.PI) / 180)) ** 2 +
(point[1] - crossingCoordinate[1]) ** 2;
const attribute = (attributes, name) => new RegExp(`\\b${name}\\s*=\\s*['"]([^'"]+)['"]`).exec(attributes)?.[1];
const sourceNodes = new Map(
[...originalXml.matchAll(/<node\b([^>]*)>/g)].map((match) => [
attribute(match[1], 'id'),
[Number(attribute(match[1], 'lon')), Number(attribute(match[1], 'lat'))],
]),
);
const sourcedOsmFile = path.join(workspace, 'source-with-crossing.osm');
const crossingNodeId = 'known-complex-crossing';
let insertedCrossing = false;
const sourcedXml = originalXml
.replace(/<way\b([^>]*)>([\s\S]*?)<\/way>/g, (way, attributes) => {
const wayId = attribute(attributes, 'id');
if (!sourceRoad.osmWayIds.includes(wayId)) return way;
const refs = [...way.matchAll(/<nd\b([^>]*)\/>/g)].map((match) => attribute(match[1], 'ref'));
const insertionSegment = refs
.slice(1)
.map((ref, index) => ({
index,
distance: distanceSquared(sourceNodes.get(ref)) + distanceSquared(sourceNodes.get(refs[index])),
}))
.sort((first, second) => first.distance - second.distance)[0].index;
const followingNode = refs[insertionSegment + 1];
return way.replace(new RegExp(`<nd\\s+ref=['"]${followingNode}['"]\\s*/>`), (node) => {
insertedCrossing = true;
return `<nd ref="${crossingNodeId}"/>${node}`;
});
})
.replace(
'</osm>',
`<node id="${crossingNodeId}" lon="${crossingCoordinate[0]}" lat="${crossingCoordinate[1]}"><tag k="highway" v="crossing"/></node></osm>`,
);
assert.ok(insertedCrossing);
fs.writeFileSync(sourcedOsmFile, sourcedXml);
const sourcedModel = compileRoadModel(sourcedXml, loadOverrides(input.overridesFile));
const sourceCrossing = sourcedModel.crossings.find((crossing) => crossing.id === crossingNodeId);
assert.deepEqual(sourceCrossing.coordinate, crossingCoordinate);
assert.ok(sourceCrossing.osmWayIds.some((id) => sourceRoad.osmWayIds.includes(id)));
const complexArea = compileInput({
...complexInput,
osmFile: sourcedOsmFile,
outDir: path.join(workspace, 'outputs', 'complex-sourced'),
stagingDir: path.join(workspace, 'outputs', '_complex-sourced'),
}).area;
const clusterState = editState(complexArea, selectionFrom({ cluster: cluster.id }));
assert.ok(
clusterState.handles.handles.every((handle) =>
@@ -296,6 +372,7 @@ assert.deepEqual(
const complexWidthHandle = clusterState.handles.handles.find(
(handle) =>
handle.kind === 'cluster-carriageway-width' &&
handle.anchor.sourceRoadIds.includes(inferredStopLine.properties.road_id) &&
clusterPreview.layers.vehicleStopLines.features.some(
(feature) => feature.properties.carriageway_key === handle.anchor.carriagewayKey,
),
@@ -416,6 +493,8 @@ assert.ok(complexCrossingHandle, 'the complex fixture must publish grouped cross
const renderedComplexCrosswalk = clusterPreview.layers.crosswalks.features.find(
(feature) => feature.properties.crossing_key === complexCrossingHandle.anchor.crossingKey,
);
assert.equal(renderedComplexCrosswalk.properties.facility_evidence, 'source-crossing-node');
assert.deepEqual(renderedComplexCrosswalk.properties.source_crossing_node_ids, [crossingNodeId]);
const crossingHandleLatitudeScale = 111320 * Math.cos((complexCrossingHandle.position[1] * Math.PI) / 180);
assert.ok(
Math.hypot(
@@ -461,6 +540,7 @@ const crossingGeometry = (preview) =>
assert.deepEqual(
movedCrossingPreview.constraintStates?.map((state) => state.applied),
[true],
JSON.stringify(movedCrossingPreview.constraintStates),
);
assert.notDeepEqual(
crossingGeometry(movedCrossingPreview),