【问题标题】:Abnormal output when using proj4 to transform latlong to UTM使用proj4将latlong转换为UTM时输出异常
【发布时间】:2014-09-17 00:08:59
【问题描述】:

以下代码将 latlong 传输到 utm 以获取日本的一个位置点。但是,utm 结果完全不正常,如下所示。有人可以帮忙吗?举个例子更好。谢谢。 ***0.607968 2.438016 -14***

   #include "proj_api.h"
   #include "stdio.h"
   main(int argc, char **argv) {
        projPJ pj_utm, pj_latlong;
        double x = 34.8;
        double y = 138.4;

        if (!(pj_utm = pj_init_plus("+proj=utm +zone=54 +ellps=WGS84")) ){
                       printf("pj_init_plus error");
           exit(1);
           }
        if (!(pj_latlong = pj_init_plus("+proj=latlong +ellps=WGS84")) ){
                       printf("pj_init_plus error");
           exit(1);
           }

           x *= DEG_TO_RAD;
           y *= DEG_TO_RAD;
          int  p = pj_transform(pj_latlong, pj_utm, 1, 1, &x, &y, NULL );
           printf("%.2f\t%.2f\n", x, y);
        exit(0);
   }

【问题讨论】:

  • 期望得到什么价值?

标签: c++ proj


【解决方案1】:

我注意到你没有检查pj_transform上的错误代码,所以我抓住了它并亲自检查了。

它正在返回-14。负返回码通常表示错误。

在 PROJ.4 文档中的一些挖掘发现 pj_strerrno 函数返回与错误代码相关的错误消息。据此,我使用了这个函数,发现-14的意思是latitude or longitude exceeded limits

我检查了您的代码并发现了这一点:

double x = 34.8;
double y = 138.4;

显然,y 应该在[-90,90] 范围内。您的坐标命名错误。

正确命名您的坐标会产生262141.18N 3853945.50E 的结果,正如预期的那样。

我的代码如下:

//Compile with: gcc cheese.cpp -lproj
#include <proj_api.h>
#include <stdio.h>
main(int argc, char **argv) {
  projPJ pj_latlong, pj_utm;
  double y = 34.8;
  double x = 138.4;

  if (!(pj_latlong = pj_init_plus("+proj=longlat +datum=WGS84")) ){
    printf("pj_init_plus error: longlat\n");
    exit(1);
  }
  if (!(pj_utm = pj_init_plus("+proj=utm +zone=54 +ellps=WGS84")) ){
    printf("pj_init_plus error: utm\n");
    exit(1);
  }

  x *= DEG_TO_RAD;
  y *= DEG_TO_RAD;
  int p = pj_transform(pj_latlong, pj_utm, 1, 1, &x, &y, NULL );
  printf("Error code: %d\nError message: %s\n", p, pj_strerrno(p));
  printf("%.2fN\t%.2fE\n", x, y);
}

【讨论】:

    猜你喜欢
    • 2017-03-29
    • 2019-04-08
    • 2012-07-15
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2018-06-28
    相关资源
    最近更新 更多