| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /**
- * 主场景区计划(票 36):把配置里的 `map.primaryArea` 翻译成「限位与自建雾能不能开、开在哪个矩形」。
- *
- * 纯函数、不 import cesium,和 domain/basemap.js、domain/mapLayers.js 同一模式:
- * 逻辑脚本因此能脱浏览器把四类输入(合法 / 顺序写反 / 缺字段 / 数值个数不对)都测到。
- *
- * 契约来源 —— 决策票 `cockpit-primary-area-guardrails/issues/01` 的 Answer(Q1/Q2/Q3),
- * 本模块只消费,不重新解释:
- * · `map.primaryArea.bounds` = `[west, south, east, north]`(度),与 `map.overlays[].bounds` 同口径;
- * · **部署配置自己写好归一化后的顺序**(甲方给的 `[W, N, E, S]` 在准备配置时转换),
- * 运行时代码**绝不自动翻转**,校验要求 `west < east`、`south < north`;
- * · 未配置 → 关闭依赖主场景区的区域限位与自建雾(票 37 / 38);顺序非法同样走关闭路径,
- * 原因进配置问题清单、也进浮标与 `data-*` 读数。
- *
- * 为什么「顺序写反」必须走关闭而不是兜底翻转:翻对了没人发现配置是错的,翻错了
- * (经纬度真的写乱)就是拿一段假范围去锁相机。两条路都会让部署问题在运行期隐身。
- */
- /** 主场景区三态(写进宿主 `data-map-primary-area`,也是 getLifecycleStats().primaryArea 的取值)。 */
- export const PRIMARY_AREA_STATE = {
- READY: 'ready',
- MISSING: 'missing',
- INVALID: 'invalid',
- }
- /** 浮标与提示语里用的短名字(CONTEXT.md「主场景区」)。 */
- export const PRIMARY_AREA_LABEL = '主场景区'
- /** 关闭路径的统一后半句:让人一眼看出「不只是没配,是两个能力一起没了」。 */
- export const PRIMARY_AREA_OFF_SUFFIX = '限位与雾气关闭'
- /**
- * 关闭原因的唯一措辞:配置问题清单(`map.primaryArea.bounds …,已按关闭处理`)与引擎浮标
- * (`主场景区:… · 限位与雾气关闭`)都从这一份取,两边不会各写各的,原因也不会在链路上丢。
- */
- export const PRIMARY_AREA_REASON = {
- MISSING: '未配置',
- SHAPE: 'bounds 应为四个有限数字',
- ORDER: '顺序应为 [西,南,东,北],不自动翻转',
- }
- /** 度 → 公里的粗略换算(只用于浮标上的人读尺寸,不参与任何判定)。 */
- const KM_PER_DEG = 111.32
- /** 四个有限数字才算形状合法;顺序另判(见下)。 */
- function isBoundsShape(value) {
- return Array.isArray(value) && value.length === 4 && value.every((n) => typeof n === 'number' && Number.isFinite(n))
- }
- function withState(state, extra) {
- return {
- state,
- enabled: state === PRIMARY_AREA_STATE.READY,
- bounds: null,
- reason: '',
- sizeText: '',
- ...extra,
- }
- }
- /**
- * 主场景区计划:**顺序合法性的唯一判据**(配置归一化也调它,不写第二份)。
- * @param {object} mapConfig 归一化后的 `config.map`
- * @returns {{state: string, enabled: boolean, bounds: number[]|null, reason: string, sizeText: string}}
- */
- export function planPrimaryArea(mapConfig = {}) {
- const area = (mapConfig && mapConfig.primaryArea) || {}
- const raw = area.bounds
- // 归一化层给一个显式 `configured`,区分「部署没写这个字段」与「写了但里面是空的」:
- // 前者是「未配置」,后者是**写错了**,必须报出来而不是当成没配(否则 `primaryArea: {}`
- // 会被读成一句无害的省略)。直接调用本函数(逻辑脚本)时按「有没有给 bounds」判断。
- const configured = typeof area.configured === 'boolean' ? area.configured : raw !== undefined && raw !== null
- if (!configured) {
- return withState(PRIMARY_AREA_STATE.MISSING, { reason: PRIMARY_AREA_REASON.MISSING })
- }
- if (!isBoundsShape(raw)) {
- return withState(PRIMARY_AREA_STATE.INVALID, { reason: PRIMARY_AREA_REASON.SHAPE })
- }
- const [west, south, east, north] = raw
- if (!(west < east) || !(south < north)) {
- return withState(PRIMARY_AREA_STATE.INVALID, { reason: PRIMARY_AREA_REASON.ORDER })
- }
- return withState(PRIMARY_AREA_STATE.READY, {
- bounds: raw.slice(),
- sizeText: sizeTextOf(west, south, east, north),
- })
- }
- /** 矩形的近似尺寸(东西按中纬度收窄)。仅供人读。 */
- function sizeTextOf(west, south, east, north) {
- const midLat = (south + north) / 2
- const widthKm = (east - west) * KM_PER_DEG * Math.cos((midLat * Math.PI) / 180)
- const heightKm = (north - south) * KM_PER_DEG
- return `${widthKm.toFixed(1)}×${heightKm.toFixed(1)} km`
- }
- /**
- * 引擎浮标上的主场景区措辞(票 36 验收第 4 条:生效与关闭都要人读得到)。
- * 生效说尺寸;关闭说原因 + 「限位与雾气关闭」,不把「没配」写成一句无害的省略。
- * @param {object} plan planPrimaryArea() 的结果
- */
- export function primaryAreaStatusText(plan) {
- const current = plan || planPrimaryArea({})
- if (current.enabled) return `${PRIMARY_AREA_LABEL}:${current.sizeText}`
- // 未配置时不再复述一遍原因("未配置(未配置)"是把同一个词说了两次);
- // 配错时原因要留着 —— 个数错与顺序错是两种修法,排查时不能只看到一句"无效"。
- const head =
- current.state === PRIMARY_AREA_STATE.INVALID ? `配置无效(${current.reason})` : PRIMARY_AREA_REASON.MISSING
- return `${PRIMARY_AREA_LABEL}:${head} · ${PRIMARY_AREA_OFF_SUFFIX}`
- }
|