#ifndef COORDINATE_PROJ_HPP
#define COORDINATE_PROJ_HPP
#include "proj.h"
#include <stdlib.h>
#include <stdio.h>
#include <exception>
#define LOG_I()
class proj_impl
{
public:
proj_impl() {
C = nullptr;
P = nullptr;
P_for_GIS = nullptr;
}
inline static int get_zone(double lon) { return int((lon + 1.5)/3.0); }
inline static int get_lon_0(double lon) { return get_zone(lon) * 3; }
~proj_impl(){
release();
}
void setconv(int _conv){
conv = _conv;
init(glon);
}
bool init(double lon = 0) {
try {
glon = lon;
release();
//merc
C = proj_context_create();
if (conv == 0){
char psztmp[32] = "";
sprintf(psztmp, "+proj=tmerc +lon_0=%d +x_0=500000", get_lon_0(lon));
P = proj_create_crs_to_crs(C, "EPSG:4326", "+proj=tmerc +lon_0=117 +x_0=500000", nullptr);
}
else if (conv == 1) {
P = proj_create_crs_to_crs(C, "EPSG:4326", "+proj=geocent +units=m", nullptr);
}
if (nullptr == P) {
fprintf(stderr, "Oops\n");
return false;
}
/* This will ensure that the order of coordinates for the input CRS */
/* will be longitude, latitude, whereas EPSG:4326 mandates latitude, */
/* longitude */
if( nullptr == (P_for_GIS = proj_normalize_for_visualization(C, P))) {
fprintf(stderr, "Oops\n");
return false;
}
proj_destroy(P);
P = P_for_GIS;
return true;
} catch (std::exception& exc) {
}
return false;
}
bool blhtoenu(double b,double l, double h, double& e,double& n,double& u) {
try {
if (conv != 0)
return false;
/* coordinates is longitude, latitude, and values are expressed in degrees. */
/* 40.098393475 116.147123333 */
in_coor = proj_coord (l, b, h, 0);
/* transform to UTM zone 32, then back to geographical */
out_coor = proj_trans (P, PJ_FWD, in_coor);
e = out_coor.enu.e;
n = out_coor.enu.n;
u = out_coor.enu.u;
return true;
} catch (std::exception& exc) {
}
return false;
}
bool blhtoecef(double b, double l, double h, double& x,double& y,double& z) {
try {
if (conv != 1)
return false;
/* coordinates is longitude, latitude, and values are expressed in degrees. */
/* 40.098393475 116.147123333 */
in_coor = proj_coord (l, b, h, 0);
/* transform to UTM zone 32, then back to geographical */
out_coor = proj_trans (P, PJ_FWD, in_coor);
x = out_coor.xyz.x;
y = out_coor.xyz.y;
z = out_coor.xyz.z;
return true;
} catch (std::exception& exc) {
}
return false;
}
bool eceftoblh(double x,double y, double z, double& b,double& l,double& h) {
try {
if (conv != 1)
return false;
/* coordinates is longitude, latitude, and values are expressed in degrees. */
/* 40.098393475 116.147123333 */
in_coor = proj_coord (x, y, z, 0);
/* transform to UTM zone 32, then back to geographical */
out_coor = proj_trans (P, PJ_INV, in_coor);
b = out_coor.lpz.lam;
l = out_coor.lpz.phi;
h = out_coor.lpz.z;
return true;
} catch (std::exception& exc) {
}
return false;
}
void release(){
if (P)
proj_destroy (P);
P = 0;
if (C)
proj_context_destroy (C); /* may be omitted in the single threaded case */
C = 0;
}
public:
PJ_CONTEXT *C;
PJ *P;
PJ* P_for_GIS;
PJ_COORD in_coor, out_coor;
int conv;
double glon;
};
#endif // COORDINATE_PROJ_HPP
proj6地理坐标转换
最新推荐文章于 2026-04-19 16:42:33 发布

2724

被折叠的 条评论
为什么被折叠?



