路径规划API是一套以HTTP形式提供的步行、公交、驾车查询及行驶距离计算接口,返回JSON 或 XML格式的查询数据,用于实现路径规划功能的开发。获取可利用的路径规划信息有助于辅助交通研究者的深入探索。本节将对静态的自行车信息规划有效的路径,为进一步地深入研究做准备。
import pandas as pd
import requests
import time
import shapefile
import math
#坐标转换,高德转WGS84
x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.1415926535897932384626 # π
a = 6378245.0 # 长半轴
ee = 0.00669342162296594323 # 偏心率平方def gcj02_to_wgs84(lng, lat):"""GCJ02(火星坐标系)转GPS84:param lng:火星坐标系的经度:param lat:火星坐标系纬度:return:"""if out_of_china(lng, lat):return [lng, lat]dlat = _transformlat(lng - 105.0, lat - 35.0)dlng = _transformlng(lng - 105.0, lat - 35.0)radlat = lat / 180.0 * pimagic = math.sin(radlat)magic = 1 - ee * magic * magicsqrtmagic = math.sqrt(magic)dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)mglat = lat + dlatmglng = lng + dlngreturn [lng * 2 - mglng, lat * 2 - mglat]def _transformlat(lng, lat):ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng))ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *math.sin(2.0 * lng * pi)) * 2.0 / 3.0ret += (20.0 * math.sin(lat * pi) + 40.0 *math.sin(lat / 3.0 * pi)) * 2.0 / 3.0ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 *math.sin(lat * pi / 30.0)) * 2.0 / 3.0return retdef _transformlng(lng, lat):ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng))ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *math.sin(2.0 * lng * pi)) * 2.0 / 3.0ret += (20.0 * math.sin(lng * pi) + 40.0 *math.sin(lng / 3.0 * pi)) * 2.0 / 3.0ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 *math.sin(lng / 30.0 * pi)) * 2.0 / 3.0return ret
#读取数据
def read_csv(path):file = pd.read_excel(path,index_col=[0],encoding="utf_8_sig",date_parser=[1,2,3])file = pd.DataFrame(file)print(file)return file#提取输入数据
def extract_data(file):#提取输入数据,赋值每个变量origin_lng = file["ori_lng"]origin_lat = file["ori_lat"]destination_lng = file["des_lng"]destination_lat = file["des_lat"]return origin_lng,origin_lat,destination_lng,destination_lat#发送请求解析数据
def mapping(origin_lng,origin_lat,destination_lng,destination_lat,my_key):l = []key = my_keyn = 0for i in range (len(origin_lat)):n += 1print("已经成功匹配{}条数据".format(n))#构造urlurl ="https://restapi.amap.com/v4/direction/bicycling?origin={},{}&destination={},{}&key={}"u = url.format(origin_lng.values[i],origin_lat.values[i],destination_lng.values[i],destination_lat.values[i],key)#发送请求time.sleep(0.2)r = requests.get(u).json()try:#解析返回返回数据#距离origin = r["data"].get("origin")o_lnglat = origin.split(",")destination = r["data"].get("destination")d_lnglat = destination.split(",")distance = r["data"]["paths"][0].get("distance")#耗时duration = r["data"]["paths"][0]["duration"]step = r["data"]["paths"][0]["steps"]for j in range (len(step)):#第i条记录,第j个路段的起始坐标print(step[j])p = step[j]["polyline"]p1 = p.split(";")for k in range(len(p1)):p2 = p1[k].split(",")#添加数据到列表l.append([i,distance,duration,float(p2[0]),float(p2[1]),float(o_lnglat[0]),float(o_lnglat[1]),float(d_lnglat[0]),float(d_lnglat[1])])except:with open(r"./error.txt","a") as f:f.write("{},{},{},{}错误n".format(origin_lng.values[i],origin_lat.values[i],destination_lng.values[i],destination_lat.values[i]))continueresult = pd.DataFrame(l,columns=["record","distance","time","lng","lat","origin_lng","origin_lat","destination_lng","destination_lat"])# 转化坐标result["lng84"] = 0.00result["lat84"] = 0.00for i in range(len(result)):result["lng84"][i] = gcj02_to_wgs84(float(result["lng"][i]), float(result["lat"][i]))[0]result["lat84"][i] = gcj02_to_wgs84(float(result["lng"][i]), float(result["lat"][i]))[1]return result
#储存数据
def save_data(result):result.to_csv(r"./bike_trip.csv",encoding="utf-8_sig")print("已完成")def main():path = r"./OD_bike.xlsx"key = '你的key值'file = read_csv(path)o_lng,o_lat,d_lng,d_lat = extract_data(file)print(o_lat.values)result = mapping(o_lng,o_lat,d_lng,d_lat,key)save_data(result)if __name__ == "__main__":main()