PROJ4在Ubuntu18.04系统的编译过程及测试代码

        首先附上PROJ源代码的GitHub地址:https://github.com/Rogers0701/PROJ

        下载之后进入PROJ文件夹,Ctrl+Alt+T快捷键打开终端,输入以下命令

mkdir build && cd build//创建build文件夹并进入build文件夹中
cmake  -DCMAKE_INSTALL_PREFIX=./install -DBUILD_LIBPROJ_SHARED=OFF ..//cmake创建Makefile,链接静态库
make//进行编译
make install//进行安装

        在cmake时会下载一个https://github.com/google/googletest/archive/release-1.11.0.zip,可能会解压失败,是访问GitHub的问题,多试几次或者使用VPN就可以解决了。

        cmake完成后,在build文件夹中会出现Makefile文件,此时进行make编译,编译完成后,使用make install安装,安装完成build文件夹中出现install文件夹

 再进入install中的include文件夹,出现proj.h则表示编译成功!

PROJ8.0版本以后proj_api.h将会被删除,如果习惯旧版本的proj函数,可以在GitHub中的分支选择8.0以前的版本(推荐7.2.1)。函数用法可以在官方文档中查询,地址如下Functions — PROJ 8.2.1 documentation

后续文章我也会对一些常用的proj函数进行解释说明。以下为官方的示例代码

#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", //目标投影

                                NULL);//创建在线程C内两个投影关系之间的相互转换


    if (0 == P) {

        fprintf(stderr, "Failed to create transformation object.\n");

        return 1;

    }//如果P中两个投影的字符串不符合proj定义,提示转换失败


    /* 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);//在线程C内使投影目标P和norm拥有相同的坐标格式,此处为经纬度

    if (0 == norm) {

        fprintf(stderr, "Failed to normalize transformation object.\n");

        return 1;

    }//norm为0,说明格式同步失败

    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);//投影转换,fwd代表源投影转换成目标投影,INV代表目标投影转换为源投影

    printf("easting: %.3f, northing: %.3f\n", b.enu.e, b.enu.n);//经纬度转化为xy坐标


    b = proj_trans(P, PJ_INV, b);//xy坐标转化为经纬度坐标

    printf("longitude: %g, latitude: %g\n", b.lp.lam, b.lp.phi);//lam为经度,phi为纬度


    /* Clean up */

    proj_destroy(P);

    proj_context_destroy(C); /* may be omitted in the single threaded case */

    return 0;

}

cmakelist编写如下:

cmake_minimum_required(VERSION 3.10)//设置cmake版本
project(proj-test)//设置项目名称

set(CMAKE_CXX_STANDARD 14)//设置c++版本

include_directories(.)
include_directories(proj)//头文件的相对路径,此处只有proj.h一个头文件,因此此处为proj.h的所在文件夹的相对路径

find_package(PROJ)//查找PROJ外部库,必不可少

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)//查找pthread库
link_libraries(m)//链接标准库math,在cmake中用m代替,若cmake链接动态库,则以上三步不需要

add_executable(test-xodr test.cpp)//创建可执行文件,文件名为test-xodr
target_link_libraries(test-xodr Threads::Threads)
target_link_libraries(test-xodr m)//若链接动态库,以上两步和上述三步均不需要
target_link_libraries(test-xodr ${PROJ_LIBRARIES})//链接proj,pthread和math库

 进入终端输入以下命令

mkdir build && cd build
cmake ..
make

//运行创建的可执行文件
./test-xodr

运行结果如下:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值