geopro/tests/core/test_geo_frame.cpp

62 lines
1.9 KiB
C++
Raw Permalink 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 "geo/GeoLocalFrame.hpp"
using geopro::core::GeoLocalFrame;
namespace {
constexpr double kPi = 3.14159265358979323846;
}
// 原点投影到 (0,0)。
TEST(GeoFrame, OriginMapsToZero) {
GeoLocalFrame f(22.5, 114.16);
auto p = f.toLocal(22.5, 114.16);
EXPECT_NEAR(p.x, 0.0, 1e-9);
EXPECT_NEAR(p.y, 0.0, 1e-9);
}
// 东向 0.01 度经度x>0约 0.01*111320*cos(22.5°)≈1028m5% 容差)。
TEST(GeoFrame, EastwardLongitudeGivesPositiveX) {
GeoLocalFrame f(22.5, 114.16);
auto p = f.toLocal(22.5, 114.17);
const double expected = 0.01 * 111320.0 * std::cos(22.5 * kPi / 180.0);
EXPECT_GT(p.x, 0.0);
EXPECT_NEAR(p.x, expected, expected * 0.05);
EXPECT_NEAR(p.y, 0.0, 1e-9);
}
// 北向 0.01 度纬度y≈0.01*110540≈1105m5% 容差)。
TEST(GeoFrame, NorthwardLatitudeGivesPositiveY) {
GeoLocalFrame f(22.5, 114.16);
auto p = f.toLocal(22.51, 114.16);
const double expected = 0.01 * 110540.0;
EXPECT_GT(p.y, 0.0);
EXPECT_NEAR(p.y, expected, expected * 0.05);
EXPECT_NEAR(p.x, 0.0, 1e-9);
}
// toLatLon 是 toLocal 的反算toLocal∘toLatLon 与 toLatLon∘toLocal 都恒等。
TEST(GeoFrame, ToLatLonRoundTrips) {
GeoLocalFrame f(22.5, 114.16);
// 经纬度 → 局部 → 经纬度 恒等。
auto p = f.toLocal(22.53, 114.19);
auto ll = f.toLatLon(p.x, p.y);
EXPECT_NEAR(ll.lat, 22.53, 1e-9);
EXPECT_NEAR(ll.lon, 114.19, 1e-9);
// 局部 → 经纬度 → 局部 恒等。
auto q = f.toLocal(ll.lat, ll.lon);
EXPECT_NEAR(q.x, p.x, 1e-6);
EXPECT_NEAR(q.y, p.y, 1e-6);
}
// 原点局部 (0,0) 反算回 (lat0,lon0)。
TEST(GeoFrame, ToLatLonOriginMapsToLat0Lon0) {
GeoLocalFrame f(22.5, 114.16);
auto ll = f.toLatLon(0.0, 0.0);
EXPECT_NEAR(ll.lat, 22.5, 1e-12);
EXPECT_NEAR(ll.lon, 114.16, 1e-12);
}