Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jangsoopark/5865edbdf801a4a5e452bae9e31eb56e to your computer and use it in GitHub Desktop.
Save jangsoopark/5865edbdf801a4a5e452bae9e31eb56e to your computer and use it in GitHub Desktop.

Revisions

  1. fronteer-kr revised this gist Jun 25, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions 기상청 격자 <-> 위경도 변환
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    // 소스출처 : http://www.kma.go.kr/weather/forecast/digital_forecast.jsp 내부에 있음
    // 기상청에서 이걸 왜 공식적으로 공개하지 않을까?
    //
    // (사용 예)
    // var rs = dfs_xy_conv("toLL","60","127");
    // console.log(rs.lat, rs.lng);
    //

    <script language="javascript">
    //<!--
  2. fronteer-kr created this gist Jun 25, 2014.
    78 changes: 78 additions & 0 deletions 기상청 격자 <-> 위경도 변환
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    // 소스출처 : http://www.kma.go.kr/weather/forecast/digital_forecast.jsp 내부에 있음
    // 기상청에서 이걸 왜 공식적으로 공개하지 않을까?

    <script language="javascript">
    //<!--
    //
    // LCC DFS 좌표변환을 위한 기초 자료
    //
    var RE = 6371.00877; // 지구 반경(km)
    var GRID = 5.0; // 격자 간격(km)
    var SLAT1 = 30.0; // 투영 위도1(degree)
    var SLAT2 = 60.0; // 투영 위도2(degree)
    var OLON = 126.0; // 기준점 경도(degree)
    var OLAT = 38.0; // 기준점 위도(degree)
    var XO = 43; // 기준점 X좌표(GRID)
    var YO = 136; // 기1준점 Y좌표(GRID)
    //
    // LCC DFS 좌표변환 ( code : "toXY"(위경도->좌표, v1:위도, v2:경도), "toLL"(좌표->위경도,v1:x, v2:y) )
    //


    function dfs_xy_conv(code, v1, v2) {
    var DEGRAD = Math.PI / 180.0;
    var RADDEG = 180.0 / Math.PI;

    var re = RE / GRID;
    var slat1 = SLAT1 * DEGRAD;
    var slat2 = SLAT2 * DEGRAD;
    var olon = OLON * DEGRAD;
    var olat = OLAT * DEGRAD;

    var sn = Math.tan(Math.PI * 0.25 + slat2 * 0.5) / Math.tan(Math.PI * 0.25 + slat1 * 0.5);
    sn = Math.log(Math.cos(slat1) / Math.cos(slat2)) / Math.log(sn);
    var sf = Math.tan(Math.PI * 0.25 + slat1 * 0.5);
    sf = Math.pow(sf, sn) * Math.cos(slat1) / sn;
    var ro = Math.tan(Math.PI * 0.25 + olat * 0.5);
    ro = re * sf / Math.pow(ro, sn);
    var rs = {};
    if (code == "toXY") {
    rs['lat'] = v1;
    rs['lng'] = v2;
    var ra = Math.tan(Math.PI * 0.25 + (v1) * DEGRAD * 0.5);
    ra = re * sf / Math.pow(ra, sn);
    var theta = v2 * DEGRAD - olon;
    if (theta > Math.PI) theta -= 2.0 * Math.PI;
    if (theta < -Math.PI) theta += 2.0 * Math.PI;
    theta *= sn;
    rs['x'] = Math.floor(ra * Math.sin(theta) + XO + 0.5);
    rs['y'] = Math.floor(ro - ra * Math.cos(theta) + YO + 0.5);
    }
    else {
    rs['x'] = v1;
    rs['y'] = v2;
    var xn = v1 - XO;
    var yn = ro - v2 + YO;
    ra = Math.sqrt(xn * xn + yn * yn);
    if (sn < 0.0) - ra;
    var alat = Math.pow((re * sf / ra), (1.0 / sn));
    alat = 2.0 * Math.atan(alat) - Math.PI * 0.5;

    if (Math.abs(xn) <= 0.0) {
    theta = 0.0;
    }
    else {
    if (Math.abs(yn) <= 0.0) {
    theta = Math.PI * 0.5;
    if (xn < 0.0) - theta;
    }
    else theta = Math.atan2(xn, yn);
    }
    var alon = theta / sn + olon;
    rs['lat'] = alat * RADDEG;
    rs['lng'] = alon * RADDEG;
    }
    return rs;
    }
    //-->
    </script>