proj6地理坐标转换

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值