bbox.test.ts
3.82 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
/**
* 聚合请求框的回归:阶段 1a 从 MapStage 抽出,行为必须与原来逐字段一致。
*
* 第五十三轮现场(2026-11-26 日志):bbox 中心 − 地图中心 =
* (+0.0064138°, +0.0063357°),与 GCJ-02→BD-09 偏移逐位吻合;
* 修法在门面出参口收口。这里钉住"喂进来的视口已是用户坐标系"这条前提:
* queryBox 自身不许再引入任何中心漂移。
*/
import { describe, expect, it } from 'vitest';
import { queryBox, type DataBounds, type ViewportBounds } from './bbox';
const viewport: ViewportBounds = {
swLng: 114.11,
swLat: 22.53,
neLng: 114.13,
neLat: 22.54,
};
describe('queryBox 视口外扩', () => {
it('pad=0.5 时四边各外扩半个视口跨度,中心不漂移', () => {
const b = queryBox({ viewport, dataBounds: null });
const spanLng = viewport.neLng - viewport.swLng;
const spanLat = viewport.neLat - viewport.swLat;
expect(b.minLng).toBeCloseTo(viewport.swLng - spanLng * 0.5, 12);
expect(b.maxLng).toBeCloseTo(viewport.neLng + spanLng * 0.5, 12);
expect(b.minLat).toBeCloseTo(viewport.swLat - spanLat * 0.5, 12);
expect(b.maxLat).toBeCloseTo(viewport.neLat + spanLat * 0.5, 12);
expect((b.minLng + b.maxLng) / 2).toBeCloseTo((viewport.swLng + viewport.neLng) / 2, 12);
expect((b.minLat + b.maxLat) / 2).toBeCloseTo((viewport.swLat + viewport.neLat) / 2, 12);
});
it('显式 pad=0 时就是视口本身', () => {
const b = queryBox({ viewport, pad: 0 });
expect(b).toEqual({
minLng: viewport.swLng,
maxLng: viewport.neLng,
minLat: viewport.swLat,
maxLat: viewport.neLat,
});
});
});
describe('queryBox 数据域收口', () => {
const dataBounds: DataBounds = {
minLng: 114.108,
maxLng: 114.132,
minLat: 22.528,
maxLat: 22.542,
};
it('外扩越过数据域时,收到 bounds ± slack(默认 0.25 度)', () => {
const b = queryBox({ viewport, dataBounds });
expect(b.minLng).toBeCloseTo(Math.max(114.1, dataBounds.minLng - 0.25), 12);
expect(b.maxLng).toBeCloseTo(Math.min(114.14, dataBounds.maxLng + 0.25), 12);
expect(b.minLat).toBeCloseTo(Math.max(22.525, dataBounds.minLat - 0.25), 12);
expect(b.maxLat).toBeCloseTo(Math.min(22.545, dataBounds.maxLat + 0.25), 12);
});
it('slack 可显式收窄,仍先外扩再夹', () => {
const b = queryBox({ viewport, dataBounds, slack: 0.001 });
expect(b.minLng).toBeCloseTo(dataBounds.minLng - 0.001, 12);
expect(b.maxLng).toBeCloseTo(dataBounds.maxLng + 0.001, 12);
});
it('视口完全在数据域内时,数据域不改变外扩结果', () => {
const inner: ViewportBounds = {
swLng: 114.115,
swLat: 22.533,
neLng: 114.118,
neLat: 22.535,
};
const wide: DataBounds = {
minLng: 114,
maxLng: 115,
minLat: 22,
maxLat: 23,
};
expect(queryBox({ viewport: inner, dataBounds: wide })).toEqual(
queryBox({ viewport: inner, dataBounds: null }),
);
});
});
describe('queryBox 坐标系口径', () => {
it('第五十三轮的地图中心喂进来,输出框中心不得出现 BD-09 偏移', () => {
// 现场那帧 getCenter()(GCJ-02,门面出参已收口)
const centerLng = 114.1225467807987;
const centerLat = 22.533795858563273;
const vp: ViewportBounds = {
swLng: centerLng - 0.01,
swLat: centerLat - 0.005,
neLng: centerLng + 0.01,
neLat: centerLat + 0.005,
};
const b = queryBox({ viewport: vp });
expect((b.minLng + b.maxLng) / 2).toBeCloseTo(centerLng, 12);
expect((b.minLat + b.maxLat) / 2).toBeCloseTo(centerLat, 12);
// 偏移指纹:日志里实测的 +0.0064138° / +0.0063357°
expect((b.minLng + b.maxLng) / 2 - centerLng).not.toBeCloseTo(0.0064138, 6);
expect((b.minLat + b.maxLat) / 2 - centerLat).not.toBeCloseTo(0.0063357, 6);
});
});