PROJ简介
PROJ是一种通用坐标转换软件,用于将地理空间坐标从一个坐标参考系(coordinate reference system, CRS)转换为另一个坐标参考系。
PROJ官方网站:https://proj.org/
macOS系统可以直接使用homebrew安装:
brew install proj
系统和PROJ版本
系统:macOS Big Sur
PROJ版本:9.0.0
c++实现
wgs84_to_utm32651.cpp
#include <stdio.h>
#include <proj.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main (int argc, char **argv) {
// 变量定义
PJ_CONTEXT *C;
PJ *P;
PJ *norm;
PJ_COORD a, b;
ifstream input(argv[1]);
ofstream output(argv[2]);
string line;
double timestamp, latitude, longtitude, height;
// 创建一个上下文对象
C = proj_context_create();
// 创建转换对象
// 第二个参数"EPSG:4326"是源坐标系EPSG代码,第三个参数是目标坐标系EPSG代码
P = proj_create_crs_to_crs (C, "EPSG:4326", "EPSG:32651", NULL);
if (0 == P) {
fprintf(stderr, "Failed to create transformation object.\n");
return 1;
}
norm = proj_normalize_for_visualization(C, P);
if (0 == norm) {
fprintf(stderr, "Failed to normalize transformation object.\n");
return 1;
}
proj_destroy(P);
P = norm;
// 从文件中一行一行读取数据,并进行坐标系的转换
while (getline(input, line)){
istringstream record(line);
record >> timestamp >> latitude >> longtitude >> height;
// 初始化PJ_COORD对象
a = proj_coord(longtitude, latitude, height, 0);
// 将wgs84坐标系下的地理空间坐标 a 转换到utm32651坐标系下
b = proj_trans(P, PJ_FWD, a);
// 输出到文件中
output.precision(15);
output << b.enu.e << " " << b.enu.n << " " << b.enu.u << endl;
}
// 销毁所创建的对象
proj_destroy(P);
proj_context_destroy(C);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project( wgs84_to_utm32651 )
find_package(PROJ CONFIG REQUIRED)
add_executable ( wgs84_to_utm32651 src/wgs84_to_utm32651.cpp)
target_link_libraries( wgs84_to_utm32651 PROJ::proj )
编译运行即可。
使用QGIS验证
转换之后的坐标可以使用QGIS软件进行验证,如果坐标点都能够重叠在一起,说明转换正确。
QGIS官方网站:https://www.qgis.org/