diff --git a/.trellis/spec/backend/direct-edit-api.md b/.trellis/spec/backend/direct-edit-api.md index 1823c7b..02f03ca 100644 --- a/.trellis/spec/backend/direct-edit-api.md +++ b/.trellis/spec/backend/direct-edit-api.md @@ -331,9 +331,9 @@ then `structureFromTopology(topology, shape, center, features)`. Projection is `cluster:`. The legacy singular field is null at this unscoped entry point. - `GET /api/edit-state?junction=` preserves the node selection contract; `?cluster=` selects a cluster. Preview uses the equivalent `selection` - object. A selected editable cluster publishes its external carriageway and - corner handles plus `cluster-crossing-inset` and - `cluster-stop-line-offset` for each external arm crossing. + object. A selected editable cluster publishes its external carriageway, + corner, and stop-line handles. It publishes `cluster-crossing-inset` only for + an arm with a matching OSM crossing node. - A cluster carriageway anchor contains `clusterId`, stable `carriagewayKey`, `sourceNodeIds`, and `sourceRoadIds`. Replay matches all source identity; a missing or changed source is stale with `anchor-missing`. @@ -353,6 +353,16 @@ then `structureFromTopology(topology, shape, center, features)`. Projection is generator applies the inset once per stripe group and positions each incoming stop line relative to its matching crossing. Never identify these facilities by `crossing_node_id`, because complex members can share a node. +- A road envelope or inferred stop line is not crossing evidence. Without a + matching source crossing node, compilation emits no complex crosswalk, + publishes no crossing-inset handle, records facility evidence as + `unknown-no-crossing-source`, and emits + `complex-junction-crossing-source-missing`. A stop line may remain as an + independent inferred facility; its position must not consume a nonexistent + crossing inset. +- Source matching uses the same inferred arm radius in handle publication and + constraint solving. A published handle must remain applicable at every + advertised value, including `value.max`. - Preview and save both evaluate the same candidate through the resolver and `compileGeometry()`. Save rejects every `severity: error` diagnostic before writing the edit document. diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md index 8605ff0..762cacf 100644 --- a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md +++ b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md @@ -41,8 +41,8 @@ ## 4. 现有设施联动与缺失信息治理 - [x] 停止按夹角和道路宽度自动伪造角部导流岛与短斑马线;缺少岛体区域或步行连接来源时省略并报告原因。 -- [ ] 现有过街/停止线依附最终车行道;区分未知、已知存在、不支持。 -- [ ] 清理没有区域/连接依据的自动角部设施;对旧结果变化提供来源和诊断说明。 +- [x] 现有过街/停止线依附最终车行道;区分未知、已知存在、不支持。缺少匹配 OSM crossing 节点时不生成复杂斑马线或退让手柄,停止线独立保留并标记 `unknown-no-crossing-source`。 +- [x] 清理没有区域/连接依据的自动角部设施;对旧结果变化提供来源和诊断说明。 - [ ] 验证已知排除区和独立通道未被核心铺装吞并,未知岛体不臆造。 - [ ] 出口 A6;导流岛自由编辑与新右转通道创建留后续任务。 @@ -112,6 +112,8 @@ complex-junction.js / native-road.js:构建与全部消费者;direct-edit-so 本轮进一步补齐两个遗漏:角部道路补面、人行道转角复用 BoundaryGraph 的区域相交判定,侵入已知排除区域时省略并给出角部对象级诊断;复杂过街仅在所属道路臂全部车行道过渡面有效时发布,避免短道路已经无效却继续输出斑马线。短道路回归现在包含 crosswalks、vehicleStopLines 以及缺接缝诊断的对象和来源 ID。排除区回归覆盖实际生成的道路圆角和人行道转角。 +用户复核指出探出分支不应自动出现斑马线。本轮收紧设施证据:道路包络和推断停止线不再充当过街存在证据;只有与道路和位置匹配的 OSM crossing 节点才生成复杂斑马线并发布 `cluster-crossing-inset`。无来源进口输出 `complex-junction-crossing-source-missing`,停止线保留但不消费不存在的退让值。单元与工作台 API 同时覆盖无来源省略和有来源编辑,且 solver 与 manifest 共用来源搜索半径,保证已发布手柄的最大值可实际应用。当前真实 fixture 的验证指标由 5 个过街手柄降为 0,纯效果图确认探出分支无斑马线。 + 此处采用明确省略冲突对象,尚未实现绕排除区域重新求解、孔洞或多区域连通;不能据此关闭 A3/A6。下一步需建立区域级共享边界与连通性验证,再推进剩余接入位姿/左右边界编辑,最后做真实数据视觉验收。 本轮验证:npm test、客户端类型检查、188 项客户端单元测试、11 项 E2E(含生产构建)、format:check 与 git diff --check 全部通过。构建保留既有 bundle 大小提示。 diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/references/current-debug.png b/.trellis/tasks/09-08-junction-reference-fidelity/references/current-debug.png new file mode 100644 index 0000000..90491a7 Binary files /dev/null and b/.trellis/tasks/09-08-junction-reference-fidelity/references/current-debug.png differ diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/references/current-metrics.json b/.trellis/tasks/09-08-junction-reference-fidelity/references/current-metrics.json new file mode 100644 index 0000000..2e00f08 --- /dev/null +++ b/.trellis/tasks/09-08-junction-reference-fidelity/references/current-metrics.json @@ -0,0 +1,139 @@ +{ + "clusterId": "cluster-8005332807", + "topologyFingerprint": "1a99b5f202d59b65aab955a1c9d9de80e19e18b00c91bcc7e5bc8517678d7632", + "armCount": 5, + "carriagewayCount": 9, + "handleCounts": { + "cluster-carriageway-width": 9, + "cluster-carriageway-transition-length": 9, + "cluster-carriageway-cutback": 9, + "cluster-carriageway-position": 9, + "cluster-carriageway-direction": 9, + "cluster-carriageway-edge-offset": 18, + "cluster-corner-radius": 5, + "cluster-stop-line-offset": 5 + }, + "diagnostics": [ + { + "severity": "warning", + "rule": "unconnected-interior-road-end", + "subjectId": "road:way/99505317:forward", + "message": "道路在区域内部结束,未连接到其他机动车道路。请确认这是实际断头,还是 OSM 节点尚未连接。" + }, + { + "severity": "warning", + "rule": "unconnected-interior-road-end", + "subjectId": "road:way/858770821:segment/2:forward", + "message": "道路在区域内部结束,未连接到其他机动车道路。请确认这是实际断头,还是 OSM 节点尚未连接。" + }, + { + "severity": "info", + "rule": "complex-junction-candidate", + "subjectId": "junction-cluster-candidate:1590698049+8042920686", + "message": "检测到 2 个路口节点由 1 条短路段(最长 19.6 米)相连,可能是同一个物理路口。当前按独立路口编译;如需合并请在 nativeRoad.junctionTemplates.clusters 中配置。" + }, + { + "severity": "info", + "rule": "complex-junction-crossing-source-missing", + "subjectId": "crossing:cluster:cluster-8005332807:arm:8005332807/segment%3Away%2F859509947%2F2/road%3Away%2F859509947%3Asegment%2F2%3Aforward|8613058676/segment%3Away%2F858770823%2F3/road%3Away%2F858770823%3Asegment%2F3%3Aforward", + "message": "该进口未找到匹配的 OSM 过街节点,过街状态未知,未生成斑马线。" + }, + { + "severity": "info", + "rule": "complex-junction-crossing-source-missing", + "subjectId": "crossing:cluster:cluster-8005332807:arm:8613058676/segment%3Away%2F928582773%2F0/road%3Away%2F928582773%3Abackward/road%3Away%2F928582773%3Aforward", + "message": "该进口未找到匹配的 OSM 过街节点,过街状态未知,未生成斑马线。" + }, + { + "severity": "info", + "rule": "complex-junction-crossing-source-missing", + "subjectId": "crossing:cluster:cluster-8005332807:arm:8024512135/segment%3Away%2F851989492%2F1/road%3Away%2F851989492%3Asegment%2F1%3Aforward|8024512147/segment%3Away%2F858770822%2F2/road%3Away%2F858770822%3Asegment%2F2%3Aforward", + "message": "该进口未找到匹配的 OSM 过街节点,过街状态未知,未生成斑马线。" + }, + { + "severity": "info", + "rule": "complex-junction-crossing-source-missing", + "subjectId": "crossing:cluster:cluster-8005332807:arm:8024512145/segment%3Away%2F858770821%2F2/road%3Away%2F858770821%3Asegment%2F2%3Aforward|8024512147/segment%3Away%2F99505317%2F0/road%3Away%2F99505317%3Aforward", + "message": "该进口未找到匹配的 OSM 过街节点,过街状态未知,未生成斑马线。" + }, + { + "severity": "info", + "rule": "complex-junction-crossing-source-missing", + "subjectId": "crossing:cluster:cluster-8005332807:arm:8005332807/segment%3Away%2F253970456%2F0/road%3Away%2F253970456%3Aforward|8024512145/segment%3Away%2F851986783%2F0/road%3Away%2F851986783%3Aforward", + "message": "该进口未找到匹配的 OSM 过街节点,过街状态未知,未生成斑马线。" + }, + { + "severity": "info", + "rule": "complex-junction-corner-facilities-omitted", + "subjectId": "junction-cluster:cluster-8005332807", + "message": "已省略 4 组缺少岛体或步行连接来源的角部导流岛和斑马线。" + }, + { + "severity": "warning", + "rule": "complex-junction-core-closure-unsupported", + "subjectId": "junction-cluster:cluster-8005332807", + "message": "路口中心区域缺少可验证的闭合边(parallel-carriageway-closure-unresolved),已保留各进口道路面。" + }, + { + "severity": "info", + "rule": "complex-junction-generated", + "subjectId": "junction-cluster:cluster-8005332807", + "message": "已识别为5 臂非规则路口,独立生成 5 个进口、道路面、斑马线和停止线。" + }, + { + "severity": "warning", + "rule": "junction-connector-envelope-fallback", + "subjectId": "junction:node/1218843451", + "message": "路口面需要按转向路径的凸包兜底生成;请检查外缘和路缘与步行带是否符合实际。" + }, + { + "severity": "info", + "rule": "ordinary-junction-surface", + "subjectId": "junction:node/1218843451", + "message": "已按道路截面与转向路径生成普通路口面。" + }, + { + "severity": "info", + "rule": "ordinary-junction-surface", + "subjectId": "junction:node/1590698049", + "message": "已按道路截面与转向路径生成普通路口面。" + }, + { + "severity": "info", + "rule": "ordinary-junction-surface", + "subjectId": "junction:node/8042920686", + "message": "已按道路截面与转向路径生成普通路口面。" + }, + { + "severity": "info", + "rule": "ordinary-junction-surface", + "subjectId": "junction:node/8613058679", + "message": "已按道路截面与转向路径生成普通路口面。" + }, + { + "severity": "pass", + "rule": "effective-carriageway-width", + "message": "车行道有效宽度符合标准范围。" + }, + { + "severity": "pass", + "rule": "effective-carriageway-width", + "message": "车行道有效宽度符合标准范围。" + }, + { + "severity": "pass", + "rule": "effective-carriageway-width", + "message": "车行道有效宽度符合标准范围。" + }, + { + "severity": "pass", + "rule": "effective-carriageway-width", + "message": "车行道有效宽度符合标准范围。" + }, + { + "severity": "pass", + "rule": "effective-carriageway-width", + "message": "车行道有效宽度符合标准范围。" + } + ] +} diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/references/current-pure.png b/.trellis/tasks/09-08-junction-reference-fidelity/references/current-pure.png new file mode 100644 index 0000000..db8c49a Binary files /dev/null and b/.trellis/tasks/09-08-junction-reference-fidelity/references/current-pure.png differ diff --git a/src/compile/complex-junction.js b/src/compile/complex-junction.js index 0f226a9..1e9f40d 100644 --- a/src/compile/complex-junction.js +++ b/src/compile/complex-junction.js @@ -85,7 +85,7 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { const inferredEnvelope = armEnvelopeAtRadius(arm, inferredRadius, center, helpers); const evidence = inferredEnvelope ? crossingEvidenceForArm(model, arm, inferredEnvelope.center, helpers) - : { kind: 'inferred-road-envelope', crossingNodeIds: [], crossing: null }; + : { kind: 'unknown-no-crossing-source', crossingNodeIds: [], crossing: null }; return [ arm.key, { @@ -164,7 +164,7 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { const stopOffset = crossingControls.get(crossingKey)?.stopLineOffsetMeters || 3; const stopCenter = pointOnCarriagewayRadius(item, center, crossingRadius + stopOffset, helpers); const crossingEvidence = crossingPlans.get(arm.key)?.evidence || { - kind: 'inferred-road-envelope', + kind: 'unknown-no-crossing-source', crossingNodeIds: [], }; const ring = [ @@ -239,16 +239,30 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { arm.crosswalkRadius = armCrosswalkRadius; const envelope = armEnvelopeAtRadius(arm, armCrosswalkRadius, center, helpers); if (!envelope) continue; + const evidence = crossingPlans.get(arm.key)?.evidence || { + kind: 'unknown-no-crossing-source', + crossingNodeIds: [], + }; + if (evidence.kind !== 'source-crossing-node') { + transitionDiagnostics.push( + helpers.diagnostic( + 'info', + `crossing:${arm.key}`, + arm.members.flatMap((item) => item.approach.roadIds.map(String)), + 'complex-junction-crossing-source-missing', + '该进口未找到匹配的 OSM 过街节点,过街状态未知,未生成斑马线。', + envelope.center, + ), + ); + continue; + } arm.crosswalkFrame = { center: envelope.center, groupDepth: 3.4, supportHeading: normalizeHeading(arm.heading + 90), envelopeWidthMeters: envelope.widthMeters, }; - arm.crosswalkEvidence = crossingPlans.get(arm.key)?.evidence || { - kind: 'inferred-road-envelope', - crossingNodeIds: [], - }; + arm.crosswalkEvidence = evidence; } for (let index = 0; index < arms.length; index += 1) { const arm = arms[index]; @@ -354,19 +368,6 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { ); return true; }; - for (const arm of arms) { - if (!arm.crosswalkFrame || arm.crosswalkEvidence.kind !== 'inferred-road-envelope') continue; - cornerDiagnostics.push( - helpers.diagnostic( - 'info', - `crossing:${arm.key}`, - arm.members.flatMap((member) => member.approach.roadIds.map(String)), - 'complex-junction-crossing-inferred', - '该进口未找到与道路及当前位置匹配的 OSM 过街节点;已按最终车行道范围推断斑马线和停止线。', - arm.crosswalkFrame.center, - ), - ); - } const omittedCornerFacilityCount = arms.filter((arm, index) => { const next = arms[(index + 1) % arms.length]; const delta = positiveHeadingDelta(arm.heading, next.heading); @@ -663,8 +664,8 @@ function buildComplexJunctionGeometry(model, cluster, helpers) { [...nodeIds], calibration ? 'complex-junction-reference-calibrated' : 'complex-junction-generated', calibration - ? `已识别为${shape.label},使用参考几何校准参数后重新生成 ${approaches.length} 个进口、道路面、斑马线和停止线。` - : `已识别为${shape.label},独立生成 ${approaches.length} 个进口、道路面、斑马线和停止线。`, + ? `已识别为${shape.label},使用参考几何校准参数后重新生成 ${approaches.length} 个进口、道路面、${crosswalks.length} 组斑马线和停止线。` + : `已识别为${shape.label},独立生成 ${approaches.length} 个进口、道路面、${crosswalks.length} 组斑马线和停止线。`, center, ), ], @@ -900,7 +901,7 @@ function crossingEvidenceForArm(model, arm, center, helpers) { ); const crossingNodeIds = crossings.map((crossing) => String(crossing.id)); return { - kind: crossingNodeIds.length ? 'source-crossing-node' : 'inferred-road-envelope', + kind: crossingNodeIds.length ? 'source-crossing-node' : 'unknown-no-crossing-source', crossingNodeIds, crossing: crossings[0] || null, }; diff --git a/src/compile/direct-edit-solver.js b/src/compile/direct-edit-solver.js index 40e62e8..2db1f0a 100644 --- a/src/compile/direct-edit-solver.js +++ b/src/compile/direct-edit-solver.js @@ -317,7 +317,7 @@ function clusterArmRadiusPoint(arm, center, radius) { function clusterCrossingBaseRadius(model, arm, center, inferredRadius) { const inferredCenter = clusterArmRadiusPoint(arm, center, inferredRadius); - if (!inferredCenter) return inferredRadius; + if (!inferredCenter) return null; const osmWayIds = new Set( arm.members.flatMap((member) => member.roads.flatMap((road) => (road.osmWayIds || []).map(String))), ); @@ -336,7 +336,7 @@ function clusterCrossingBaseRadius(model, arm, center, inferredRadius) { haversineMeters(first.coordinate, inferredCenter) - haversineMeters(second.coordinate, inferredCenter) || String(first.id).localeCompare(String(second.id)), )[0]; - return crossing ? haversineMeters(center, crossing.coordinate) : inferredRadius; + return crossing ? haversineMeters(center, crossing.coordinate) : null; } function clusterArms(clusterId, items) { @@ -1055,9 +1055,29 @@ function solveConstraints(model, constraints, junctionData, diagnostics, cluster (candidate) => `crossing:${candidate.key}` === constraint.anchor?.crossingKey, ); const value = Number(constraint.value?.insetMeters ?? constraint.value?.offsetMeters); + const crossingSourceRadius = arm + ? clusterCrossingBaseRadius( + model, + arm, + clusterCenter( + model, + clusters.find((item) => String(item.id) === clusterId), + ), + Math.max( + 2, + complexJunctionMetrics(clusters.find((item) => String(item.id) === clusterId)).coreRadius * 0.68, + ), + ) + : null; const min = constraint.kind === 'cluster-crossing-inset' ? 0 : MIN_STOP_LINE_OFFSET_METERS; const max = constraint.kind === 'cluster-crossing-inset' ? MAX_CROSSWALK_INSET_METERS : 12; - if (!arm || !Number.isFinite(value) || value < min || value > max) { + if ( + !arm || + (constraint.kind === 'cluster-crossing-inset' && !Number.isFinite(crossingSourceRadius)) || + !Number.isFinite(value) || + value < min || + value > max + ) { blocking( diagnostics, constraint, @@ -1796,32 +1816,35 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters) const pointAt = (distance) => clusterArmRadiusPoint(arm, center, distance) || coordinateAt(line, Math.min(0.9, Math.max(0.05, distance / length))); - const baseCrosswalkDistance = clusterCrossingBaseRadius( + const sourceCrossingDistance = clusterCrossingBaseRadius( model, arm, center, Math.max(2, complexJunctionMetrics(cluster).coreRadius * 0.68), ); + const baseCrosswalkDistance = + sourceCrossingDistance ?? Math.max(2, complexJunctionMetrics(cluster).coreRadius * 0.68); const insetConstraint = constraintFor(constraints, 'cluster-crossing-inset', anchor); const inset = numericValue(insetConstraint, 'insetMeters', 0); - handles.push({ - handleId: `handle:cluster-crossing-inset:${crossingKey}`, - kind: 'cluster-crossing-inset', - anchor, - position: pointAt(baseCrosswalkDistance + inset), - axisAzimuth: arm.heading, - value: { current: inset, min: 0, max: MAX_CROSSWALK_INSET_METERS, unit: 'meter' }, - ...(insetConstraint ? { constraintId: insetConstraint.id } : {}), - affects: [`complex-crosswalk:${clusterId}`], - editable: true, - }); + if (Number.isFinite(sourceCrossingDistance)) + handles.push({ + handleId: `handle:cluster-crossing-inset:${crossingKey}`, + kind: 'cluster-crossing-inset', + anchor, + position: pointAt(baseCrosswalkDistance + inset), + axisAzimuth: arm.heading, + value: { current: inset, min: 0, max: MAX_CROSSWALK_INSET_METERS, unit: 'meter' }, + ...(insetConstraint ? { constraintId: insetConstraint.id } : {}), + affects: [`complex-crosswalk:${clusterId}`], + editable: true, + }); const stopConstraint = constraintFor(constraints, 'cluster-stop-line-offset', anchor); const offset = numericValue(stopConstraint, 'offsetMeters', 3); handles.push({ handleId: `handle:cluster-stop-line-offset:${crossingKey}`, kind: 'cluster-stop-line-offset', anchor, - position: pointAt(baseCrosswalkDistance + inset + offset), + position: pointAt(baseCrosswalkDistance + (Number.isFinite(sourceCrossingDistance) ? inset : 0) + offset), axisAzimuth: arm.heading, value: { current: offset, min: MIN_STOP_LINE_OFFSET_METERS, max: 12, unit: 'meter' }, ...(stopConstraint ? { constraintId: stopConstraint.id } : {}), diff --git a/test/index.js b/test/index.js index a2bf785..d0d54ad 100644 --- a/test/index.js +++ b/test/index.js @@ -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', ); diff --git a/test/workbench-edit-api.js b/test/workbench-edit-api.js index ec38f9b..e09562e 100644 --- a/test/workbench-edit-api.js +++ b/test/workbench-edit-api.js @@ -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(/]*)>/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(/]*)>([\s\S]*?)<\/way>/g, (way, attributes) => { + const wayId = attribute(attributes, 'id'); + if (!sourceRoad.osmWayIds.includes(wayId)) return way; + const refs = [...way.matchAll(/]*)\/>/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(``), (node) => { + insertedCrossing = true; + return `${node}`; + }); + }) + .replace( + '', + ``, + ); +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),