spotlightData.ts 5.07 KB
import { useHeatmapStore, type SpotlightView } from '@/store/useHeatmapStore';
import type { SpotlightCell, SpotlightResult } from '../aggregate/types';
import { districtShape } from '../geo/districtShapes';
import type { SpotlightData, SpotlightUnitPaint } from '../map/SpotlightLayer';
import { addCell, addPoint, emptyBounds, finishBounds } from '../map/spotlightGeometry';

/**
 * 榜单那一行换成屏上的一档,四种画法共用这一份。
 *
 * 起数以 Worker 回传的那一个为准 —— 它和屏上那些格子出自同一次归格,牌子念的数因此
 * 和涂出来的深浅必定是同一批记录。榜上那个数只在结果里没有这一档时兜底。
 *
 * 两者不等就是「屏上一数比榜上少几十起」那种一查就全盘失信的错,所以这里同时报到控制台:
 * 屏幕上宁可只念画得出来的那个数,也不把两个数并排摆给人对。
 *
 * 三种来源在这里收口,屏上一个数都不现算:
 *   - 区界:几何来自边界文件(见 geo/districtShapes),起数用榜上那一行 —— 这一档涂的是整片
 *     区域,没有「归格」这一步,深浅的分母是面积而不是格子;
 *   - 街道 / 道路:几何与起数都来自 Worker 那一次归格,榜上的数只用来对账;
 *   - 反复出险的位置:榜上每一行本来就绑着一个地理方格,格子与起数是同一条记录,不再问 Worker。
 */
export function toSpotlightData(view: SpotlightView, result: SpotlightResult | null): SpotlightData {
  // 区字典名单不在 view 里(view 只存下标),从当下 store 读:聚焦与区字典同源,
  // 名单换了意味着数据换了,那时整个聚焦本来就会被筛变动作收起(见 store 的 resetFilter)
  const districtNames =
    view.kind === 'districts' ? (useHeatmapStore.getState().meta?.dict.districts ?? []) : [];
  const units: SpotlightUnitPaint[] = view.units.map((u, i): SpotlightUnitPaint => {
    if (view.kind === 'districts') {
      // 取不到边界的那一档在组装时就已经被摘出名单(见 spotlightView.buildSpotlight),
      // 真走到这里只剩一种可能:数据在两次读取之间换了。宁可不画这片,也不拿 bbox 冒充边界。
      const shape = districtShape(districtNames, u.idx);
      if (!shape) {
        console.error('[spotlight] 区界与档位的下标对不上', { 档位: u.label, idx: u.idx });
      }
      return {
        label: u.label,
        count: u.count,
        sub: u.sub,
        cells: [],
        polys: shape?.polys ?? [],
        areaKm2: shape?.areaKm2 ?? 0,
        pin: shape ? { lng: shape.anchor[0], lat: shape.anchor[1] } : null,
      };
    }

    const src = result?.cells[i];
    if (!src) return { label: u.label, count: u.count, sub: u.sub, cells: u.cells ?? [] };
    if (src.count !== u.count) {
      console.error('[spotlight] 榜上起数与归格起数不一致', {
        档位: u.label,
        榜上: u.count,
        归格: src.count,
      });
    }
    const cells: SpotlightCell[] = [];
    for (let k = 0; k < src.lng.length; k += 1) {
      cells.push({ lng: src.lng[k], lat: src.lat[k], n: src.n[k] });
    }
    return { label: u.label, count: src.count, sub: u.sub, cells };
  });

  return {
    // 车辆属性那一屏没有自己的画法:它要画的就是「一类案件的一批落点」,
    // 与街道那一屏同构(同一套方格 + 牌子),只是取数时筛的是车辆属性那一列。
    // 硬给这一屏加一种 variant,等于让图层为一个已经存在的画法再写一遍分支
    variant: view.kind === 'attrs' ? 'streets' : view.kind,
    // 区界没有格子;位置用的是 stats 那把尺;街道、道路与车辆属性以 Worker 回传的为准
    cellM:
      view.kind === 'districts'
        ? 0
        : view.kind === 'places'
          ? view.cellM
          : (result?.cellM ?? view.cellM),
    units,
  };
}

/**
 * 这一屏要取景到哪一片。
 *
 * 用的是各自的数据来源给的地理范围,而不是屏幕上画出来的那截:拖到屏幕外的形状仍然算在这一档
 * 的范围里(与 SpotlightLayer 的命中表同一口径),否则「明明在屏上、取景却把我带到别处」。
 */
export function spotlightFrame(view: SpotlightView, result: SpotlightResult | null) {
  const acc = emptyBounds();
  if (view.kind === 'districts') {
    const names = useHeatmapStore.getState().meta?.dict.districts ?? [];
    for (const u of view.units) {
      const shape = districtShape(names, u.idx);
      if (!shape) continue;
      addPoint(acc, shape.bbox.minLng, shape.bbox.minLat);
      addPoint(acc, shape.bbox.maxLng, shape.bbox.maxLat);
    }
  } else if (result) {
    for (const group of result.cells) {
      addPoint(acc, group.bounds.minLng, group.bounds.minLat);
      addPoint(acc, group.bounds.maxLng, group.bounds.maxLat);
    }
  } else {
    // 反复出险的位置:格子本身就是范围,按边长铺开再并进来
    for (const u of view.units) {
      for (const c of u.cells ?? []) addCell(acc, c, view.cellM);
    }
  }
  return finishBounds(acc);
}