latitude, longitude -> hjdcode 변환

ewillwin·2023년 1월 19일
0

TSMtech Record

목록 보기
30/39
from urllib.request import urlopen
import requests as rq
from bs4 import BeautifulSoup as bs
from typing import final
from venv import create
import pandas as pd
from multiprocessing import Process
from typing import final
from venv import create
import requests
from decimal import Decimal
import re
import xml.etree.ElementTree as ET
import time
import json
import datetime
import sys
import googlemaps

APP_KEY = '비밀이셈'

headers = {'Authorization': "KakaoAK 비밀이셈"}

def get_hjd(x, y):
    URL = 'https://dapi.kakao.com/v2/local/geo/coord2regioncode.json'
    URL += '?x=' + str(x) + '&y=' + str(y)
    response = requests.get(URL, headers=headers)
    response_json = json.loads(response.text)
    hjd = response_json['documents'][1]['code']
    return hjd


# ttt = gethjd(126.3987149, 34.7905132)
# print(ttt)

src = open("C:/Users/TSM/OneDrive/바탕 화면/alladdr/forhjd.csv", "r", encoding="cp949")
dest = open("C:/Users/TSM/OneDrive/바탕 화면/alladdr/hjd.csv", 'w', encoding="cp949")

idx = 0
#startidx = 98573
for line in src:
    linelist = line.split(',')
    lat = linelist[2]
    lng = linelist[3][:-1]
    hjdcode = get_hjd(lng, lat)
    # print(hjdcode)
    if hjdcode is not None:
        tmp = linelist[0] + ',' + linelist[1] + ',' + str(hjdcode) + '\n'
    else:
        tmp = linelist[0] + ',' + linelist[1] + ',' + "False" + '\n'
    dest.write(tmp)
    
src.close()
dest.close()
  • googlemaps API로 얻은 위도, 경도를 이용하여 kakaomap API를 통해 행정동코드로 변환하는 코드
  • kakaomap API 일일 트래픽 제한 -> 100,000회
  • 위도, 경도가 추출되지 않는 지번주소의 경우를 제외하고 모두 행정동코드 변환이 가능한듯

참고 사항

  • 행정기준코드와 행정동코드 구별
  • 법정동코드와 행정동코드의 시도 및 시군구 코드 부분 (앞에서부터 5자리)은 동일함
profile
Software Engineer @ LG Electronics

0개의 댓글