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

阅读: 评论:0

基于百度地图API的交通可达性分析python
⽂章⽬录
⼀、交通可达性是什么?
交通可达性最重要的考虑因素是交通成本,即交通距离与交通时间
可以使⽤API 的路径规划服务功能,选择公交路线,获取两个位置之间的交通时间和交通距离,其使⽤规则是通过http/https 形式发起检索请求,将两个位置的坐标传递给百度地图服务器,服务器通过计算后将路径规划结果返回。从返回的参数中选择distance 和duration 分别表⽰总交通⽹络距离和总交通出⾏时间。
案例说明:
⽐如我们要计算上海迪⼠尼乐园的公交可达性,可以先将上海划分为500m*500m的⽹格,然后将⽹格的经纬度作为起点,将迪⼠尼乐园的经纬度作为终点,通过百度地图API计算起终点的时间和距离,再借⽤GIS分析⼯具,将结果可视化在地图上,即可⽣成如下可达性地图。
该篇⽂章主要介绍如何借⽤百度API计算两点之间的真实出⾏时间和距离。
⼆、计算步骤
1.引⼊库
代码如下(⽰例):
import requests
import json
import time
2.调⽤百度API进⾏两点之间的路径查询
若查询数据量较⼤,服务器有时会掉线,因此做了等待后重新尝试连接的功能。代码如下(⽰例):
def getjson(ocoo,dcoo):
# 先纬度后经度
url='api.map.baidu/direction/v2/driving?origin='+ocoo+'&destination='+dcoo+'&coord_type
=wgs84&departure_time=1595548800&tactics_incity =4&ak=XXX'
while True:
try:
(url=url,timeout=5)
北戴河自驾游攻略
break
ptions.ConnectionError:
print('ConnectionError -- please wait 3 sec')
time.sleep(3)内江人事考试网
ptions.ChunkedEncodingError:
print('ChunkedEncodingError -- please wait 3 sec')
time.sleep(3)
except:
print('Unknow error')
time.sleep(3)
decodejson=json.loads(html)
return decodejson
3.输⼊待计算的⽂件和保存结果⽂件路径
将待查询的两点的位置属性保存到⽂本⽂件,⽂件格式为
记录编号起点经度起点纬度终点经度终点纬度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')
# 输出结果⽂件的保存路径
青岛最好的酒店file_object2=open(r'D:\fromsz_','w')
4.读取⽂件并进⾏时间和距离计算
try:
for line in file_object:
count=count+1
spline=line.split(',')
idn=spline[0]
coor=spline[5].strip()+','+spline[4].strip()
door=spline[7].strip()+','+spline[6].strip()
#print coor
decodejson=getjson(coor,door)
('status')==0:#表⽰运⾏成功
('result')
('routes')
#获得需要的时间和距离
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)+','+('status'))+('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:10
import requests
import json
宁波动物园门票多少钱一张
import time
starttime=time.asctime(time.localtime(time.time()))
starttime1=time.time();
# 调⽤百度API进⾏两点之间的路径查询
def getjson(ocoo,dcoo):
# 先纬度后经度
url='api.map.baidu/direction/v2/driving?origin='+ocoo+'&destination='+dcoo+'&coord_type=wgs84&departure_time=1595548800&tactics_incity =4&ak=XXX'
while True:
try:
(url=url,timeout=5)
break
ptions.ConnectionError:
print('ConnectionError -- please wait 3 sec')
time.sleep(3)
ptions.ChunkedEncodingError:
print('ChunkedEncodingError -- please wait 3 sec')
time.sleep(3)
except:
print('Unknow error')
time.sleep(3)
decodejson=json.loads(html)
return decodejson
# 输⼊查询⽂件的路径
file_object=open(r'D:\input\fromsz_base202011.csv','r')
# 输⼊结果⽂件的保存路径
file_object2=open(r'D:\fromsz_','w')
count=0
count=0
try:
for line in file_object:
count=count+1
spline=line.split(',')
idn=spline[0]
coor=spline[5].strip()+','+spline[4].strip()
door=spline[7].strip()+','+spline[6].strip()
#print coor
decodejson=getjson(coor,door)
('status')==0:#表⽰运⾏成功
('result')
('routes')
#获得需要的时间和距离
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()
成都36个一日游免费景点
print(count)
print('duration:',(finishtime1-starttime1)/60.0,'mins')
else:
print(str(coor)+','+('status'))+('message'))
finally:
file_object.close()
山东青岛自由行攻略file_object2.close()
print('finish')
总结
以上就是利⽤地图API进⾏可达性计算的⽅法,操作简单,⽤户友好,结果准确。
⽽传统的GIS可达性计算,需要构建完善的GIS 交通⽹络模型,⼯作量较⼤。
注:API个⼈key有查询额度限制,企业key额度较⾼,若有⼤量查询需求,可私信提供企业key。若有可达性分析需求,也可私信帮忙!

本文发布于:2023-09-05 19:53:15,感谢您对本站的认可!

本文链接:http://www.035400.com/whly/4/224672.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:交通   计算   时间   距离   路径
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2024-2030 Comsenz Inc.Powered by © 文化旅游网 滇ICP备2022007236号-403 联系QQ:1103060800网站地图