문의 시, 사용하시는 개발환경과 디벨로퍼스 앱ID를 알려주세요.
안녕하세요 파이썬으로 엑셀에 연결하고 그후 좌표를찾아 길찾기 환경을 만들려고 합니다 그런데 안되는 곳이 있네요 안되는 이유가 뭔가요
import pandas as pd
import requests
api_key = “````”
df = pd.read_excel(“C:\Users\user\Desktop\새 폴더\123.xlsx”)
addresses = df[“주소”].tolist()[:500]
url = “https://dapi.kakao.com/v2/local/geo/coord2address.json”
headers = {“Authorization”: f"KakaoAK (```````)"}
def get_address_by_coords(coords):
params = {"x": coords[0], "y": coords[1]}
response = requests.get(url, headers=headers, params=params)
result_json = response.json()
address = result_json["documents"][0]["address"]["address_name"]
return address
path = []
for address in addresses:
params = {"query": address}
response = requests.get(url, headers=headers, params=params)
result_json = response.json()
coords = result_json["documents"][0]["y"], result_json["documents"][0]["x"]
path.append(",".join(map(str, coords)))
def calculate_distance(coords1, coords2):
url = (
f"https://dapi.kakao.com/v2/local/geo/transcoord.json?"
f"y1={coords1[1]}&x1={coords1[0]}&y2={coords2[1]}&x2={coords2[0]}&input_coord=WGS84&output_coord=WGS84"
)
response = requests.get(url, headers=headers)
result_json = response.json()
distance = result_json["documents"][0]["distance"]
return distance
distances = {}
for i in range(len(path)):
for j in range(i, len(path)):
if i == j:
distances[(i, j)] = float("inf")
else:
coords1 = path[i].split(",")
coords2 = path[j].split(",")
distance = calculate_distance(coords1, coords2)
distances[(i, j)] = distance
distances[(j, i)] = distance
sorted_distances = sorted(distances.items(), key=lambda x: x[1])
sorted_distances = [item for item in sorted_distances if item[1] != float(“inf”)]
visited = []
result_path = []
for item in sorted_distances:
if item[0][0] not in visited:
visited.append(item[0][0])
result_path.append(addresses[item[0][0]])
if item[0][1] not in visited:
visited.append(item[0][1])
result_path.append(addresses[item[0][1]])
if len(result_path) == len(addresses):
break
print(result_path)