offshoreCorridors.test.ts 5.95 KB
/**
 * 海上合法走廊(S2)的判定测试。
 *
 * 走廊是离线、随仓库提交的静态 GeoJSON(public/geo/shenzhen-offshore-corridors.geojson),
 * 这里用合成几何测「解析 + 命中判定 + 容灾」,落水判定本身复用真实的 shenzhen-land.geojson。
 */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';
import { parseCorridors, pointInLegalCorridor, type CorridorFeature } from './offshoreCorridors';
import { parseDistrictGeometry } from './districtShapes';
import { gcj02ToWgs84 } from '@hm/clean-core';
import { classifyOffshore } from '../data/cleanRules';

const LAND = JSON.parse(
  readFileSync(resolve(__dirname, '../../../../public/geo/shenzhen-land.geojson'), 'utf8'),
) as unknown;
const landShapes = parseDistrictGeometry(LAND);

const GEOJSON = JSON.parse(
  readFileSync(
    resolve(__dirname, '../../../../public/geo/shenzhen-offshore-corridors.geojson'),
    'utf8',
  ),
) as unknown;

describe('parseCorridors', () => {
  it('解析 LineString / Point / MultiLineString / Polygon 四种几何', () => {
    const fc = {
      type: 'FeatureCollection',
      features: [
        {
          type: 'Feature',
          properties: { name: '桥', bufferM: 500 },
          geometry: { type: 'LineString', coordinates: [[113.8, 22.6], [113.9, 22.6]] },
        },
        {
          type: 'Feature',
          properties: { name: '岛', bufferM: 1200 },
          geometry: { type: 'Point', coordinates: [114.0, 22.5] },
        },
        {
          type: 'Feature',
          properties: { name: '多线', bufferM: 300 },
          geometry: { type: 'MultiLineString', coordinates: [[[114.1, 22.5], [114.2, 22.5]]] },
        },
        {
          type: 'Feature',
          properties: { name: '港区', bufferM: 800 },
          geometry: { type: 'Polygon', coordinates: [[[114.3, 22.5], [114.31, 22.5], [114.31, 22.51], [114.3, 22.51], [114.3, 22.5]]] },
        },
      ],
    };
    const c = parseCorridors(fc);
    expect(c).toHaveLength(4);
    expect(c[0].name).toBe('桥');
    expect(c[0].bufferM).toBe(500);
    expect(c[0].paths[0]).toEqual([[113.8, 22.6], [113.9, 22.6]]);
    expect(c[1].paths[0]).toEqual([[114.0, 22.5]]);
    expect(c[2].paths).toHaveLength(1);
    expect(c[3].polygon).toBeDefined();
  });

  it('缺省 bufferM 用 200,忽略不支持的几何与缺坐标的要素', () => {
    const fc = {
      type: 'FeatureCollection',
      features: [
        { type: 'Feature', properties: { name: '无缓冲' }, geometry: { type: 'Point', coordinates: [114.0, 22.5] } },
        { type: 'Feature', properties: { name: '坏点' }, geometry: { type: 'Point', coordinates: [114.0] } },
        { type: 'Feature', properties: { name: '不支持' }, geometry: { type: 'MultiPolygon', coordinates: [] } },
      ],
    };
    const c = parseCorridors(fc);
    expect(c).toHaveLength(1);
    expect(c[0].bufferM).toBe(200);
  });

  it('真实走廊文件能解析出 7 条结构', () => {
    const c = parseCorridors(GEOJSON);
    expect(c.length).toBe(7);
    expect(c.map((x) => x.name)).toContain('深中通道(含西人工岛 / 深中隧道)');
  });
});

describe('pointInLegalCorridor', () => {
  const corridors: CorridorFeature[] = [
    { name: '桥', bufferM: 500, paths: [[[113.8, 22.6], [113.9, 22.6]]] },
    { name: '岛', bufferM: 1200, paths: [[[114.0, 22.5]]] },
    {
      name: '港区',
      bufferM: 300,
      paths: [],
      polygon: [[[114.3, 22.5], [114.31, 22.5], [114.31, 22.51], [114.3, 22.51], [114.3, 22.5]]],
    },
  ];

  it('null 走廊直接返回未命中(容灾:数据没就绪时交回上层按陆地掩膜处理)', () => {
    expect(pointInLegalCorridor(113.85, 22.6, null).hit).toBe(false);
  });

  it('离折线 0.001° 内的点命中(约百米级,< 500m 缓冲)', () => {
    const m = pointInLegalCorridor(113.85, 22.601, corridors);
    expect(m.hit).toBe(true);
    expect(m.name).toBe('桥');
  });

  it('离折线很远的点不命中', () => {
    expect(pointInLegalCorridor(113.85, 22.9, corridors).hit).toBe(false);
  });

  it('Point 走廊:半径内的点命中、半径外不命中', () => {
    expect(pointInLegalCorridor(114.0, 22.501, corridors).name).toBe('岛');
    expect(pointInLegalCorridor(114.0, 22.52, corridors).hit).toBe(false); // 约 2.2km,超 1200m
  });

  it('Polygon 走廊:面内与面边界缓冲内都命中', () => {
    expect(pointInLegalCorridor(114.305, 22.505, corridors).name).toBe('港区'); // 面内
    expect(pointInLegalCorridor(114.302, 22.5, corridors).name).toBe('港区'); // 面边界外 ~220m,< 300m
    expect(pointInLegalCorridor(114.5, 22.5, corridors).hit).toBe(false);
  });
});

describe('classifyOffshore · 走廊豁免', () => {
  const SEA_A = { lng: 114.563342, lat: 22.600221 }; // 大鹏外海,GCJ-02

  it('不加走廊:海上点被标落水', () => {
    const r = classifyOffshore(SEA_A.lng, SEA_A.lat, landShapes);
    expect(r.flag).toBeTruthy();
    expect(r.corridor).toBe('');
  });

  it('加一条覆盖该点(WGS-84)的走廊:豁免标记,corridor 字段回写结构名', () => {
    const [wlng, wlat] = gcj02ToWgs84(SEA_A.lng, SEA_A.lat);
    const corridors: CorridorFeature[] = [
      { name: '测试合法岛', bufferM: 5000, paths: [[[wlng, wlat]]] },
    ];
    const r = classifyOffshore(SEA_A.lng, SEA_A.lat, landShapes, corridors);
    expect(r.flag).toBe(0);
    expect(r.corridor).toBe('测试合法岛');
    expect(r.note).toContain('海上合法走廊');
  });

  it('陆域点即使有走廊也不受影响(走廊只豁免海上点)', () => {
    const LAND_PT = { lng: 114.057, lat: 22.543 };
    const [wlng, wlat] = gcj02ToWgs84(LAND_PT.lng, LAND_PT.lat);
    const corridors: CorridorFeature[] = [
      { name: '覆盖市区的假走廊', bufferM: 50000, paths: [[[wlng, wlat]]] },
    ];
    const r = classifyOffshore(LAND_PT.lng, LAND_PT.lat, landShapes, corridors);
    expect(r.flag).toBe(0);
    expect(r.corridor).toBe('');
  });
});