1

以下代码是为了将 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);
   }
4

1 回答 1

3

我注意到你没有检查错误代码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);
}
于 2014-12-24T20:32:30.083 回答