【问题标题】:postcode distances using google使用谷歌的邮政编码距离
【发布时间】:2013-01-09 15:58:20
【问题描述】:

我有两个邮政编码列表(在 R 中)...一个孩子的地址及其学业成绩和一个学校...

我希望能够为每个孩子找到最近的学校...所以大概需要通过转换为长和纬值来计算邮政编码之间的距离?

然后我希望能够在谷歌地图上绘制每所学校的所有孩子......看看住在学校附近的孩子是否获得更好的成绩......也许为孩子们绘制不同颜色的学校,孩子们根据他们的分数有一个渐变的颜色?

也许使用 googleVis 包?

例如...

如果我们有 3 个孩子和 2 个学校的数据...

student.data <- cbind(post.codes=c("KA12 6QE", "SW1A 0AA", "WC1X 9NT"),score=c(23,58,88))
school.postcodes <- c("SL4 6DW", "SW13 9JT")

(注意,我的实际数据显然比给定的数据要大得多,因此可扩展性很有用...)

应该如何使用 googleVis 或任何其他包来完成上述操作?

【问题讨论】:

  • 您假设您拥有一所学校(按邮政编码)?
  • 是的……每所学校只有一个邮政编码……这就是 school.postcodes 对象……

标签: r google-maps google-maps-api-3 google-visualization


【解决方案1】:

我会从这样的东西开始得到纬度/经度

获取每个邮政编码的纬度/经度

library(XML)
school.postcodes <- c("KA12 6QE", "SW1A 0AA", "WC1X 9NT")
ll <- lapply(school.postcodes,
    function(str){
       u <- paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
       doc <-  xmlTreeParse(u, useInternal=TRUE)
       lat=xpathApply(doc,'/GeocodeResponse/result/geometry/location/lat',xmlValue)[[1]]
       lng=xpathApply(doc,'/GeocodeResponse/result/geometry/location/lng',xmlValue)[[1]]
       c(code = str,lat = lat, lng = lng)
})
# get long/lat for the students
ll.students <- lapply(student.data$post.codes,
             function(str){
               u <- paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
               doc <-  xmlTreeParse(u, useInternal=TRUE)
               lat=xpathApply(doc,'/GeocodeResponse/result/geometry/location/lat',xmlValue)[[1]]
               lng=xpathApply(doc,'/GeocodeResponse/result/geometry/location/lng',xmlValue)[[1]]
               c(code = str,lat = lat, lng = lng)
             })

ll <- do.call(rbind,ll)
ll.students <- do.call(rbind,ll.students)

do.call(rbind,ll)
      code         lat          lng         
[1,] "KA12%206QE" "55.6188429" "-4.6766226"
[2,] "SW1A%200AA" "51.5004864" "-0.1254664"
[3,] "WC1X%209NT" "51.5287992" "-0.1181098"

获取距离矩阵

library(RJSONIO)
dist.list <- lapply(seq(nrow(ll)),
                    function(id){
                      url <- paste("http://maps.googleapis.com/maps/api/distancematrix/json?origins=",
                                   ll[id,2],",",ll[id,3],
                                   "&destinations=",
                                   paste( ll.students[,2],ll.students[,3],sep=',',collapse='|'),
                                   "&sensor=false",sep ='')
                      res <- fromJSON(url)
                        hh <- sapply(res$rows[[1]]$elements,function(dest){
                          c(distance= as.numeric(dest$distance$value),
                                     duration = dest$duration$text)
                        })
                      hh <- rbind(hh,destination =  ll.students[,1])

                    })
names(dist.list) <- ll[,1]

dist.list
$`SL4 6DW`
            [,1]              [,2]      [,3]     
distance    "664698"          "36583"   "41967"  
duration    "6 hours 30 mins" "43 mins" "49 mins"
destination "1"               "2"       "3"      

$`SW13 9JT`
            [,1]              [,2]      [,3]     
distance    "682210"          "9476"    "13125"  
duration    "6 hours 39 mins" "22 mins" "27 mins"
destination "1"               "2"       "3"  

【讨论】:

  • 看起来不错...谢谢...我知道这可能是一件小事,但距离值...这是什么单位?其次,有没有办法获得学生名单与其最近的学校之间的距离,而不是学生与其他学生之间的距离?此外,有没有办法将其合并到googleVis 中,以将学生的分数视为一种颜色,将学校视为另一种颜色?
  • @h.l.m 是以米为单位的距离。距离由邮政编码给出。如果你提供正确的邮政编码,我会告诉你距离。
  • 谢谢...有机会为每个学生获取最近学校的代码吗?而不是学生之间的距离?
  • 我在第一个 lapply() 调用 xpathApply 中遇到错误(doc, "/GeocodeResponse/result/geometry/location/lat", : subscript out of bounds
猜你喜欢
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多