# Driving Distance - road distance
library(xlsx)
library(ggmap)
transp<- read.csv2("Finaldata.csv",sep=';',header=T)
dtransp<-subset(transp,select=c(First_CDD_Dealer))
#find unique levels of dealer
unidealer<- unique(transp$First_CDD_Dealer)
unidealer2<-unique(dtransp)
dealcount<- unidealer2
#Makes a list of the unique dealer names
dealcode<-as.list(unidealer2)
#Define matrices
dealerdist<- matrix(nrow=nrow(unidealer2),ncol=nrow(unidealer2))
#Name the columns and rows in matrix
dimnames(dealerdist)<- dealcode
colnames(dealerdist)<- t(dealcount)
for (i in 40:length(unidealer)){
#will make upper part of distance matrix as using i to reduce length of j-loop
for (j in 1:length(unidealer)){
#i'th dealer position
from<- as.numeric(unique(subset(transp, First_CDD_Dealer==unidealer[i], select=(c(First_Dealer_lon,First_Dealer_lat)))))
#j'th dealer position
to<- as.numeric(unique(subset(transp, First_CDD_Dealer==unidealer[j], select=(c(First_Dealer_lon,First_Dealer_lat)))))
# Call google API to calculate distance and travel time
ddist<- mapdist(from, to, mode = c("driving"),output = c("simple"), messaging = FALSE, sensor = FALSE, language = "en-EN",override_limit = FALSE)
#Makes a distance matrix in km
dealerdist[i,j]<- ddist$km
#Mirror in diagonal to make a symmertrical distance matrix
# dealerdist[j,i]<-dealerdist[i,j]
}
}
#save dealer km distance matrix
write.csv2(as.matrix(dealerdist), file = "Z:/Project/R/Dealerdist17.csv")
b <- read.csv2("Dealerdist17.csv",sep=';',header=T)
write.table(b, file = "Z:/Project/R/dist17.txt")
1.Error:is.character(from)不为TRUE 另外:警告信息: 在revgeocode(from):反向地理编码失败 - 位置错误? location =“ - 70.8864”反向地理编码失败 - 位置不好? location =“42.1697”
它很奇怪,因为有时相同的代码从另一个系统运行同一个经销商循环?知道为什么会这样吗?我的编码有问题吗?
答案 0 :(得分:0)
我遇到了同样的问题,一切正常,直到突然出现这个错误。
反向地理编码似乎也有限制。如果您尝试运行地理编码(“someplace,somecountry”),您可能会收到此错误:“地理编码失败,状态为OVER_QUERY_LIMIT”。
简而言之,地理编码功能没有将有效值返回到mapdist函数,因为查询限制导致一切都失败。
您可以使用geocodeQueryCheck()
查看剩余的查询数量