简体   繁体   English

使用纬度和经度向量在R中查找步行距离

[英]Find walking distance in R using vectors of latitude and longitude

Hi I have a series of homes and series of rail stations. 嗨,我有一系列的房屋和一系列的火车站。 I want to calculate the walking distance from each home (n=1718) to each station (n=11). 我想计算从每个家庭(n = 1718)到每个站(n = 11)的步行距离。 I know google limits you to 2000 obs per day, so I would do all home obs to 1 station. 我知道谷歌限制你每天2000张,所以我会做所有家庭的阻止1站。 My data looks like this: 我的数据如下:

Home data:                      

        longitude   latitude            
    1   -76.27769   36.86308
    2   -76.29188   36.87556
    3   -76.26982   36.86628
    4   -76.27455   36.86894

    Station Data
        Longitude   Latitude
    1   -76.30377   36.85945
    2   -76.29490   36.85395
    3   -76.28896   36.85156
    4   -76.28989   36.84719
    5   -76.28579   36.84568

I found a code like this, but it sums up the distance for every home rather than the distance for each individual home.I'm pretty new to R...help! 我找到了这样的代码,但它总结了每个家的距离,而不是每个家的距离。我是R的新手...帮助!

  `distHoras <- function(origin, destination){

  origin <- gsub(",", "", origin)
  origin <- gsub(" ", "+", origin)
  origin <- paste("origins=", origin, sep = "")

  destination <- gsub(",", "", destination)
  destination <- gsub(" ", "+", destination)
  destination <- paste("destination=", paste(destination, 
                                               collapse = "|"), sep = "")


  mode4url <- paste("mode=", 'walking', sep = "")
  lang4url <- paste("language=", 'en-EN', sep = "")
  sensor4url <- paste("sensor=", tolower(as.character(FALSE)), 
                      sep = "")
  posturl <- paste(origin, destination, mode4url, sensor4url, 
                   sep = "&")
  url_string <- paste("http://maps.googleapis.com/maps/api/distancematrix/json?", 
                      posturl, sep = "")
  url_string <- URLencode(url_string)
  connect <- url(url_string)
  tree <- fromJSON(paste(readLines(connect), collapse = ""))
  close(connect)
  rapply(tree$rows,I)
}`

I get output like this 我得到这样的输出

distHoras('origin', 'destination')
 elements.distance.text elements.distance.value 
             "1,253 km"               "1252635" 
 elements.duration.text elements.duration.value 
       "9 days 8 hours"                "804659" 
        elements.status 
                   "OK" 

Something like this?? 这样的事情?

google.dist <- function(from,to,mode="walking") {
  require(httr)
  require(XML)
  url    <- "https://maps.googleapis.com/maps/api/distancematrix/xml"
  origin <- paste(with(from,paste(latitude,longitude,sep=",")),collapse="|")
  dest   <- paste(with(to,paste(latitude,longitude,sep=",")),collapse="|")
  response <- GET(url,query=list(origins=origin,destinations=dest,mode=mode))
  doc      <- content(response,type="text/xml")
  status   <- sapply(doc["//row/element/status"],xmlValue)
  if(any(status!="OK")) warning("Error Status on some routes")
  distance <- sapply(doc["//row/element/distance/value"],xmlValue)
  data.frame(expand.grid(to=1:nrow(to),from=1:nrow(from)),distance=as.numeric(distance))
}
google.dist(Home,Station)
#    to from distance
# 1   1    1     3275
# 2   2    1     2494
# 3   3    1     2163
# 4   4    1     2548
# 5   5    1     2212
# 6   1    2     2539
# 7   2    2     2950
# 8   3    2     3288
# 9   4    2     3815
# 10  5    2     4034
# ...

This uses the Google Distance Matrix API with XML output. 这使用带有XML输出的Google Distance Matrix API。 The returned data frame has the row numbers of the to and from data frames and the distance in meters between them. 返回的数据帧具有的行号tofrom数据帧和它们之间的距离。 The API is documented here . API 在此处记录 Please note carefully the terms of use . 请仔细注意使用条款

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 使用 18k 行数据框中的 2 个纬度和 2 个经度向量计算距离(以英里为单位) - R calculate distance in miles using 2 latitude & 2 longitude vectors in a data frame for 18k rows 获取经度点和纬度点向量之间的距离 - Getting distance between vectors of longitude and latitude points 计算数据帧R中多个距离经度纬度 - Calculate distance longitude latitude of multiple in dataframe R 在R中使用Google Maps计算步行距离 - Calculating walking distance using Google Maps in R 无法使用Haversine公式正确使用R中的纬度和经度计算距离 - Unable to calculate the distance using latitude and longitude in R using Haversine formula correctly R中不等位数之间的距离(纬度和经度是输入) - Distance between unequal number of locations in R (latitude and longitude are inputs) R程序 - 驱动城市之间的纬度和经度 - R program- Drive distance latitude and longitude between cities 计算 R dataframe 中两个经纬度地理坐标之间的距离 - Calculating the distance between two latitude and longitude Geocordinate in a R dataframe 试图找到纬度和经度坐标对之间的距离 - Trying to find the distance between pairs of latitude and longitude coordinates 使用R中的纬度和经度计算观测值 - Counting Observations Using Latitude and Longitude in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM