同一参考椭球下不同坐标系转换
投影坐标转经纬度
经纬度转投影坐标
头文件和宏定义
#include <proj_api.h> #include <string> #define RAD_TO_DEG 57.295779513082321 #define DEG_TO_RAD .017453292519943296
定义坐标系
projPJ pj_from_; projPJ pj_to_; int zone = 51; //UTM std::string to_coordinate = "+proj=utm +zone=" + std::to_string(zone) + " +ellps=WGS84 +datum=WGS84 +units=m +no_defs"; //WGS84 std::string from_coordinate = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
坐标系赋值函数
bool CoordinateConvertTool::SetConvertParam(from_coordinate,to_coordinat)
{
if (pj_from_)
{ pj_free(pj_from_); pj_from_ = nullptr; } if (pj_to_) { pj_free(pj_to_); pj_to_ = nullptr; } if (!(pj_from_ = pj_init_plus(from_coordinate.c_str()))) {return false; } if (!(pj_to_ = pj_init_plus(to_coordinate.c_str()))) { pj_free(pj_from_); pj_from_ = nullptr; return false; } return true; }
坐标转换函数
bool CoordinateConvertTool::CoordiateConvertToLatLong(const double utm_x, const double utm_y, const double utm_z, double* longitude, double* latitude, double* height_ellipsoid)
{ if (!pj_from_ || !pj_to_)
{ return false; } double gps_longitude = utm_x; double gps_latitude = utm_y; double gps_alt = utm_z; if (pj_is_latlong(pj_from_))
{ gps_longitude *= DEG_TO_RAD; gps_latitude *= DEG_TO_RAD; gps_alt = utm_z; } if (0 != pj_transform(pj_from_,pj_to_, 1, 1, &gps_longitude, &gps_latitude, &gps_alt))
{return false; } if (pj_is_latlong(pj_to_))
{ gps_longitude *= RAD_TO_DEG; gps_latitude *= RAD_TO_DEG; } *longitude = gps_longitude; *latitude = gps_latitude; *height_ellipsoid = gps_alt; return ture; }