geopro/tests/render/test_tile_math.cpp

49 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include <cmath>
#include "ground/TileMath.hpp"
using geopro::render::lonLatToTile;
using geopro::render::tileBounds;
// z=1 把世界分 2x2原点(0°,0°)在东/南象限交界 → 标准 slippy 取 (1,1)。
TEST(TileMath, OriginZoom1) {
auto t = lonLatToTile(0.0, 0.0, 1);
EXPECT_EQ(t.z, 1);
EXPECT_EQ(t.x, 1);
EXPECT_EQ(t.y, 1);
}
// z=1 西北瓦片 (0,0) 覆盖西半球北部west=-180, east=0, north≈85.0511(墨卡托上限), south=0。
TEST(TileMath, BoundsZoom1NW) {
auto b = tileBounds(1, 0, 0);
EXPECT_NEAR(b.west, -180.0, 1e-6);
EXPECT_NEAR(b.east, 0.0, 1e-6);
EXPECT_NEAR(b.north, 85.0511287798, 1e-4);
EXPECT_NEAR(b.south, 0.0, 1e-6);
}
// 往返一致:任一经纬点所属瓦片的边界必须包含该点(经度严格、纬度含墨卡托方向)。
TEST(TileMath, RoundTripContains) {
const double lon = 116.391, lat = 39.907; // 北京附近
const int z = 12;
auto t = lonLatToTile(lon, lat, z);
EXPECT_EQ(t.z, z);
auto b = tileBounds(t.z, t.x, t.y);
EXPECT_GE(lon, b.west);
EXPECT_LE(lon, b.east);
EXPECT_LE(lat, b.north); // north 是瓦片上边界(纬度大)
EXPECT_GE(lat, b.south);
}
// 夹紧:超界经纬不应产生越界瓦片索引。
TEST(TileMath, ClampInRange) {
auto t = lonLatToTile(500.0, 95.0, 3); // 非法输入
const int hi = (1 << 3) - 1;
EXPECT_GE(t.x, 0);
EXPECT_LE(t.x, hi);
EXPECT_GE(t.y, 0);
EXPECT_LE(t.y, hi);
}