Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스
- 포스코 코딩테스트
- classifier-free guidance
- manganinja
- 포스코 채용
- dp
- kt인적성
- posco 채용
- KT
- controlNet
- stable diffusion
- 코딩테스트
- colorization
- 논문 리뷰
- Generative Models
- Image Generation
- ip-adapter
- ddim
- DDPM
- 과제형 코딩테스트
- diffusion models
Archives
- Today
- Total
Paul's Grit
[Python] [Data Visualization] Geocode AIP를 사용하여 위치 정보 불러오기 본문
Data Analysis/Visualization
[Python] [Data Visualization] Geocode AIP를 사용하여 위치 정보 불러오기
Paul-K 2023. 8. 10. 18:14Google Maps API 활용 지도 시각화¶
In [33]:
import googlemaps
gmaps_key = 'Your API Key'
gmaps = googlemaps.Client(key=gmaps_key)
geocode = gmaps.geocode(address='1547 N. Wells St Chicago')
geocode
Out[33]:
[{'address_components': [{'long_name': '1547', 'short_name': '1547', 'types': ['street_number']}, {'long_name': 'North Wells Street', 'short_name': 'N Wells St', 'types': ['route']}, {'long_name': 'Near North Side', 'short_name': 'Near North Side', 'types': ['neighborhood', 'political']}, {'long_name': 'Chicago', 'short_name': 'Chicago', 'types': ['locality', 'political']}, {'long_name': 'Cook County', 'short_name': 'Cook County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Illinois', 'short_name': 'IL', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '60610', 'short_name': '60610', 'types': ['postal_code']}], 'formatted_address': '1547 N Wells St, Chicago, IL 60610, USA', 'geometry': {'bounds': {'northeast': {'lat': 41.9105488, 'lng': -87.63420149999999}, 'southwest': {'lat': 41.9104894, 'lng': -87.6345554}}, 'location': {'lat': 41.9105258, 'lng': -87.6343775}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 41.91186808029151, 'lng': -87.63309861970849}, 'southwest': {'lat': 41.90917011970851, 'lng': -87.6357965802915}}}, 'place_id': 'ChIJDcIHTEHTD4gRv4ogAyUDBsE', 'types': ['premise']}]
"geocode"를 .json으로 저장해서 저장된 형태 확인해보기¶
In [34]:
import json
print(json.dumps(geocode, indent=1))
[ { "address_components": [ { "long_name": "1547", "short_name": "1547", "types": [ "street_number" ] }, { "long_name": "North Wells Street", "short_name": "N Wells St", "types": [ "route" ] }, { "long_name": "Near North Side", "short_name": "Near North Side", "types": [ "neighborhood", "political" ] }, { "long_name": "Chicago", "short_name": "Chicago", "types": [ "locality", "political" ] }, { "long_name": "Cook County", "short_name": "Cook County", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Illinois", "short_name": "IL", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "60610", "short_name": "60610", "types": [ "postal_code" ] } ], "formatted_address": "1547 N Wells St, Chicago, IL 60610, USA", "geometry": { "bounds": { "northeast": { "lat": 41.9105488, "lng": -87.63420149999999 }, "southwest": { "lat": 41.9104894, "lng": -87.6345554 } }, "location": { "lat": 41.9105258, "lng": -87.6343775 }, "location_type": "ROOFTOP", "viewport": { "northeast": { "lat": 41.91186808029151, "lng": -87.63309861970849 }, "southwest": { "lat": 41.90917011970851, "lng": -87.6357965802915 } } }, "place_id": "ChIJDcIHTEHTD4gRv4ogAyUDBsE", "types": [ "premise" ] } ]
위도, 경도가 저장된 형태¶
In [35]:
lat = geocode[0].get('geometry')['location']['lat']
lng = geocode[0].get('geometry')['location']['lng']
lat, lng
Out[35]:
(41.9105258, -87.6343775)
Folium 사용해서 지도에 시각화 하기¶
In [36]:
import folium
import pandas as pd
import numpy as np
from tqdm import tqdm
In [38]:
# 표시할 주소 데이터 불러오기
df = pd.read_csv('../data/chicago_sandwich2.csv', index_col=0)
df.tail()
Out[38]:
Cafe | Menu | Price | Address | |
---|---|---|---|---|
Rank | ||||
46 | Chickpea | Kufta | $8. | 2018 W. Chicago Ave |
47 | The Goddess and Grocer | Debbie’s Egg Salad | $6.50 | . 25 E. Delaware Pl |
48 | Zenwich | Beef Curry | $7.50 | . 416 N. York St |
49 | Toni Patisserie | Le Végétarien | $8.75 | . 65 E. Washington St |
50 | Phoebe’s Bakery | The Gatsby | $6.85 | . 3351 N. Broadwa |
In [52]:
for idx, row in df.iterrows():
print(str(idx) + '.\t' + row['Address'])
1. 2109 W. Chicago Ave 2. 800 W. Randolph St 3. . 445 N. Clark St 4. . 914 Noyes St 5. 825 W. Fulton Mkt 6. . 100 E. Walton St 7. 1639 S. Wabash Ave 8. 2211 W. North Ave 9. 3619 W. North Ave 10. 3267 S. Halsted St 11. 2537 N. Kedzie Blvd 12. . Multiple location 13. 3124 N. Broadwa 14. 3455 N. Southport Ave 15. 2657 N. Kedzie Ave 16. . 1120 W. Grand Ave 17. . 1141 S. Jefferson St 18. . 333 E. Benton Pl 19. . 1411 N. Wells St 20. 1747 N. Damen Ave 21. 3209 W. Irving Park Rd 22. Multiple location 23. 5347 N. Clark St 24. . 2954 W. Irving Park Rd 25. . Multiple location 26. . 191 Skokie Valley Rd 27. . Multiple location 28. . 1818 W. Wilson Ave 29. 2517 W. Division St 30. 218 W. Kinzie St 31. . Multiple location 32. . 1547 N. Wells St 33. 415 N. Milwaukee Ave 34. . 1840 N. Damen Ave 35. 1220 W. Webster Ave 36. 5357 N. Ashland Ave 37. 1834 W. Montrose Ave 38. 615 N. State St 39. Multiple location 40. 241 N. York Rd 41. . 1323 E. 57th St 42. 655 Forest Ave 43. Hotel Lincol 44. 100 S. Marion St 45. . 26 E. Congress Pkwy 46. 2018 W. Chicago Ave 47. . 25 E. Delaware Pl 48. . 416 N. York St 49. . 65 E. Washington St 50. . 3351 N. Broadwa
In [53]:
# 다음과 같은 경우 googlmaps로 찾을 수 없음
# 예외 처리 필요
df.loc[25, 'Address']
Out[53]:
'. Multiple location'
In [55]:
geocode = gmaps.geocode(address='. Multiple location')
geocode
Out[55]:
[]
Geocoding AIP를 통해 위경도 값 찾기¶
In [60]:
lat_list = []
lng_list = []
gmaps_key = 'Your API Key'
gmaps = googlemaps.Client(key=gmaps_key)
wrong_add = '. Multiple location'
for idx, row in tqdm(df.iterrows(), total=len(df.index)):
if row['Address'] == wrong_add:
lat = np.nan
lng = np.nan
else:
geocode = gmaps.geocode(row['Address'] + ', Chicago')
lat = geocode[0].get('geometry')['location']['lat']
lng = geocode[0].get('geometry')['location']['lng']
lat_list.append(lat)
lng_list.append(lng)
100%|██████████| 50/50 [00:05<00:00, 9.04it/s]
Geocoding AIP로 찾은 위경도 값을 데이터 프레임에 추가¶
In [61]:
df['lat'] = lat_list
df['lng'] = lng_list
Folium을 사용하여 시각화¶
In [62]:
mapping = folium.Map(location=[lat_list[0], lng_list[0]], zoom_start=11)
In [63]:
for idx, row in df.iterrows():
if np.isnan(row['lat']) or np.isnan(row['lng']):
continue
else:
folium.Marker(location=[row['lat'], row['lng']], popup=row['Cafe']).add_to(mapping)
mapping
Out[63]:
Make this Notebook Trusted to load map: File -> Trust Notebook
'Data Analysis > Visualization' 카테고리의 다른 글
[Python] [Data Visualization] Folium을 사용한 지도 시각화 (2) (0) | 2023.08.11 |
---|---|
[Python] [Data Analysis] Naver 검색 API 사용한 쇼핑 데이터 분석 (0) | 2023.08.11 |
[Python] [Data Visualization] 지도 시각화 도구 Folium (0) | 2023.08.10 |
[Python] [Data Visualization] seaborn을 활용한 데이터 시각화 기초 (1) | 2023.08.08 |
[Python] [Data Visualization] matplotlib Documentation Examples (0) | 2023.08.08 |