使用Google地图在R中进行地理编码 [英] Geocoding in R with Google Maps

查看:251
本文介绍了使用Google地图在R中进行地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试运行代码,通过Google地图对R中的地理位置进行地理编码,并从此博客文章中找到 XML 包:
http://www.r-chart.com/2010/07 /maps-geocoding-and-r-user-conference.html



以下是他的功能:



($ doc $ path){
sapply(getNodeSet
$ gGeoCode = function(str){
library(XML)
u = paste('http://maps.google.com/maps/api/geocode/xml?sensor=false& ; address =',str)
doc = xmlTreeParse(u,useInternal = TRUE)
str = gsub('','%20',str)
lng = getDocNodeVal(doc, / geocodeResponse / result / geometry / location / lat)
lat = getDocNodeVal(doc,/ GeocodeResponse / result / geometry / location / lng)
c(lat,lng)
}

当我运行 gGeoCode()时,以下错误r:

 > gGeoCode(Philadelphia,PA)
未能加载外部实体http%3A // maps.google.com / maps / api / geocode / xml%3Fsensor = false& address =%20Philadelphia,%20PA
错误:1:未能加载外部实体http%3A // maps.google.com / maps / api / geocode / xml%3Fsensor = false& address =%20Philadelphia,%20PA

如果我只用 Philadelphia,PA 将API url粘贴到浏览器中,添加到最后,就像传递给 xmlParseTree 的字符串一样,当我下载它时,我得到的结果看起来像合法的xml。



这是代码问题,还是我没有配置什么东西?

解决方案

你有没有想过关于使用json调用呢?看看你的代码,你可以做到这一点(你需要从omegahat.com安装包RCurl和RJSONIO)。



复制并粘贴到R :

  library(RCurl)
库(RJSONIO)

construct.geocode.url < - function(address,return.call =json,sensor =false){
root< - http://maps.google.com/maps/api/geocode/
u < - paste(root,return.call,?address =,address,& sensor =,sensor,sep =)
return(URLencode(u))
}
$ b $ gGeoCode< - function(address,verbose = FALSE){
if(verbose)cat(address,\\\

u< - construct.geocode .url(address)
doc< - getURL(u)
x< - fromJSON(doc,simplify = FALSE)
if(x $ status ==OK){
lat < - x $ results [[1]] $ geometry $ location $ lat
lng < - x $ results [[1]] $ geometry $ location $ lng
return(c( lat,lng))
} else {
return(c(NA,NA))
}
}

以下是您如何使用上述功能:

  x < -  gGeoCode 宾夕法尼亚州费城)

这是您得到的结果。我认为在原始代码中,lat和lng是混合起来的?但希望这是你想要的:

 > x 
[1] 39.95233 -75.16379

希望对小伙伴有帮助,



Tony Breyal


I've tried running the code to geocode locations in R via Google Maps and the XML package from this blog post: http://www.r-chart.com/2010/07/maps-geocoding-and-r-user-conference.html

Here are his functions:

getDocNodeVal=function(doc, path){
  sapply(getNodeSet(doc, path), function(el) xmlValue(el))
}

gGeoCode=function(str){
  library(XML)
  u=paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
  doc = xmlTreeParse(u, useInternal=TRUE)
  str=gsub(' ','%20',str)
  lng=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lat")
  lat=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lng")
  c(lat,lng)
}

When I run gGeoCode(), I get the following error:

> gGeoCode("Philadelphia, PA")
failed to load external entity "http%3A//maps.google.com/maps/api/geocode/xml%3Fsensor=false&address=%20Philadelphia,%20PA"
Error: 1: failed to load external entity "http%3A//maps.google.com/maps/api/geocode/xml%3Fsensor=false&address=%20Philadelphia,%20PA"

If I just paste into a browser the API url with Philadelphia, PA appended to the end, like the string passed to xmlParseTree, I get a result that looks like legitimate xml when I download it.

Is this an issue with the code, or have I failed to configure something or another?

解决方案

Have you thought about using the json call instead? Looking at your code, you could achieve the same doing this (you'll need to install packages RCurl and RJSONIO from omegahat.com).

Copy and paste this into R:

library(RCurl)
library(RJSONIO)

construct.geocode.url <- function(address, return.call = "json", sensor = "false") {
  root <- "http://maps.google.com/maps/api/geocode/"
  u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
  return(URLencode(u))
}

gGeoCode <- function(address,verbose=FALSE) {
  if(verbose) cat(address,"\n")
  u <- construct.geocode.url(address)
  doc <- getURL(u)
  x <- fromJSON(doc,simplify = FALSE)
  if(x$status=="OK") {
    lat <- x$results[[1]]$geometry$location$lat
    lng <- x$results[[1]]$geometry$location$lng
    return(c(lat, lng))
  } else {
    return(c(NA,NA))
  }
}

Here's how you use the above functions:

x <- gGeoCode("Philadelphia, PA")

This is the result you get. I think in the original code, lat and lng are mixed up? But hopefully this is what you want:

> x
[1]  39.95233 -75.16379

Hope that helps a little mate,

Tony Breyal

这篇关于使用Google地图在R中进行地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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