offshore.test.ts
8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* 坐标落水 / 域外(H 类)的判定测试。
*
* 这一类管不管用,完全取决于多边形有没有贴着海岸线,所以直接用仓库里那份
* 真实陆地掩膜(public/geo/shenzhen-land.geojson,由 Natural Earth 陆地数据裁剪出深圳陆块)
* 跑,而不是合成矩形。
*
* 同时保留一份「行政区边界」对照(public/geo/shenzhen.geojson):AMap 行政边界会把沿海区
* 的水域划进区界,海上点反而会被判成「在陆域内」—— 这正是之前海上点没被剔除的根因,
* 用一个断言钉住它,免得哪天又有人拿行政边界去判落水。
*
* ⚠️ 坐标系(这才是本测试真正要守住的隐性契约):
* - 出险案件坐标是 **GCJ-02**(见 ingestCore 写进 meta 的 `coordSystem: 'GCJ-02'`),
* 全程不转换直接喂高德底图;
* - 陆地掩膜 shenzhen-land.geojson 来自 Natural Earth,是 **WGS-84**;
* - 两套系差 ~500m,所以判落水前必须把案件点(GCJ-02)归一化到 WGS-84 再比,否则近岸点判反。
* `offshoreVerdict` 是纯 WGS-84 几何(它也被「行政边界对照」复用,那条必须 GCJ-02 对 GCJ-02);
* 转换只在 `classifyOffshore` 一处做。下面「坐标系」那一组测试专门钉这个。
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';
import { parseDistrictGeometry, offshoreVerdict } from '../geo/districtShapes';
import { gcj02ToWgs84, wgs84ToGcj02 } from '@hm/clean-core';
import { classifyOffshore } from './cleanRules';
import { ingestBytes } from './ingestCore';
const LAND_GEOJSON = JSON.parse(
readFileSync(resolve(__dirname, '../../../../public/geo/shenzhen-land.geojson'), 'utf8'),
) as unknown;
const ADMIN_GEOJSON = JSON.parse(
readFileSync(resolve(__dirname, '../../../../public/geo/shenzhen.geojson'), 'utf8'),
) as unknown;
/** 陆地掩膜(真正的海岸线)—— 这是生产路径用的判定源 */
const landShapes = parseDistrictGeometry(LAND_GEOJSON);
/** 行政区边界(不含水域区分)—— 只用作对照,证明它分辨不出海上点 */
const adminShapes = parseDistrictGeometry(ADMIN_GEOJSON);
/** 超人截图里两条:坐标落在海上 */
const SEA_A = { lng: 114.563342, lat: 22.600221 }; // 大鹏新区大鹏街道 附近海域
const SEA_B = { lng: 113.956424, lat: 22.502281 }; // 南山区蛇口街道 附近海域
/** 深圳市区内陆一点(市民中心附近),应当判在陆域 */
const LAND = { lng: 114.057, lat: 22.543 };
/** 东莞陆域一点(深圳市域外,但落在「陆地」上)—— 应当判 offLand(不在任何深圳区陆块内) */
const NEIGHBOR_LAND = { lng: 113.9, lat: 22.95 };
describe('offshoreVerdict · 陆地掩膜(生产判定源)', () => {
it('两条海上点都不在任何深圳陆块多边形内', () => {
expect(offshoreVerdict(SEA_A.lng, SEA_A.lat, landShapes).onLand).toBe(false);
expect(offshoreVerdict(SEA_B.lng, SEA_B.lat, landShapes).onLand).toBe(false);
});
it('市区内陆点落在陆域内', () => {
expect(offshoreVerdict(LAND.lng, LAND.lat, landShapes).onLand).toBe(true);
});
it('邻市陆域点(东莞)落在「陆地」上 → H 类不标(归属交给 C 类按地址文本判)', () => {
// 陆地掩膜是矩形裁剪出的整片陆地(含邻市陆地),所以东莞陆域点 onLand=true、
// 不会被 H 类误标为落水。H 类只标「落在海上」的点;邻市归属由 C 类(地址文本)负责。
expect(offshoreVerdict(NEIGHBOR_LAND.lng, NEIGHBOR_LAND.lat, landShapes).onLand).toBe(true);
});
it('海上点带出「距最近陆界」的距离(正数)', () => {
const v = offshoreVerdict(SEA_A.lng, SEA_A.lat, landShapes);
expect(Number.isFinite(v.distM)).toBe(true);
expect(v.distM).toBeGreaterThan(0);
});
});
describe('offshoreVerdict · 行政区边界对照(回归钉)', () => {
it('行政边界会把海上点误判成「在陆域内」—— 所以落水判定绝不能用它', () => {
// 这一条是根因证据:AMap 行政边界把大鹏外海 / 深圳湾的水域划进了区界,
// 用行政边界判落水,两条海上点都会是 onLand=true —— 海上点于是被漏掉、没被剔除。
expect(offshoreVerdict(SEA_A.lng, SEA_A.lat, adminShapes).onLand).toBe(true);
expect(offshoreVerdict(SEA_B.lng, SEA_B.lat, adminShapes).onLand).toBe(true);
});
});
describe('坐标系 · 案件点(GCJ-02) 必须归一化到 WGS-84 陆地掩膜', () => {
it('gcj02ToWgs84 与 wgs84ToGcj02 互为逆(误差 < 1e-7°)', () => {
const [gLng, gLat] = wgs84ToGcj02(114.057, 22.543);
const [bLng, bLat] = gcj02ToWgs84(gLng, gLat);
expect(Math.abs(bLng - 114.057)).toBeLessThan(1e-7);
expect(Math.abs(bLat - 22.543)).toBeLessThan(1e-7);
});
it('深圳一带 GCJ-02→WGS-84 偏移约 0.004–0.006° 经度 / 0.002–0.004° 纬度(~500m)', () => {
const [wLng, wLat] = gcj02ToWgs84(114.057, 22.543);
expect(114.057 - wLng).toBeGreaterThan(0.004);
expect(114.057 - wLng).toBeLessThan(0.006);
expect(Math.abs(22.543 - wLat)).toBeGreaterThan(0.002);
expect(Math.abs(22.543 - wLat)).toBeLessThan(0.004);
});
it('回归·海上近岸点(GCJ-02) 不转换误判陆域、转换后正确判落水', () => {
// 这一组坐标是从海岸带扫描里抓出来的真实「判反」点:把 GCJ-02 当 WGS-84 直接判,
// 会误以为在陆域(漏抓海上点);经 classifyOffshore 归一化后再判,才正确落水。
const SEA_GCJ = { lng: 113.805091, lat: 22.620097 };
expect(offshoreVerdict(SEA_GCJ.lng, SEA_GCJ.lat, landShapes).onLand).toBe(true); // 不转换:错判陆域
expect(classifyOffshore(SEA_GCJ.lng, SEA_GCJ.lat, landShapes).flag).toBeTruthy(); // 转换后:正确落水
});
it('回归·陆上近岸点(GCJ-02) 不转换误判落水、转换后正确保留', () => {
const LAND_GCJ = { lng: 113.892874, lat: 22.455950 };
expect(offshoreVerdict(LAND_GCJ.lng, LAND_GCJ.lat, landShapes).onLand).toBe(false); // 不转换:错判海上
expect(classifyOffshore(LAND_GCJ.lng, LAND_GCJ.lat, landShapes).flag).toBe(0); // 转换后:正确保留
});
});
describe('classifyOffshore', () => {
it('海上点(陆地掩膜)给 OFFSHORE 标记与说明', () => {
const r = classifyOffshore(SEA_A.lng, SEA_A.lat, landShapes);
expect(r.flag).toBeTruthy();
expect(r.note).toContain('不在任何深圳行政区陆域多边形内');
});
it('陆域点不给标记', () => {
expect(classifyOffshore(LAND.lng, LAND.lat, landShapes).flag).toBe(0);
});
it('边界没就绪(null)时整类跳过,不当误杀', () => {
expect(classifyOffshore(SEA_A.lng, SEA_A.lat, null).flag).toBe(0);
});
});
describe('ingestBytes · OFFSHORE 进清洗报告', () => {
const csv = [
'案件号,出险地点,报案时间,经度,纬度,行政区,街道,道路',
'90578002800029787319,大鹏新区大鹏街道,2026-09-29 20:31:00,114.563342,22.600221,大鹏新区,大鹏街道,',
'90576002800029561018,南山区蛇口街道,2026-09-25 14:44:00,113.956424,22.502281,南山区,蛇口街道,',
'90570000000000000000,福田区华富街道,2026-09-25 14:44:00,114.057,22.543,福田区,华富街道,',
].join('\n');
it('注入陆地掩膜:两条海上点进 OFFSHORE_NEAR / OFFSHORE_FAR 之一,内陆点不进', async () => {
const bytes = new TextEncoder().encode(csv).buffer;
// 直接注入真实陆地掩膜几何,绕开 Worker 里的 fetch
const res = await ingestBytes('probe.csv', bytes, undefined, landShapes);
const near = res.report.classes.find((c) => c.key === 'OFFSHORE_NEAR');
const far = res.report.classes.find((c) => c.key === 'OFFSHORE_FAR');
const total = (near?.count ?? 0) + (far?.count ?? 0);
expect(total).toBe(2);
// 两类都默认不勾选(默认保留、不剔除)
expect(near?.defaultOn ?? false).toBe(false);
expect(far?.defaultOn ?? false).toBe(false);
// 内陆那条不进任何 offshore 类:OFFSHORE_NEAR=1<<11, OFFSHORE_FAR=1<<12,正好 2 行命中其一
const OFFSHORE_BITS = (1 << 11) | (1 << 12);
const offshoreRows = (res.report.flags as Uint16Array).filter(
(f) => (f & OFFSHORE_BITS) !== 0,
).length;
expect(offshoreRows).toBe(2);
});
it('不注入边界时(离线)OFFSHORE 计数为 0,不误杀', async () => {
const bytes = new TextEncoder().encode(csv).buffer;
const res = await ingestBytes('probe.csv', bytes, undefined, null);
const total = (res.report.classes ?? [])
.filter((c) => c.key === 'OFFSHORE_NEAR' || c.key === 'OFFSHORE_FAR')
.reduce((s, c) => s + c.count, 0);
expect(total).toBe(0);
expect(res.report.count).toBe(3);
});
});