这里路由API格式

时间:2018-11-29 16:50:41

标签: r here-api

先谢谢了。

我是R的新手,并且一直在编写R脚本以将传输数据从一个节点拉到另一个节点。我正在提取所需的数据。我可以输入travelTime。要将数据附加到数据帧中,如何获取经度和纬度信息。同样,对于for循环的任何见解,都是由于某些原因导致数据重复x倍的原因。

library(httr)
library(dplyr)
library(jsonlite)
library(RJSONIO)
library(RCurl)
od_pairs <- read.csv("od-pairs.csv", stringsAsFactors = FALSE)


# create single list of locations and their geolocs
fdc_locs <- od_pairs %>% select(fdc, fdc_lat, fdc_lon) %>%
  rename("loc"=fdc, "lat"=fdc_lat, "lon"=fdc_lon) %>% unique()
store_locs <- od_pairs %>% select(store, store_lat, store_lon) %>%
  rename("loc"=store, "lat"=store_lat, "lon"=store_lon) %>% unique()
all_locs <- rbind(fdc_locs, store_locs)
# list of FDCs
fdcs <- unique(od_pairs$fdc)
# create stores pairs list
pairs_list <- data.frame()
for (fdc in fdcs) {
  temp <- od_pairs[od_pairs$fdc == fdc,]
  
  pairs <- t(combn(temp$store, 2))
  pairs <- as.data.frame(pairs)
  
  pairs_list <- rbind(pairs_list, pairs)
}
names(pairs_list) <- c("loc_1", "loc_2")
# append fdc-store pairs to pairs_list
fdc_store_list <- od_pairs[,1:2] %>% rename("loc_1"=fdc, "loc_2"=store)
pairs_list <- rbind(pairs_list, fdc_store_list)
# create all od pairs with geocords
pairs_data <- left_join(pairs_list, all_locs, by=c("loc_1"="loc")) %>%
  rename("origin_lat"=lat, "origin_lon"=lon) %>%
  left_join(all_locs, by=c("loc_2"="loc")) %>%
  rename("dest_lat"=lat, "dest_lon"=lon)
#initialize the dataframe
test_data <- data.frame(pairs_data)

head(test_data)
#loc_1 loc_2 origin_lat origin_lon dest_lat  dest_lon
#1    12     3   38.75407  -90.42642 45.05273 -93.36556
#for (i in 1:nrow(test_data)){

#for loop
test_data[1:12,]

out_data <- data.frame()
traveltime <- c()
loop_loc_1 <-c()
loop_loc_2 <- c()
loop_origin_lat<-c()
loop_origin_lon<-c()
loop_dest_lat<-c()
loop_dest_lon<-c()
for (i in 1:12){
  # test call to here api
  
  call <- paste0("https://route.api.here.com/routing/7.2/calculateroute.json",
                 "?app_id=","CKVTMPCodnv7tuSLRPp0",
                 "&app_code=","ycmJrgNrwGCLOFc7jczscA", # change me
                 "&waypoint0=geo!",test_data$origin_lat[i],",",test_data$origin_lon[i],
                 "&waypoint1=geo!",test_data$dest_lat[i],",",test_data$dest_lon[i],
                 "&mode=fastest;truck;traffic:disabled")
  
  
  
  #Testing URL Connection
  GET(call)
  response <- fromJSON(call, simplify = FALSE )
  
  loop_loc_1 <- c(loop_loc_1, test_data$loc_1[i])
  loop_loc_2 <- c(loop_loc_2, test_data$loc_2[i])
  loop_origin_lat<-c(loop_origin_lat,test_data$origin_lat)
  loop_origin_lon<-c(loop_origin_lon,test_data$origin_lon)
  loop_dest_lat<-c(loop_dest_lat,as.numeric((response[[1]]$route[[1]]$position$latitude)))
  loop_dest_lon<-c(loop_dest_lon,test_data$dest_lon)
  
  
  
  #out_data$travel_time <- rbind(out_data$travel_time, as.numeric((response[[1]]$route[[1]]$summary$travelTime)))
  
  traveltime <- c(traveltime, as.numeric((response[[1]]$route[[1]]$summary$travelTime)))

  
  
}

a <- cbind(loop_loc_1, loop_loc_2,loop_origin_lat,loop_origin_lon,loop_dest_lat,loop_dest_lon,traveltime)

str(traveltime/60)
str(response)

1 个答案:

答案 0 :(得分:0)

您可以通过导入install.packages("rjson")来读取JSON响应。

如果可行,请检查以下内容:

routingHERE_simple <- function(search, App_id="", App_code=""){
  if(!is.character(App_id)){stop("'App_id' must be a character string")}
  if(!is.character(App_code)){stop("'App_code' must be a character string")}

if(App_id=="" & App_code==""){    
App_id <- "{Enter AppId}"
App_code <- "{Enter AppCode}"
base_url <- "https://route.ls.hereapi.com/routing/7.2/calculateroute.json"
 }else{
base_url <- "https://route.ls.hereapi.com/routing/7.2/calculateroute.json"
}

a <- httr::GET(base_url, query=list(app_id = App_id,
                                  app_code = App_code,                                      
                                  waypoint0=geo!52.5,13.4,
                                  waypoint1=geo!52.5,13.45,
                                  mode=fastest;car;traffic:disabled))
 response <- httr::content(a)

 if(length(response$Response$route) > 0){
   ret <- response$Response$route[[1]]$waypoint[[1]]$mappedPosition
  }else{
   ret <- NA
}
 return(ret)
}