使用谷歌的邮政编码距离

时间:2013-01-09 15:58:21

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

我有两个邮政编码列表(在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")

(N.B。我的实际数据显然明显大于给定的数据,因此可扩展性很有用......)

googleVis或任何其他软件包应该怎么做才能完成上述工作?

1 个答案:

答案 0 :(得分:7)

我会从这样开始得到lat / long

为每个邮政编码获取lat / long

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"