react) 단기예보 사용자를 위한 경위도를 좌표값으로 바꿔주는 custom Hook

김명성·2022년 8월 3일
0

공공데이터스럽게 매우 불친절하다.

어찌저찌 경도 위도를 찾았더니, 경위도는 사용할 수 없고 좌표 값으로 입력하라고 한다...

다행히 경위도를 좌표값으로 변환해주는 자바스크립트 코드가 적혀있는 선인의 고서를 발견하여 조금이나마 React스럽게 바꾸어주었다.

import React from 'react';

interface LatLngTypes {
  geoY: number;
  geoX: number;
}

const useConvertLatLng = ({geoX,geoY}:LatLngTypes) => {

const RE = 6371.00877; // 지구 반경(km)
const GRID = 5.0; // 격자 간격(km)
const SLAT1 = 30.0; // 투영 위도1(degree)
const SLAT2 = 60.0; // 투영 위도2(degree)
const OLON = 126.0; // 기준점 경도(degree)
const OLAT = 38.0; // 기준점 위도(degree)
const XO = 43; // 기준점 X좌표(GRID)
const YO = 136; // 기준점 Y좌표(GRID)

    const DEGRAD = Math.PI / 180.0;
    const RADDEG = 180.0 / Math.PI;
    
    const re = RE / GRID;
    const slat1 = SLAT1 * DEGRAD;
    const slat2 = SLAT2 * DEGRAD;
    const olon = OLON * DEGRAD;
    const olat = OLAT * DEGRAD;
    
    let 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);
    let sf = Math.tan(Math.PI * 0.25 + slat1 * 0.5);
    sf = Math.pow(sf, sn) * Math.cos(slat1) / sn;
    let ro = Math.tan(Math.PI * 0.25 + olat * 0.5);
    ro = re * sf / Math.pow(ro, sn);
    let rs:any = {};

        rs['lat'] = geoY;
        rs['lng'] = geoX;
        let ra = Math.tan(Math.PI * 0.25 + (geoY) * DEGRAD * 0.5);
        
        ra = re * sf / Math.pow(ra, sn);
        
        
        let theta = geoX * 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);

    return {
      rs:rs
    };
};

export default useConvertLatLng;

위대한 고서
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=javaking75&logNo=220089575454

감사합니다 선생님


0개의 댓글