PROJ 7.2.1的安装与使用

一、简介

Ubuntu18.04 和Ubuntu20.04自带proj4库,没有的话也可以通过apt的方式安装proj-bin库,指令如下

sudo apt-get install proj-bin

ubunut18.04的proj4版本是4.9.3, ubuntu20.04的proj4的版本时候6.xxx。由于4.0到6.0版本api更新很大,所以我们从源码安装指定版本。

二、源码安装

1、下载

https://github.com/OSGeo/PROJ.git -b 7.2.1

2、编译

Build requirements

  • C99 compiler

  • C++11 compiler

  • SQLite3 >= 3.11 (headers, library and executable)

  • libtiff >= 4.0 (headers and library)

  • optional (but recommended): curl >= 7.29.0

  • GNU make for autotools build or CMake >= 3.9

安装依赖库

sudo apt install sqlite3

编译并安装

cd PROJ
mkdir build
cd build
cmake ..
cmake --build .
sudo cmake --build . --target install

#卸载
sudo xargs rm < install_manifest.txt

三、使用

1、代码调用

#include <stdio.h>
#include <proj.h>

int main (void) {
    PJ_CONTEXT *C;
    PJ *P;
    PJ *norm;
    PJ_COORD a, b;
  
    /* or you may set C=PJ_DEFAULT_CTX if you are sure you will     */
    /* use PJ objects from only one thread                          */
    C = proj_context_create();
    P = proj_create_crs_to_crs (C,
                                "EPSG:4326",
                                "+proj=utm +zone=32 +datum=WGS84", /* or EPSG:32632 */
                                NULL);
    if (0 == P) {
        fprintf(stderr, "Failed to create transformation object.\n");
        return 1;
    }


    /* This will ensure that the order of coordinates for the input CRS */
    /* will be longitude, latitude, whereas EPSG:4326 mandates latitude, */
    /* longitude */
    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;
  
    /* a coordinate union representing Copenhagen: 55d N, 12d E    */
    /* Given that we have used proj_normalize_for_visualization(), the order of
    /* coordinates is longitude, latitude, and values are expressed in degrees. */
    a = proj_coord(12, 55, 0, 0);
  
    /* transform to UTM zone 32, then back to geographical */
    b = proj_trans(P, PJ_FWD, a);
    printf("easting: %.3f, northing: %.3f\n", b.enu.e, b.enu.n);
  
    b = proj_trans(P, PJ_INV, b);
    printf("longitude: %g, latitude: %g\n", b.lp.lam, b.lp.phi);

    /* Clean up */
    proj_destroy(P);
    proj_context_destroy(C); /* may be omitted in the single threaded case */

    return 0;

}

 

find_package(PROJ)

target_link_libraries(MyApp PRIVATE ${PROJ_LIBRARIES})

2、命令行使用

#EPSG:4326 (WGS84) 经度 维度      EPSG:4526(CGCS2000/3-degree Gauss-Kruger zone 38 中央经线114)
echo 114.0994496 30.4262139 | cs2cs +init=epsg:4326 +to +init=epsg:4526
结果:笛卡尔X 笛卡尔Y 38为代号,并笛卡尔X加了500公里
38509554.26	3367365.98

 

posted @ 2021-11-30 14:12  chenjian688  阅读(2904)  评论(0编辑  收藏  举报