projection.test.ts
2.36 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
/**
* 投影换算的守卫。
*
* 自绘层原先逐帧调用 map.lngLatToContainer 求屏幕位置,现在改成「每一点的墨卡托纬度
* 在喂数据时算一次,每帧只做乘加」。这个替换只有在**两者是同一种投影**时才不会让
* 屏幕上的方格、黑点盘、聚焦形状整体错位一截 —— 而观感问题在构建期测不出来,所以钉在这里。
*
* 拿包内那份网格公式(`grid.ts` 的 `pxPerDegLat`)当基准:聚合网格、热力半径、覆盖物半径
* 全部由它换算,也就是界面上「48 像素一格」这套直觉的尺子。`mercY` 是同一投影的积分形式,
* 两者必须对得上:把 mercY 在某个纬度上做微分,得到的像素/度数应与 pxPerDegLat 一致。
*/
import { describe, expect, it } from 'vitest';
import { mercY, pxPerDegLng } from '../src/frame';
import { pxPerDegLat } from '../src/grid';
const TILE = 256;
const ZOOM = 12;
/** 球形墨卡托的瓦片坐标 y:与高德 / OSM 那一套同一个式子 */
function pixelY(latDeg: number): number {
const world = TILE * Math.pow(2, ZOOM);
return ((1 - mercY(latDeg) / Math.PI) * world) / 2;
}
describe('mercY 与分格用的是同一支墨卡托', () => {
it('中心纬度上下各取一点,像素差除以纬度差就是每度像素', () => {
const lat = 22.55;
const d = 0.01;
const measured = (pixelY(lat - d) - pixelY(lat + d)) / (2 * d);
const expected = pxPerDegLat(ZOOM, lat, TILE);
// 中心差分对 0.01° 的量级误差远小于 0.1%
expect(measured).toBeCloseTo(expected, 2);
});
it('纬度越高,同一纬度差占的像素越多(墨卡托拉伸)', () => {
const span = (a: number, b: number) => Math.abs(pixelY(a) - pixelY(b));
expect(span(20, 21)).toBeLessThan(span(30, 31));
expect(span(30, 31)).toBeLessThan(span(40, 41));
});
it('纬度增大时屏幕 y 减小(北上、下为正向)', () => {
expect(pixelY(22.6)).toBeLessThan(pixelY(22.5));
});
it('经度方向是线性的:每度像素 = 那一级的世界宽度 ÷ 360', () => {
expect(pxPerDegLng(ZOOM, TILE)).toBe((TILE * Math.pow(2, ZOOM)) / 360);
});
it('超出墨卡托区间时按 ±85° 封顶,不会算出无穷大', () => {
expect(Number.isFinite(mercY(89))).toBe(true);
expect(mercY(89)).toBe(mercY(85));
expect(mercY(-89)).toBe(mercY(-85));
});
});