坐标转换系列二 基于开源的proj4
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <sstream>
#include <string>
#include "./proj4/include/proj_api.h"
#pragma comment(lib,"./proj4/lib/proj_i.lib")
using namespace std;
int main()
{
const char* wgs84 = "+proj=latlong +ellps=WGS84 +datum=NAD83";
const char* beijing1954 = "+proj=tmerc +ellps=krass +x_0=500000.0000000000 +y_0=0 +towgs84=77,-88,-99,0,0,0,0 +lon_0=111e +lat_0=0.0000000000 +units=m +no_defs";
projPJ pj_wgs84 = pj_init_plus(wgs84);
projPJ pj_bj54 = pj_init_plus(beijing1954);
double x = 106.48260558;
double y = 38.64279675;
double z = 0;
x *= DEG_TO_RAD;
y *= DEG_TO_RAD;
pj_transform(pj_wgs84, pj_bj54, 1, 1, &x, &y, NULL);
cout.setf(ios::fixed);
cout.precision(9);
cout.width(12);
cout << x << "\t" << y << "\t" << z << endl;
}
程序运行结果如下所示:
1、本程序基于proj4开源库,基于x86平台;
2、定义原始坐标系字符串const char* wgs84;
3、定义目标坐标系字符串const char* beijing1954;
4、分别构造projPJ;
5、将经纬度转换为弧度;
6、调用pj_transform函数,进行坐标转换;
7、转换结果输出并显示。
结论1:该篇文章介绍的坐标转换流程清晰、简洁;较上篇文章实用性更强。
结论2:两篇文章介绍的转换方法均能实现目标,精度相当。
结论3:多线程中推荐使用本文介绍的方法。
结论4:源码下载地址为:https://download.csdn.net/download/m0_38122606/15533345