CollisionAvoidance/src/spatial/CoordinateConverter.cpp

35 lines
1.1 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 "CoordinateConverter.h"
#include <cmath>
void CoordinateConverter::setReferencePoint(double lat, double lon) {
ref_lat_ = lat * M_PI / 180.0; // 转换为弧度
ref_lon_ = lon * M_PI / 180.0;
cos_ref_lat_ = std::cos(ref_lat_);
}
Vector2D CoordinateConverter::toLocalXY(double lat, double lon) const {
// 使用等角投影转换
double lat_rad = lat * M_PI / 180.0;
double lon_rad = lon * M_PI / 180.0;
// 计算相对于参考点的差值
double d_lon = lon_rad - ref_lon_;
double d_lat = lat_rad - ref_lat_;
// 转换为米使用WGS84椭球体参数
Vector2D result;
result.x = EARTH_RADIUS * d_lon * cos_ref_lat_; // 东西方向
result.y = EARTH_RADIUS * d_lat; // 南北方向
return result;
}
std::pair<double, double> CoordinateConverter::toLatLon(const Vector2D& xy) const {
double d_lon = xy.x / (EARTH_RADIUS * cos_ref_lat_);
double d_lat = xy.y / EARTH_RADIUS;
double lat = (ref_lat_ + d_lat) * 180.0 / M_PI;
double lon = (ref_lon_ + d_lon) * 180.0 / M_PI;
return {lat, lon};
}