Programming Language/Python
[Python] googlemaps를 사용해서 각 경찰서의 위도, 경도 정보를 얻기
draidev
2022. 3. 24. 22:59
저번 게시글 [Python] 데이터 시각화 (지도/ Folium, GeoJSON)에 이어집니다.
목차>
더보기
목차
googlemaps library 설치
- pip install googlemaps==4.6.0
- pip install --index-url=http://pypi.python.org/simple/ --trusted-host pypi.python.org googlemaps==4.6.0
- conda config --set ssl_verify false 입력 후 -> conda install googlemaps==4.6.0
01 gmaps.geocode( )
googlemaps.geocode(찾고싶은 위치명, 언어)
import googlemaps
gmaps = googlemaps.Client(key="-- input your key --")
tmpMap = gmaps.geocode('서울강남경찰서', language="ko")
location키의 lat, lng(위도, 경도) 값을 이용해서 경찰서의 위치를 찾습니다.
lat = []
lng = []
for name in df['경찰서']:
tmpMap = gmaps.geocode(name, language='ko')
tmpLoc = tmpMap[0].get('geometry')
lat.append(tmpLoc['location']['lat'])
lng.append(tmpLoc['location']['lng'])
df['lat'] = lat
df['lng'] = lng
02 foliom.CircleMarker( )
- 경찰서별로 원형 마커를 생성하여 점수를 radius 로 매겨 지도에 표시합니다.
- folium.Circle() 의 경우는 radius가 자동으로 meter 단위가 됩니다. (아래 CircleMarker에서의 radius는 pixel 단위여서 지도 해상도에 따라서 크기가 변합니다.)
map = folium.Map(location=[37.5502, 126.982], zoom_start=11)
map.choropleth(geo_data = geo_str,
data = crime_ratio['전체발생비율'],
columns = [crime_ratio.index, crime_ratio['전체발생비율']],
fill_color = 'PuRd',
key_on = 'feature.id')
# CircleMarker() : 경찰서 위치를 원모양 마커로 표시합니다.
for n in df.index:
folium.CircleMarker([df['lat'][n], df['lng'][n]],
radius=df['점수'][n]*0.7, # 0.5 -> 0.7
color='#3186cc', fill=True, fill_color='#3186cc').add_to(map)