Created
June 22, 2022 08:55
-
-
Save tpu01yzx/4d1969c683b1e5891fc033faab1cbb11 to your computer and use it in GitHub Desktop.
根据baidu地图的SDK获取两两之间出租车的价格等信息.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time as tm | |
import pandas as pd | |
import numpy as np | |
import requests | |
import json | |
from queue import Queue | |
# 以下部分需要填写 | |
file_path = "" # 数据文件绝对路径,csv格式 | |
rows = 0 #起始行数,默认为0 | |
city = "广州市" | |
ak="your ak from baidu map OpenAPI Console." # API Key池, 以" ", " "的形式加入可用的AK | |
loc_list = pd.read_csv("locations.txt", header=None) #读取数据 | |
nrows = loc_list.shape[0] #获得行数 | |
#nrows = 3 | |
qKey=Queue() | |
XY = np.zeros((nrows, 2), dtype="float") | |
def getXY(place): | |
url = "http://api.map.baidu.com/geocoder?output=json&city=" + city | |
real_url = url + "&address=" + place + "&ak=" + ak | |
req = requests.get(real_url,timeout=1) | |
t = req.text | |
req.close() | |
data = json.loads(t) #将数据保存在数组data中 | |
# print(data) | |
flag = True | |
try: | |
if data['status'] == 'OK' and data['result']['confidence'] > 0.5: | |
return (data['result']['location']['lng'], data['result']['location']['lat']) | |
else: | |
return (0,0) | |
except: | |
return (-1,-1) | |
locations = [] | |
print('Total: ' + str(nrows) + " row(s).") | |
for i in range(nrows): | |
locations.append(loc_list.values[i][0]) | |
print(loc_list.values[i][0]) | |
XY[i] = getXY(loc_list.values[i][0]) | |
print(XY[i]) | |
def stod(slng, slat, dlng, dlat):#输入:起点纬度、起点经度、终点纬度、终点经度 | |
url ="http://api.map.baidu.com/directionlite/v1/transit?" | |
real_url = url +"origin="+str(slat)+","+str(slng)+"&destination="+str(dlat)+","+str(dlng)+"&ak="+ak | |
#print(real_url) | |
req = requests.get(real_url) | |
t = req.text | |
#print(t) | |
data = json.loads(t) #将数据保存在数组data中 | |
try: | |
if data['status'] == 0 and data['message'] == 'ok': | |
taxi = data['result']['taxi'] | |
distance = taxi['distance'] / 1000 | |
duration = taxi['duration'] / 60 | |
taxi = taxi['detail'] | |
item_cnt = len(taxi) | |
#print(item_cnt) | |
if item_cnt > 0: | |
res = np.zeros(item_cnt, dtype='float') | |
for i in range(item_cnt): | |
taxi_item = taxi[i] | |
res[i] = taxi_item['total_price'] | |
return res, distance, duration | |
else: | |
return [], distance, duration | |
else: | |
return [[0]], 0, 0 | |
except: | |
return [[-1]], -1, -1 | |
def fix(x, frac_len): | |
return str(round(x, frac_len)) | |
def fix2(x): | |
return fix(x, 2) | |
def fix6(x): | |
return fix(x, 6) | |
ctx = getcontext() | |
ctx.prec = 2 | |
prices = [] | |
distances = [] | |
durations = [] | |
for i in range(nrows): | |
prices_row = [] | |
distances_row = [] | |
durations_row = [] | |
for j in range(nrows): | |
if i == j: | |
res_str = '-' | |
distance = 0 | |
duration = 0 | |
else: | |
res, distance, duration = stod(XY[i][0], XY[i][1], XY[j][0], XY[j][1]) | |
res_str = (', '.join(map(str, res))) | |
prices_row.append(res_str) | |
distances_row.append(fix2(distance)) | |
durations_row.append(fix2(duration)) | |
prices.append(prices_row) | |
distances.append(distances_row) | |
durations.append(durations_row) | |
print(prices) | |
print(distances) | |
print(durations) | |
import xlsxwriter | |
workbook = xlsxwriter.Workbook('taxi_data.xlsx') | |
#print(prices) | |
#print(distances) | |
#print(durations) | |
worksheet = workbook.add_worksheet('prices') | |
worksheet.write_row(0, 1, locations) | |
worksheet.write_column(1, 0, locations) | |
for row_num, row_data in enumerate(prices): | |
for col_num, col_data in enumerate(row_data): | |
worksheet.write(row_num + 1, col_num + 1, col_data) | |
worksheet = workbook.add_worksheet('distances') | |
worksheet.write_row(0, 1, locations) | |
worksheet.write_column(1, 0, locations) | |
for row_num, row_data in enumerate(distances): | |
for col_num, col_data in enumerate(row_data): | |
worksheet.write(row_num + 1, col_num + 1, col_data) | |
worksheet = workbook.add_worksheet('durations') | |
worksheet.write_row(0, 1, locations) | |
worksheet.write_column(1, 0, locations) | |
for row_num, row_data in enumerate(durations): | |
for col_num, col_data in enumerate(row_data): | |
worksheet.write(row_num + 1, col_num + 1, col_data) | |
workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment