본문 바로가기
develop/java

JAVA - 주소로 위경도 및 격자좌표 구하기

by hybr1d 2016. 12. 27.

public HashMap<String, Object> getLatLon(HashMap<String, Object> hashMap) throws Exception {

    HashMap<String, Object> resultMap = new HashMap<>();

   

    String json;

    StringBuilder sb = new StringBuilder();

    double doubleLat = 0.0;

    double doubleLon = 0.0;

     

    try {

    String location = hashMap.get("siteAddr").toString();

//     String addr = "http://maps.google.com/maps/api/geocode/json?address=";

    String addr = "https://apis.daum.net/local/geo/addr2coord?apikey=";

    String apiKey = "cd4e0a08d5f3d7549bcf28b75603275a";

    URL url = new URL(addr+apiKey+"&q=" + URLEncoder.encode(location, "UTF-8")+"&output=json");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestMethod("GET");

            conn.setRequestProperty("Content-type", "application/json");

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));


            String line;

            while ((line = rd.readLine()) != null) {

                sb.append(line);

            }

            rd.close();

            conn.disconnect();

    } catch (Exception e) {

    System.out.println(e.getMessage());

    }

    json = sb.toString();

    log.info("==========json  : " + json);

    JSONObject object = (JSONObject) JSONValue.parse(json);

    object = (JSONObject) object.get("channel");

    JSONArray array = (JSONArray) object.get("item");

    log.info("==========array  : " + array);

    for (Object o : array) {

    JSONObject object2 = (JSONObject) o;

    log.info("==========object2  : " + object2);

    String lat = object2.get("lat").toString();

    String lng = object2.get("lng").toString();

   

    doubleLat = Double.parseDouble(lat);

    doubleLon = Double.parseDouble(lng);

    }

    resultMap.put("lat", doubleLat);

    resultMap.put("lon", doubleLon);

    

    log.info("resultMap : " + resultMap);

    return resultMap;

    }


========================================================

 /** 위경도 좌표로 격자 X Y 좌표 구하기 */

@Override

public HashMap<String, Object> getGridXY(HashMap<String, Object> params) {

    double RE = 6371.00877; // 지구 반경(km)

        double GRID = 5.0; // 격자 간격(km)

        double SLAT1 = 30.0; // 투영 위도1(degree)

        double SLAT2 = 60.0; // 투영 위도2(degree)

        double OLON = 126.0; // 기준점 경도(degree)

        double OLAT = 38.0; // 기준점 위도(degree)

        double XO = 43; // 기준점 X좌표(GRID)

        double YO = 136; // 기1준점 Y좌표(GRID)

        HashMap<String, Object> resultMap = new HashMap<>();

        

        try {

        HashMap<String, Object> siteMap = dao.getSiteAddr(params); //현장 주소 가져오기

HashMap<String, Object> latLon = getLatLon(siteMap); //현장 주소를 통한 위경도 좌표 가져오기

double DEGRAD = Math.PI / 180.0;

       // double RADDEG = 180.0 / Math.PI;

 

double re = RE / GRID;

double slat1 = SLAT1 * DEGRAD;

double slat2 = SLAT2 * DEGRAD;

double olon = OLON * DEGRAD;

double olat = OLAT * DEGRAD;

 

double 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);

double sf = Math.tan(Math.PI * 0.25 + slat1 * 0.5);

sf = Math.pow(sf, sn) * Math.cos(slat1) / sn;

double ro = Math.tan(Math.PI * 0.25 + olat * 0.5);

ro = re * sf / Math.pow(ro, sn);

 

double lat = Double.parseDouble(latLon.get("lat").toString());

double lon = Double.parseDouble(latLon.get("lon").toString());

 

double ra = Math.tan(Math.PI * 0.25 + (lat) * DEGRAD * 0.5);

ra = re * sf / Math.pow(ra, sn);

double theta = lon * DEGRAD - olon;

if (theta > Math.PI)

theta -= 2.0 * Math.PI;

if (theta < -Math.PI)

theta += 2.0 * Math.PI;

theta *= sn;

 

resultMap.put("nx", Math.floor(ra * Math.sin(theta) + XO + 0.5));

resultMap.put("ny", Math.floor(ro - ra * Math.cos(theta) + YO + 0.5));

log.info("======= XY : "+resultMap);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        return resultMap;

}



'develop > java' 카테고리의 다른 글

JAVA API 호출  (0) 2017.12.20
poi 기본 샘플  (0) 2017.11.20
월 일별로 데이터 넣기  (0) 2016.11.01
JAVA 파일 업로드 및 썸네일 생성 및 FTP 파일 업로드  (0) 2016.05.18
형변환  (0) 2014.11.19