高德地图获取天津道路交通态势

道路状态

获取矩形区域内道路交通状态

详细说明请阅读官方说明文档交通态势

范围经纬度

矩形范围需要用高德坐标描述,通过JS API获取鼠标点击经纬度功能得到左下及右上坐标。
注意:矩形对角线不能超过10公里

请求说明

请求如下:
https://restapi.amap.com/v3/traffic/status/rectangle?rectangle=117.670834,38.919082;117.733319,38.988499&extensions=all&levels=1&key=xxxxxxx

  • 其中rectangle为矩形区域范围,对角坐标用;隔开
  • levels指定道路等级,下面各值代表的含义:
    1:高速(京藏高速)
    2:城市快速路、国道(西三环、103国道)
    3:高速辅路(G6辅路)
    4:主要道路(长安街、三环辅路路)
    5:一般道路(彩和坊路)
    6:无名道路
  • key需要自己注册申请
    说明:交通态势接口计划将于2020年12月31日下线,目前不再对新用户开放。如需合作请通过工单进行商务咨询。
  • 返回结果为json格式

结果存储

环境:RRstudio
Library:curl

  1. 受矩形范围限定,需要将目标区域分割成多块矩形,分别进行获取。创建tjarea.csv文件保存每块区域的请求地址。每个地址一行,无需表头。

  2. 建立tj_traffic.r脚本,每10分钟一次请求,保存json文件到本地

    library(curl)  
    setwd("E:/")  
    Url = read.csv("E:/TRAFFIC/tjarea.csv",header = FALSE)  
    mytime = Sys.time()  
    mydate = format(mytime,"%Y%m%d")  
    dir.create(mydate)  
    nowhour = substr(as.character(Sys.time()),12,13)  
    nowmin = substr(as.character(Sys.time()),15,16)  
    min =   
      switch (as.numeric(nowmin)%/%10+1,  
      "00",         
      "10",  
      "20",  
      "30",  
      "40",  
      "50"  
    )  
      dir.create(paste(mydate,"/",nowhour,min,sep = "", collapse = NULL))  
      tempdir = paste(mydate,"/",nowhour,min,sep = "", collapse = NULL)  
    for (i in 1:315) {  
      url = as.character(Url[i,]);  
      dir = paste("E:/TRAFFIC/TJTRAFFIC/",tempdir,"/tj_", as.character(i),".json",sep = "", collapse = NULL);  
      curl_download(url, dir)  
    }
  3. 建立bat文件,用于设置Windows定时任务。

    Rscript -e "source(\"E:/TJTRAFFIC/tj_traffic.R/",encoding = 'UTF-8')"

Json解析

Library:rjson,xlsx
解析json文件中的roadinfo,其中包含多条道路数据,用rbind拼接并添加timestamp,添加路名与描述组成的唯一字段,每小时输出一个文件xlsx

Sys.setenv(JAVA_HOME="C:/Program Files/Java/jre1.8.0\_111")   
library(rjson)  
library(dplyr)  
library(xlsx)  
# 设置工作目录  
setwd("E:/TJTRAFFIC/") 
# 读取模板
roadtraffic = readRDS("roadtraffic.rds")  

#日期循环
for (date in 20161122:20161123) {  
  for (hour in 0:23) {  
    for (k in 0:5) {  
      for (i in 1:195) {  
          tryCatch({  
            result = fromJSON(file = paste(  
              date,"/",formatC(hour,width = 2,flag = '0'),k,"0/tj\_",i,".json",sep = "", collapse = NULL  
            ))  
            if (result$status == 1) {  
              roads = result$trafficinfo$roads  
              temp = do.call(rbind,roads)  
              temp = as.data.frame(temp)  
              strtime = paste(  
                date,formatC(hour,width = 2,flag = '0'),k,"000",sep = "", collapse = NULL  
              )  
              temp = mutate(temp,timestamp = strtime)  
              roadtraffic = rbind.data.frame(roadtraffic,temp)  
            }  
          }  
            ,error=function(e) NULL   
          )  
      }  
    }  
    roadtraffic = mutate(roadtraffic, fullname = paste(name,direction,sep = "", collapse = NULL))  
    write.xlsx2(roadtraffic,paste("excel/",date,formatC(hour,width = 2,flag = '0'),".xlsx",sep = "", collapse = NULL), row.name = FALSE)  
    roadtraffic = readRDS("roadtraffic.rds")  
  }  
}