【问题标题】:Converting from UTM to LongLat using Proj4 in C++在 C++ 中使用 Proj4 从 UTM 转换为 LongLat
【发布时间】:2017-03-29 13:54:00
【问题描述】:

我已经解决这个问题好几天了,但无法找到我做错了什么的解释。我希望你能帮我一把。

我有一组 UTM 坐标 (epsg:23030),我想通过使用 C++ 的 proj4 库 (libproj-dev) 将其转换为 LongLat 坐标 (epsg:4326)。我的代码如下:

#include "proj_api.h
#include <geos/geom/Coordinate.h>

geos::geom::Coordinate utm2longlat(double x, double y){ 

    // Initialize LONGLAT projection with epsg:4326
    if ( !( pj_longlat = pj_init_plus("+init=epsg:4326" ) ) ){
       qDebug() << "pj_init_plus error: longlat";
    }

    // Initialize UTM projection with epsg:23030
    if ( ! (pj_utm = pj_init_plus("+init=epsg:23030" ) ) ){
       qDebug() << "pj_init_plus error: utm";
    }

    // Transform UTM projection into LONGLAT projection
    int p = pj_transform( pj_utm, pj_longlat, 1, 1, &x, &y, NULL );

    // Check for errors
    qDebug() << "Error message" << pj_strerrno( p ) ;

    // Return values as coordinate
    return geos::geom::Coordinate(x, y)
}

我对函数utm2longlat的调用:

...
// UTM coordinates
double x =  585363.1;
double y =  4796767.1;
geos::geom::Coordinate coord = utm2longlat( x, y );

qDebug() << coord.x << coord.y;

/* Result is -0.0340087 0.756025    <-- WRONG */

在我的例子中:

  • 我知道UTM坐标(585363.1 4796767.1)指的是LongLat坐标(-1.94725 43.3189)
  • 但是,当调用该函数时,该函数返回一组错误的坐标:(-0.0340087 0.756025 )

我想知道在初始化投影时是否有任何错误配置,所以我决定测试 Proj4 Python 绑定(pyproj),只是为了测试我是否得到了相同的错误坐标……奇怪的是,我得到了好的坐标.

from pyproj import Proj, transform

// Initialize UTM projection
proj_utm = Proj(init='epsg:23030')
// Initialize LongLat projection 
proj_lonlat = Proj(init='epsg:4326')

x_utm, y_utm = 585363.1, 4796767.1

x_longlat, y_longlat = transform(proj_utm, proj_lonlat, x_utm, y_utm)

// Print results
print "original", x_utm, y_utm    
print "utm2lonlat", x_longlat, y_longlat  

/* Result is -1.94725 43.3189  <-- CORRECT */

据我了解,pyprojProj4 库上的一组 Cython 绑定,因此我在两种编程语言中使用相同的核心。

您对可能出现的问题有任何线索吗?我是否错过了 C++ 函数中的某种类型的转换?

提前致谢。

【问题讨论】:

    标签: python c++ geospatial proj


    【解决方案1】:

    结果对我来说似乎是正确的,但它以弧度而不是度数返回。将结果转换为度数并再次检查。

    【讨论】:

    • 就是这样,非常非常感谢!!如果可以的话,我会投票赞成你的答案,但我还没有足够的声誉。我想 Python 绑定会隐式进行转换。希望这对其他人有帮助。
    猜你喜欢
    • 2014-09-17
    • 2019-04-08
    • 2023-03-20
    • 2015-12-25
    • 2018-09-05
    • 2019-11-03
    • 2013-01-15
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多