« 「ツナシマン」が注目されてる・・・? | メイン | Panoramio と思い出プラン »

libproj を使って平面直角座標(JGD2000)を緯度経度に変換する
libproj を使って平面直角座標(JGD2000)を緯度経度に変換するサンプルソースです。

国土地理院の発行する数値地図ではY,Xの順に入っているので、このツールも 入力は"Y X 座標系" の順に入力するようにしています。

#include <stdio.h>
#include <projects.h>

#define WKBUFSIZE 1024

static char* JGD2000_ZAHYOKEI[]
= {"+init=epsg:2443", "+init=epsg:2444", "+init=epsg:2445", "+init=epsg:2446",
   "+init=epsg:2447", "+init=epsg:2448", "+init=epsg:2449", "+init=epsg:2450",
   "+init=epsg:2451", "+init=epsg:2452", "+init=epsg:2453", "+init=epsg:2454",
   "+init=epsg:2455", "+init=epsg:2456", "+init=epsg:2457", "+init=epsg:2458",
   "+init=epsg:2459", "+init=epsg:2460", "+init=epsg:2461"};

int main()
{
  char* str_param[1];
  char  wkbuf[WKBUFSIZE];
  float x,y;
  int a;

  PJ* proj_param;
  XY idata;
  LP odata;

  while(fgets(wkbuf, WKBUFSIZE, stdin) != NULL){
    /* パラメータが Y,X の順なのに注意! */
    if(sscanf(wkbuf, "%f %f %d", &y, &x, &a) != 3){
        fprintf(stderr, "format error: %s\n", wkbuf);
        continue;
    }
    if(a < 1 || a > (sizeof JGD2000_ZAHYOKEI / sizeof JGD2000_ZAHYOKEI[0])){
      fprintf(stderr, "illegal number: %d\n", a);
      continue;
    }
    str_param[0] = JGD2000_ZAHYOKEI[a-1];
    idata.u = x;
    idata.v = y;
    proj_param = pj_init(sizeof(str_param)/sizeof(char *), str_param);
    odata = pj_inv(idata, proj_param);
    pj_free(proj_param);
    odata.u /= DEG_TO_RAD;
    odata.v /= DEG_TO_RAD;
    printf("%.6f %.6f\n", odata.u, odata.v);
  }
  return 0;
}


コンパイルは以下のようにします。
cc -g -o yx2lnglat yx2lnglat.c -lproj

利用は、例えば以下のように実行します。
echo -145112.8 -43458.0 6| ./yx2lnglat
(実行結果)
135.525678 34.690998

もっとも、単に変換するだけなら、プログラミングせずとも、 invproj コマンドを使えばOKです。

参考
http://bubble.atnifty.com/modules/bwiki/index.php?libproj
http://proj.maptools.org/man_pj_init.html

トラックバック

このエントリーのトラックバックURL:
http://www.mailpia.jp/cgi-bin/mt_32/mt-tb.cgi/82

コメントを投稿