Python通过百度API接口获取路段车辆实时速度

编程入门 行业动态 更新时间:2024-10-11 17:19:44

Python通过百度API接口获取<a href=https://www.elefans.com/category/jswz/34/882200.html style=路段车辆实时速度"/>

Python通过百度API接口获取路段车辆实时速度

首先要计算路网中路段的起始点坐标(可以通过ArcGIS的计算几何实现),然后通过对路段的起始点坐标进行百度的路径规划,获取规划路段的路程和行驶时间,来计算路段的行驶速度,还可以获取路段的路况信息。

#coding=utf8
import json  
import requests
import arcpy
import datetime#获取时间
time = datetime.datetime.now()
timestr = time.strftime("%H:%M")
print("时间:")
print (timestr)#*********输入***********
sum = 1061
shp = "E:\路网.shp"
#********************#创建字段
arcpy.MakeFeatureLayer_management(shp,'shp_lyr')
arcpy.AddField_management('shp_lyr',"time", "text")
arcpy.AddField_management('shp_lyr',"distance", "double")
arcpy.AddField_management('shp_lyr',"duration", "double")
arcpy.AddField_management('shp_lyr',"traffic", "long")
arcpy.AddField_management('shp_lyr',"speed", "double")#遍历路网
cursor = arcpy.UpdateCursor(shp)
for row in cursor:#起点84坐标x1 = row.getValue('起点x')y1 = row.getValue('起点y')#终点84坐标x2 = row.getValue('终点x')y2 = row.getValue('终点y')print("起止点坐标:")print(x1,y1,x2,y2)#起点84坐标# x1 = 117.791043# y1 = 36.922526#终点84坐标# x2 = 117.790572# y2 = 36.899719# 驾车路线规划url = '='+ str(y1) + ',' + str(x1) +'&destination='+ str(y2) + ',' + str(x2) +'&ak=key&coord_type=wgs84'response = requests.get(url)data = response.json()#获取规划路径距离distance = data['result']["routes"][0]["distance"]#获取规划路径时间duration = data['result']["routes"][0]["duration"]#获取路况评价 0: 无路况  1: 畅通  2: 缓行  3: 拥堵   4: 严重拥堵 traffic_condition = data['result']["routes"][0]["traffic_condition"]#计算路段车速speed = distance / durationprint("获取数据:")print(distance, duration, traffic_condition, speed)#写入数据row.setValue("time", timestr)row.setValue("distance", distance)row.setValue("duration", duration)row.setValue("traffic", traffic_condition)row.setValue("speed", speed)cursor.updateRow(row)

更多推荐

Python通过百度API接口获取路段车辆实时速度

本文发布于:2024-02-13 08:33:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路段   实时   接口   车辆   速度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!