日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

基于百度地图API的交通可达性分析python

發(fā)布時(shí)間:2024/3/24 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于百度地图API的交通可达性分析python 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 一、交通可達(dá)性是什么?
  • 二、計(jì)算步驟
    • 1.引入庫
    • 2.調(diào)用百度API進(jìn)行兩點(diǎn)之間的路徑查詢
    • 3.輸入待計(jì)算的文件和保存結(jié)果文件路徑
    • 4.讀取文件并進(jìn)行時(shí)間和距離計(jì)算
    • 5.代碼總覽
  • 總結(jié)


一、交通可達(dá)性是什么?

交通可達(dá)性最重要的考慮因素是交通成本,即交通距離與交通時(shí)間
可以使用API 的路徑規(guī)劃服務(wù)功能,選擇公交路線,獲取兩個(gè)位置之間的交通時(shí)間和交通距離,其使用規(guī)則是通過http/https 形式發(fā)起檢索請(qǐng)求,將兩個(gè)位置的坐標(biāo)傳遞給百度地圖服務(wù)器,服務(wù)器通過計(jì)算后將路徑規(guī)劃結(jié)果返回。從返回的參數(shù)中選擇distance 和duration 分別表示總交通網(wǎng)絡(luò)距離和總交通出行時(shí)間。

案例說明:
比如我們要計(jì)算上海迪士尼樂園的公交可達(dá)性,可以先將上海劃分為500m*500m的網(wǎng)格,然后將網(wǎng)格的經(jīng)緯度作為起點(diǎn),將迪士尼樂園的經(jīng)緯度作為終點(diǎn),通過百度地圖API計(jì)算起終點(diǎn)的時(shí)間和距離,再借用GIS分析工具,將結(jié)果可視化在地圖上,即可生成如下可達(dá)性地圖。

該篇文章主要介紹如何借用百度API計(jì)算兩點(diǎn)之間的真實(shí)出行時(shí)間和距離。

二、計(jì)算步驟

1.引入庫

代碼如下(示例):

import requests import json import time

2.調(diào)用百度API進(jìn)行兩點(diǎn)之間的路徑查詢

若查詢數(shù)據(jù)量較大,服務(wù)器有時(shí)會(huì)掉線,因此做了等待后重新嘗試連接的功能。
代碼如下(示例):

def getjson(ocoo,dcoo):# 先緯度后經(jīng)度url='http://api.map.baidu.com/direction/v2/driving?origin='+ocoo+'&destination='+dcoo+'&coord_type=wgs84&departure_time=1595548800&tactics_incity=4&ak=XXX'while True:try:response=requests.get(url=url,timeout=5)breakexcept requests.exceptions.ConnectionError:print ('ConnectionError -- please wait 3 sec')time.sleep(3)except requests.exceptions.ChunkedEncodingError:print ('ChunkedEncodingError -- please wait 3 sec')time.sleep(3)except:print ('Unknow error')time.sleep(3)html=response.textdecodejson=json.loads(html)return decodejson

3.輸入待計(jì)算的文件和保存結(jié)果文件路徑

將待查詢的兩點(diǎn)的位置屬性保存到文本文件,文件格式為

記錄編號(hào)起點(diǎn)經(jīng)度起點(diǎn)緯度終點(diǎn)經(jīng)度終點(diǎn)緯度
1113.837522.8075113.827522.8175
2113.837522.5655113.887522.4626
3113.837522.1658113.873222.1235
# 輸入查詢文件的路徑 file_object=open(r'D:\input\fromsz_base202011.csv','r') # 輸出結(jié)果文件的保存路徑 file_object2=open(r'D:\fromsz_base_dis202011.txt','w')

4.讀取文件并進(jìn)行時(shí)間和距離計(jì)算

try:for line in file_object:count=count+1spline=line.split(',')idn=spline[0]coor=spline[5].strip()+','+spline[4].strip()door=spline[7].strip()+','+spline[6].strip()#print coordecodejson=getjson(coor,door)if decodejson.get('status')==0:#表示運(yùn)行成功result=decodejson.get('result')routes=result.get('routes')#獲得需要的時(shí)間和距離if len(routes)>0: time2=routes[0].get('duration')distance=routes[0].get('distance')file_object2.write(str(idn)+','+str(time2)+','+str(distance) +'\n')if count%10==0:finishtime=time.asctime( time.localtime(time.time()))finishtime1=time.time()print (count)print ('duration:',(finishtime1-starttime1)/60.0,'mins')else:print (str(coor)+','+ str(decodejson.get('status'))+decodejson.get('message'))

5.代碼總覽

# -*- coding: utf-8 -*- # @Author: Xie # @Date: 2021-04-15 11:49:25 # @Last Modified by: Xie # @Last Modified time: 2021-04-15 11:58:10import requests import json import time starttime=time.asctime(time.localtime(time.time())) starttime1=time.time(); # 調(diào)用百度API進(jìn)行兩點(diǎn)之間的路徑查詢 def getjson(ocoo,dcoo):# 先緯度后經(jīng)度url='http://api.map.baidu.com/direction/v2/driving?origin='+ocoo+'&destination='+dcoo+'&coord_type=wgs84&departure_time=1595548800&tactics_incity=4&ak=XXX'while True:try:response=requests.get(url=url,timeout=5)breakexcept requests.exceptions.ConnectionError:print ('ConnectionError -- please wait 3 sec')time.sleep(3)except requests.exceptions.ChunkedEncodingError:print ('ChunkedEncodingError -- please wait 3 sec')time.sleep(3)except:print ('Unknow error')time.sleep(3)html=response.textdecodejson=json.loads(html)return decodejson # 輸入查詢文件的路徑 file_object=open(r'D:\input\fromsz_base202011.csv','r') # 輸入結(jié)果文件的保存路徑 file_object2=open(r'D:\fromsz_base_dis202011.txt','w') count=0 try:for line in file_object:count=count+1spline=line.split(',')idn=spline[0]coor=spline[5].strip()+','+spline[4].strip()door=spline[7].strip()+','+spline[6].strip()#print coordecodejson=getjson(coor,door)if decodejson.get('status')==0:#表示運(yùn)行成功result=decodejson.get('result')routes=result.get('routes')#獲得需要的時(shí)間和距離if len(routes)>0: time2=routes[0].get('duration')distance=routes[0].get('distance')file_object2.write(str(idn)+','+str(time2)+','+str(distance) +'\n')if count%10==0:finishtime=time.asctime( time.localtime(time.time()))finishtime1=time.time()print (count)print ('duration:',(finishtime1-starttime1)/60.0,'mins')else:print (str(coor)+','+ str(decodejson.get('status'))+decodejson.get('message')) finally:file_object.close()file_object2.close()print ('finish')

總結(jié)

以上就是利用地圖API進(jìn)行可達(dá)性計(jì)算的方法,操作簡單,用戶友好,結(jié)果準(zhǔn)確。
而傳統(tǒng)的GIS可達(dá)性計(jì)算,需要構(gòu)建完善的GIS 交通網(wǎng)絡(luò)模型,工作量較大。

注:API個(gè)人key有查詢額度限制,企業(yè)key額度較高,若有大量查詢需求,可私信提供企業(yè)key。
若有可達(dá)性分析需求,也可私信幫忙!

參考文獻(xiàn):
[1]《大型公共服務(wù)設(shè)施公共交通可達(dá)性評(píng)價(jià)方法》
[2]百度API文檔:http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2

總結(jié)

以上是生活随笔為你收集整理的基于百度地图API的交通可达性分析python的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。