basemapCoord.test.ts 4.05 KB
import { describe, expect, it, vi } from 'vitest';
import { BaiduBasemap } from './BaiduBasemap';
import { bd09ToGcj02 } from './coord';
import type { BaiduGLMap, BaiduGLNamespace, BaiduGLPoint } from './load';

/**
 * 回归:4.0 全局声明 GCJ-02 后,getBounds()/overlayPixelToPoint() 仍吐 BD-09。
 * 2026-11-26 现场:两帧只竖向平移 40px,同一团红核连同数字整块消失,
 * 取数框中心却比 getCenter() 偏 (+0.0064138°, +0.0063357°) —— 正是 BD-09 减 GCJ-02。
 * 框底边因此压在屏幕下半部,往那个方向拖就会把点拖出取数范围。
 */
const GCJ_CENTER = [114.1225467807987, 22.533795858563273] as const;
// 现场探针那一帧:外扩 0.5 前的原始 getBounds() 角点(BD-09)
const BD_BOX = {
  sw: { lng: 114.12067828325467, lat: 22.536868687240945 },
  ne: { lng: 114.1372428674532, lat: 22.54339444398659 },
};
const BD_PIXEL_POINT = { lng: 114.12896057535393, lat: 22.540131565613766 };

function fake(opts: { coordType?: string; crs?: string } = {}) {
  const point = (lng: number, lat: number): BaiduGLPoint =>
    ({ lng, lat }) as unknown as BaiduGLPoint;
  const map = {
    centerAndZoom: vi.fn(),
    getSize: () => ({ width: 1844, height: 782 }),
    getCenter: () => point(...GCJ_CENTER),
    getZoom: () => 18,
    getBounds: () => ({
      getSouthWest: () => point(BD_BOX.sw.lng, BD_BOX.sw.lat),
      getNorthEast: () => point(BD_BOX.ne.lng, BD_BOX.ne.lat),
    }),
    getCRS: opts.crs === undefined ? undefined : () => opts.crs,
    setZoom: vi.fn(),
    setMapStyle: vi.fn(),
    getContainer: () => document.createElement('div'),
    pointToOverlayPixel: () => ({ x: 922, y: 391 }),
    overlayPixelToPoint: () => point(BD_PIXEL_POINT.lng, BD_PIXEL_POINT.lat),
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    addLayer: vi.fn(),
    removeLayer: vi.fn(),
    destroy: vi.fn(),
  } as unknown as BaiduGLMap;
  const B = {
    coordType: opts.coordType ?? 'BMAP_COORD_GCJ02',
    Pixel: class {
      constructor(
        public x: number,
        public y: number,
      ) {}
    },
  } as unknown as BaiduGLNamespace;
  return new BaiduBasemap(B, map);
}

describe('百度门面坐标口径', () => {
  it('全局 GCJ + BD09 CRS:取数框从 BD-09 转回 GCJ-02,中心对齐 getCenter()', () => {
    const b = fake({ crs: 'BMAP_CRS_BD09' }).getBounds();
    const [swLng, swLat] = bd09ToGcj02(BD_BOX.sw.lng, BD_BOX.sw.lat);
    const [neLng, neLat] = bd09ToGcj02(BD_BOX.ne.lng, BD_BOX.ne.lat);
    expect(b.getSouthWest().getLng()).toBeCloseTo(swLng, 8);
    expect(b.getSouthWest().getLat()).toBeCloseTo(swLat, 8);
    expect(b.getNorthEast().getLng()).toBeCloseTo(neLng, 8);
    expect(b.getNorthEast().getLat()).toBeCloseTo(neLat, 8);
    // BD09 余弦扰动项让变换轻微非线性:两角转换后的中点与中心直接转换差 ~6e-6°,
    // 容差按 1e-4°(约 10m)给,足以区分「转了」与「没转」(后者差 0.0064°)。
    expect((b.getSouthWest().getLng() + b.getNorthEast().getLng()) / 2).toBeCloseTo(
      GCJ_CENTER[0],
      4,
    );
    expect((b.getSouthWest().getLat() + b.getNorthEast().getLat()) / 2).toBeCloseTo(
      GCJ_CENTER[1],
      4,
    );
  });

  it('全局 GCJ + BD09 CRS:反投影与数字/热力用的口径一致,命中不会偏 600m', () => {
    const p = fake({ crs: 'BMAP_CRS_BD09' }).containerToLngLat([10, 20]);
    expect(p.getLng()).toBeCloseTo(GCJ_CENTER[0], 6);
    expect(p.getLat()).toBeCloseTo(GCJ_CENTER[1], 6);
  });

  it('CRS 已是 GCJ02 时不再二次转换', () => {
    const b = fake({ crs: 'BMAP_CRS_GCJ02' }).getBounds();
    expect(b.getSouthWest().getLng()).toBeCloseTo(BD_BOX.sw.lng, 8);
    expect(b.getSouthWest().getLat()).toBeCloseTo(BD_BOX.sw.lat, 8);
    const p = fake({ crs: 'BMAP_CRS_GCJ02' }).containerToLngLat([10, 20]);
    expect(p.getLng()).toBeCloseTo(BD_PIXEL_POINT.lng, 8);
  });

  it('全局仍是 BD-09 时出参原样返回', () => {
    const b = fake({ coordType: 'BMAP_COORD_BD09', crs: 'BMAP_CRS_BD09' }).getBounds();
    expect(b.getSouthWest().getLng()).toBeCloseTo(BD_BOX.sw.lng, 8);
  });
});