邮编距离使用谷歌 [英] postcode distances using google

查看:183
本文介绍了邮编距离使用谷歌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个邮编列表(在R中)......一个孩子的地址,他们的学业成绩和一所学校......

我想为了能够得到最接近每个孩子的学校......所以大概需要在邮政编码之间通过转换为长期和经济价值来计算距离?

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

可能是使用googleVis包的东西?



例如.. 。

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

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

(NB My ac tual数据明显大于给定的数据,因此可伸缩性将会非常有用......)

googleVis或任何其他包可以处理该问题完成上面的内容?

解决方案

我会从这样的事情开始获取纬度/经度



为每个邮政编码获取纬度/长度

  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]]
ng = xpathApply(doc,'/ GeocodeResponse / result / geometry / location / lng',xmlValue)[[1]]
c(code = str,lat = lat,lng = lng)
})
#get long / lat为学生
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 / doc /'/ location / lat',xmlValue)[[1]]
lng = xpathApply(doc,'/ GeocodeResponse / result / geometry / location / lng',xmlValue) bc(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%206QE55.6188429 -4.6766226
[2,]SW1A%200AA51.5004864-0.1254664
[3,]WC1X%209NT51.5287992-0.1181098

得到距离矩阵

 库(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])$ ​​b
$ b})
名称(dist.list)< - ll [,1]

dist.list
$ `SL4 6DW`
[,1] [,2] [,3]
距离6646983658341967
持续时间6小时30分钟43分钟 49分钟
目的地123

$`SW13 9JT`
[,1] [,2] [,3]
距离682210947613125
持续时间6小时39分钟22分钟27分钟
目的地123


I have a two lists of postcodes (in R)...one of children's addresses with their academic score and one of schools...

i would like to be able to get the closest school for each child...so presumably a calculation of distance would been needed between postcodes by converting to long and lat values?

And then I would like to be able to plot on a google map all the children per school...and see if the children who live closer to school get better grades...perhaps ploting schools a different colour to kids, and the kids having a gradient of colour according to their score?

perhaps something using the googleVis package?

so for example...

if we have the data for 3 kids and 2 schools...

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. My actual data is obviously significantly larger than the one given so scalability would be useful...)

what should be done with googleVis or any other package for that matter to be able to complete the above?

解决方案

I would start by something like this to get the lat/long

Get lat/long for each post code

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"

get the distance matrix

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"  

这篇关于邮编距离使用谷歌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆