高德地图API的武汉某区域的步行路径规划
利用python的requests函数,基于高德地图 网页服务API
使用地理编码化、步行路径规划等开发方法
以武汉武昌、青山交界处以区域为例
做已有出行点至拟设共享单车站点的 步行路径规划
#coding=utf-8
import requests
import pandas as pd
from time import sleep
pd.set_option('display.max_columns', 20) # 显示省略号内容
key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #高德地图API
def geo(address: str,city = None)->dict:
#请求参数
parameters = {'key':key,
'city':city,
'citylimit':True,
'address':address
}
r = requests.get('https://restapi.amap.com/v3/geocode/geo?parameters',params = parameters)
data = r.json()['geocodes'][0]['location']
return data
#查询关键字(必填) 查询城市(可选) 仅范围指定城市数据(可选)
#逆地理编码调用
def regeo(location: str,radius = str,batch = str)->dict:
parameters = {'key':key,
'location':location,
'radius':radius,
'batch':batch
}
r = requests.get('https://restapi.amap.com/v3/geocode/regeo?parameters',params = parameters)
data = r.json()['regeocode']['formatted_address']
return data
#路径规划
def walking(origin: str,destination: str)->dict:
parameters = {'key':key,
'origin':origin,
'destination':destination,
'output':'json'
}
r = requests.get('https://restapi.amap.com/v3/direction/walking?parameters',params = parameters)
data = r.json()['route']['paths'][0]['steps']
data_1 = r.json()['route']['paths'][0]['distance']
# for i in range(0,len(data)):
# print(data[int(i)]['instruction'])
return data_1
go_data = pd.read_csv('./站点步行路径_1.csv', encoding='gbk')
st_data = pd.read_csv('./停车点坐标.csv', encoding='gbk')
# print(st_data.head())
name_go = go_data['出行点名'].values.tolist()
name_st = st_data['停车点名'].values.tolist()
# bm_go = go_data['Dili_BM'].values.tolist()
# bm_st = st_data['Dili_BM'].values.tolist()
walk_dis_list = []
for i in range(len(name_go)):
go_dis_list = []
for j in range(len(name_st)):
geo_bianma_go = geo(name_go[i], '武汉')
geo_bianma_st = geo(name_st[j], '武汉')
walk_lujing = walking(geo_bianma_go,geo_bianma_st)
# print(walk_lujing)
go_dis_list.append(walk_lujing)
print(i,'-',len(go_dis_list))
walk_dis_list.append(go_dis_list)
sleep(2)
walk_table = pd.DataFrame(walk_dis_list)
print(walk_table)
path=r'.\walk_dis_1.csv'
walk_table.to_csv(path, encoding='gbk')