# coding=utf-8 import csv import numpy as np import folium from folium import plugins import csv import requests import json import pandas as pd import re import time import webbrowser def PlotLineOnMap(): Lat = [] Lon = [] Category = [] Date_time = [] print(type(Lat)) fn = 'result.csv' with open(fn) as csvFile: csvDictReader = csv.DictReader(csvFile) for row in csvDictReader: print(row['lon'], row['lat']) Lat.insert(0,float(row['lat'])) Lon.insert(0,float(row['lon'])) Category.insert(0,str(row['category'])) Date_time.insert(0,str(row['date_time'])) print(Lat) # 给出的坐标系为GCJ-02,如果需要测试google地图,需要进行坐标转换 tri = np.array(list(zip(Lat, Lon,Category,Date_time))) san_map = folium.Map( location=[36.641254, 117.068689], zoom_start=16, # 高德街道图 tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}', # tiles='http://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}', # 高德卫星图 attr='default') layer_0329 = folium.FeatureGroup(name="0329") layer_0330 = folium.FeatureGroup(name="0330") layer_0331 = folium.FeatureGroup(name="0331") layer_0401 = folium.FeatureGroup(name="0401") layer_0402 = folium.FeatureGroup(name="0402") layer_0403 = folium.FeatureGroup(name="0403") layer_0404 = folium.FeatureGroup(name="0404") layer_0405 = folium.FeatureGroup(name="0405") layer_0406 = folium.FeatureGroup(name="0406") layer_0407 = folium.FeatureGroup(name="0407") layer_0408 = folium.FeatureGroup(name="0408") layer_0409 = folium.FeatureGroup(name="0409") layer_0410 = folium.FeatureGroup(name="0410") layer_0411 = folium.FeatureGroup(name="0411") layer_0412 = folium.FeatureGroup(name="0412") layer_0413 = folium.FeatureGroup(name="0413") layer_0414 = folium.FeatureGroup(name="0414") layer_0415 = folium.FeatureGroup(name="0415") layer_0416 = folium.FeatureGroup(name="0416") layer_0417 = folium.FeatureGroup(name="0417") layer_0418 = folium.FeatureGroup(name="0418") layer_0419 = folium.FeatureGroup(name="0419") layer_0420 = folium.FeatureGroup(name="0420") layer_0421 = folium.FeatureGroup(name="0421") layer_0422 = folium.FeatureGroup(name="0422") layer_0423 = folium.FeatureGroup(name="0423") layer_0424 = folium.FeatureGroup(name="0424") layer_0425 = folium.FeatureGroup(name="0425") layer_0426 = folium.FeatureGroup(name="0426") layer_0427 = folium.FeatureGroup(name="0427") layer_0428 = folium.FeatureGroup(name="0428") layer_0429 = folium.FeatureGroup(name="0429") layer_0430 = folium.FeatureGroup(name="0430") layer_0501 = folium.FeatureGroup(name="0501") layer_0502 = folium.FeatureGroup(name="0502") layer_0503 = folium.FeatureGroup(name="0503") layer_0504 = folium.FeatureGroup(name="0504") layer_0505 = folium.FeatureGroup(name="0505") layer_0506 = folium.FeatureGroup(name="0506") layer_0507 = folium.FeatureGroup(name="0507") #folium.PolyLine(tri, color='#3388ff').add_to(san_map) # marker_cluster = plugins.MarkerCluster().add_to(san_map) for lat, lon,category,date_time in zip(Lat, Lon,Category,Date_time): print([category]) print(type(category)) print(category) print([date_time]) if( date_time == '0329' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0329) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0329) if( date_time == '0330' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0330) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0330) if (date_time == '0331'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0331) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0331) if( date_time == '0401' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0401) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0401) if( date_time == '0402' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0402) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0402) if( date_time == '0403' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0403) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0403) if (date_time == '0404'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0404) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0404) if( date_time == '0405' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0405) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0405) if( date_time == '0406' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0406) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0406) if( date_time == '0407' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0407) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0407) if( date_time == '0408' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0408) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0408) if( date_time == '0409' ): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0409) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0409) if (date_time == '0410'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0410) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0410) if (date_time == '0411'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0411) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0411) if (date_time == '0412'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0412) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0412) if (date_time == '0413'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0413) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0413) if (date_time == '0414'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0414) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0414) if (date_time == '0415'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0415) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0415) if (date_time == '0416'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0416) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0416) if (date_time == '0417'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0417) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0417) if (date_time == '0418'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0418) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0418) if (date_time == '0419'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0419) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0419) if (date_time == '0420'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0420) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0420) if (date_time == '0421'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0421) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0421) if (date_time == '0422'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0422) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0422) if (date_time == '0423'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0423) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0423) if (date_time == '0424'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0424) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0424) if (date_time == '0425'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0425) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0425) if (date_time == '0426'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0426) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0426) if (date_time == '0427'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0427) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0427) if (date_time == '0428'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0428) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0428) if (date_time == '0429'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0429) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0429) if (date_time == '0430'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0430) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0430) if (date_time == '0501'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0501) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0501) if (date_time == '0502'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0502) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0502) if (date_time == '0503'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0503) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0503) if (date_time == '0504'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0504) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0504) if (date_time == '0505'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0505) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0505) if (date_time == '0506'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0506) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0506) if (date_time == '0507'): if (category == '确诊者住址'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color="red")).add_to(layer_0507) elif (category == '确诊者轨迹'): folium.Marker(location=[lat, lon], popup='Mt. Hood Meadows', icon=folium.Icon(icon='cloud', color='orange')).add_to(layer_0507) san_map.add_child(layer_0329) san_map.add_child(layer_0330) san_map.add_child(layer_0331) san_map.add_child(layer_0401) san_map.add_child(layer_0402) san_map.add_child(layer_0403) san_map.add_child(layer_0404) san_map.add_child(layer_0405) san_map.add_child(layer_0406) san_map.add_child(layer_0407) san_map.add_child(layer_0408) san_map.add_child(layer_0409) san_map.add_child(layer_0410) san_map.add_child(layer_0411) san_map.add_child(layer_0412) san_map.add_child(layer_0413) san_map.add_child(layer_0414) san_map.add_child(layer_0415) san_map.add_child(layer_0416) san_map.add_child(layer_0417) san_map.add_child(layer_0418) san_map.add_child(layer_0419) san_map.add_child(layer_0420) san_map.add_child(layer_0421) san_map.add_child(layer_0422) san_map.add_child(layer_0423) san_map.add_child(layer_0424) san_map.add_child(layer_0425) san_map.add_child(layer_0426) san_map.add_child(layer_0427) san_map.add_child(layer_0428) san_map.add_child(layer_0429) san_map.add_child(layer_0430) san_map.add_child(layer_0501) san_map.add_child(layer_0502) san_map.add_child(layer_0503) san_map.add_child(layer_0504) san_map.add_child(layer_0505) san_map.add_child(layer_0506) san_map.add_child(layer_0507) san_map.add_child(folium.map.LayerControl()) san_map.add_child(folium.LatLngPopup()) san_map.save('test.html') def main(): num = 0 y = [] ''' 在url里key后面的******换为高德开放平台自己申请的key ''' with open("地址.csv", 'r') as f: # 写入将要转换的地址的文件路径,此处为默认文件路径(要先将文件提前导入)(注意是csv格式文件) r = csv.reader(f, delimiter=',') for row in r: print(row[0]) print(row[1]) url = "http://restapi.amap.com/v3/geocode/geo?key=831894c7f8fe1d2bedb982a040536650&address=" + row[2] dat = { 'count': "1", } r = requests.post(url, data=json.dumps(dat)) s = r.json() try: b = s['geocodes'] text = str(b) print(text) except: text = 'none' # 省份 try: pat1 = "'province': '(.*?)'," province = re.compile(pat1).findall(text)[0] print("省份:", province) except: province = 'none' print("省份:", province) # 城市 try: pat2 = "'city': '(.*?)'," city = re.compile(pat2).findall(text)[0] print("城市:", city) except: city = 'none' print("城市:", city) # 区划 try: pat3 = "'district': '(.*?)'," district = re.compile(pat3).findall(text)[0] print("区划:", district) except: district = 'none' print("区划:", district) # 经纬度 try: pat4 = "'location': '(.*?)'," res3 = re.compile(pat4).findall(text)[0] # print(res3) lon_lat = res3.split(',') lon = float(lon_lat[0]) lat = float(lon_lat[1]) print("经度:", lon) print("纬度:", lat) except: lon = 'none' lat = 'none' print("经度:", lon) print("纬度:", lat) num += 1 print("第" + str(num) + "条地址转换成功") print('**************************************') date_time =row[0] y.append([num,str(row[0]),row[1],row[2], province, city, district, lon, lat]) result = pd.DataFrame(y) result.columns = ['num', 'date_time','category','address', 'province', 'city', 'district', 'lon', 'lat'] result.to_csv('result.csv', encoding='gbk', index=False) print("全部地址转换成功") time.sleep(5) #暂停10秒 PlotLineOnMap() webbrowser.open('test.html') if __name__ == '__main__': main()
wonderful,轨迹分层上图
最新推荐文章于 2024-04-08 22:48:46 发布
此代码展示了如何使用Python和Folium库在地图上标记和区分不同日期的新冠疫情确诊者住址和轨迹。通过读取CSV数据,将数据转换为 Folium FeatureGroup,以便在高德地图上显示。每个日期对应不同的颜色标识,有助于追踪疫情发展动态。
摘要由CSDN通过智能技术生成