bucketDots.test.ts
4.66 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
import { describe, expect, it } from 'vitest';
import { expandBucketsToDots } from './bucketDots';
/**
* 同圆契约的验法:点的散布是确定性的三角分布(±spread×格宽),有限样本均值
* 必然有统计波动,所以不拿「均值严格等于桶心」当断言。真正要保证的是:
* 每个点都落在自己桶心的 ±spread 范围内,且经纬度属于同一个桶 ——
* 这样热力峰不可能跑到别的桶去,数字圆心与热力峰严格同位。
*/
function expectWithin(values: Int32Array, from: number, to: number, center: number, radius: number) {
for (let i = from; i < to; i += 1) {
expect(Math.abs(values[i] - center)).toBeLessThanOrEqual(radius);
}
}
const SPREAD = 0.45;
const CELL = 1000; // dLng=0.001° → 存储刻度 1000
const R = SPREAD * CELL; // 单侧散布上限 450
describe('桶展开点阵(官方热力层同源喂数)', () => {
it('点阵围绕桶心散布:每桶的点都不越出自己的格,热力峰与数字圆心同圆', () => {
const lng = new Int32Array([114060000, 114062000, 114064000]);
const lat = new Int32Array([22620000, 22622000, 22624000]);
const count = new Int32Array([10, 100, 30]);
const dots = expandBucketsToDots(lng, lat, count, null, 140, SPREAD, 0.001, 0.001);
expect(dots.count).toBe(140); // 1 点/案
// 逐点归属:每个点必须贴着某一桶心(±450),且经纬度来自同一个桶 ——
// 不依赖分段顺序,直接锁死「点不会串到别的桶去」。
let bucket1Count = 0;
for (let i = 0; i < dots.count; i += 1) {
const near = [0, 1, 2].find((b) => Math.abs(dots.lng[i] - lng[b]) <= R);
expect(near).toBeGreaterThanOrEqual(0);
const b = near as number;
expect(Math.abs(dots.lat[i] - lat[b])).toBeLessThanOrEqual(R);
if (b === 1) bucket1Count += 1;
}
expect(bucket1Count).toBe(100); // 案量 100 的桶拿到 100 个点
});
it('案量越大分到越多点:热力主次由真实案量驱动', () => {
const lng = new Int32Array([114060000, 114062000, 114064000]);
const lat = new Int32Array([22620000, 22622000, 22624000]);
const count = new Int32Array([1, 100, 10]);
// 预算 111 / 总案量 111 = 1 点/案,低案量桶保底也是 1 点 → 恰好按案量分配
const dots = expandBucketsToDots(lng, lat, count, null, 111, SPREAD, 0.001, 0.001);
expect(dots.count).toBe(111);
expect(dots.count).toBe(count[0] + count[1] + count[2]);
});
it('过滤掩码生效:keep=0 的桶不产生任何点', () => {
const lng = new Int32Array([114060000, 114062000]);
const lat = new Int32Array([22620000, 22622000]);
const count = new Int32Array([50, 50]);
const keep = new Uint8Array([1, 0]);
const dots = expandBucketsToDots(lng, lat, count, keep, 100, SPREAD, 0.001, 0.001);
// 保留桶只有 50 个案;预算 100 不会把它放大成 100 点(上限语义)
expect(dots.count).toBe(50);
expectWithin(dots.lng, 0, 50, 114060000, R);
});
it('确定性:同样的桶两次展开得到逐字节相同的结果', () => {
const lng = new Int32Array([114060000, 114062000]);
const lat = new Int32Array([22620000, 22622000]);
const count = new Int32Array([7, 19]);
const a = expandBucketsToDots(lng, lat, count, null, 50, SPREAD, 0.001, 0.001);
const b = expandBucketsToDots(lng, lat, count, null, 50, SPREAD, 0.001, 0.001);
expect(a.count).toBe(b.count);
expect(Array.from(a.lng)).toEqual(Array.from(b.lng));
expect(Array.from(a.lat)).toEqual(Array.from(b.lat));
});
it('预算大于总案量时不放大:1 个案最多 1 点,绝不硬凑预算', () => {
const lng = new Int32Array([114060000, 114062000]);
const lat = new Int32Array([22620000, 22622000]);
const count = new Int32Array([10, 30]);
// 预算 35000 远大于总案量 40;旧实现会把每案放大成 875 点 → 局部全饱和 + 卡顿
const dots = expandBucketsToDots(lng, lat, count, null, 35000, SPREAD, 0.001, 0.001);
expect(dots.count).toBe(40);
});
it('空输入 / 全被过滤 / 零案量都返回空数组,不抛错', () => {
const empty = new Int32Array(0);
const r1 = expandBucketsToDots(empty, empty, empty, null, 100, SPREAD, 0.001, 0.001);
expect(r1.count).toBe(0);
const lng = new Int32Array([114060000]);
const lat = new Int32Array([22620000]);
const count = new Int32Array([10]);
const r2 = expandBucketsToDots(lng, lat, count, new Uint8Array([0]), 100, SPREAD, 0.001, 0.001);
expect(r2.count).toBe(0);
const zero = new Int32Array([0]);
const r3 = expandBucketsToDots(lng, lat, zero, null, 100, SPREAD, 0.001, 0.001);
expect(r3.count).toBe(0);
});
});