알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)

[Flutter,Dart](신)동네예보정보조회서비스 기상청 API,위도 경도->좌표 본문

Flutter,Dart

[Flutter,Dart](신)동네예보정보조회서비스 기상청 API,위도 경도->좌표

백곳 2019. 7. 3. 00:02

이번에 공공 데이터 포털 의 API 중 동네 예보 정보 조회 서비스 API 를 사용할 일이 있어서

사용 하다가 동네를 찾을때 위도 경도를 사용하는것이 아닌 기상청 만의 특정 좌표를 사용 하더라구요.

잠시 당황 했지만 사용 법을 찬찬히 보겠습니다.

일단 첨부된 문서에는

위에

OpenAPI 사용자 활용가이드(기상청_신규 동네예보정보조회서비스) 압축 파일을 다운 받아서 보면

위에 좌표를 프로그램을 찾아야 합니다.

이렇게 친절히 C언어? 로 된 소스를 던져 줍니다.

요즘 들어 C언어로 개발을 기억이 거의 없네요 ㅎㅎ

이제 해당 로직 분석후 제가 C->Dart 를 바꿔어 보았습니다.

 

 

import 'dart:math';

class Weather_map_xy {
  int x;
  int y;
  Weather_map_xy(this.x, this.y);
}

class lamc_parameter {
  double Re; /* 사용할 지구반경 [ km ]      */
  double grid; /* 격자간격        [ km ]      */
  double slat1; /* 표준위도        [degree]    */
  double slat2; /* 표준위도        [degree]    */
  double olon; /* 기준점의 경도   [degree]    */
  double olat; /* 기준점의 위도   [degree]    */
  double xo; /* 기준점의 X 좌표  [격자거리]  */
  double yo; /* 기준점의 Y 좌표  [격자거리]  */
  int first; /* 시작여부 (0 = 시작)         */
}

Weather_map_xy changelaluMap(double longitude, double latitude) {
  int NX = 149; /* X 축 격자점 수 */
  int NY = 253; /* Y 축 격자점 수 */
  double PI, DEGRAD, RADDEG;
  double re, olon, olat, sn, sf, ro;
  double slat1, slat2, alon, alat, xn, yn, ra, theta;
  lamc_parameter map = lamc_parameter();
  map.Re = 6371.00877; // 지도반경
  map.grid = 5.0; // 격자간격 (km)
  map.slat1 = 30.0; // 표준위도 1
  map.slat2 = 60.0; // 표준위도 2
  map.olon = 126.0; // 기준점 경도
  map.olat = 38.0; // 기준점 위도
  map.xo = 210 / map.grid; // 기준점 X 좌표
  map.yo = 675 / map.grid; // 기준점 Y 좌표
  map.first = 0;
  if ((map).first == 0) {
    // PI = asin(1.0) * 2.0;
    PI = 3.1415926535897931;
    DEGRAD = PI / 180.0;
    RADDEG = 180.0 / PI;
    re = map.Re / map.grid;
    slat1 = map.slat1 * DEGRAD;
    slat2 = map.slat2 * DEGRAD;
    olon = map.olon * DEGRAD;
    olat = map.olat * DEGRAD;
    sn = tan(PI * 0.25 + slat2 * 0.5) / tan(PI * 0.25 + slat1 * 0.5);
    sn = log(cos(slat1) / cos(slat2)) / log(sn);
    sf = tan(PI * 0.25 + slat1 * 0.5);
    sf = pow(sf, sn) * cos(slat1) / sn;
    ro = tan(PI * 0.25 + olat * 0.5);
    ro = re * sf / pow(ro, sn);
    map.first = 1;
  }
  ra = tan(PI * 0.25 + latitude * DEGRAD * 0.5);
  ra = re * sf / pow(ra, sn);
  theta = longitude * DEGRAD - olon;
  if (theta > PI) theta -= 2.0 * PI;
  if (theta < -PI) theta += 2.0 * PI;
  theta *= sn;

  double x = (ra * sin(theta)) + map.xo;
  double y = (ro - ra * cos(theta)) + map.yo;
  x = x + 1.5;
  y = y + 1.5;
  return Weather_map_xy(x.toInt(), y.toInt());
}

 

아래와 같이 사용 하였습니다. 

      Weather_map_xy weathermapxy =
          changelaluMap(currentposition.longitude, currentposition.latitude);
      print('x='+weathermapxy.x.toString() + "y=" +weathermapxy.y.toString())

테스트 결과 

 

위와 같이 정확히 잘 나오네요 

Comments