使用 Google 地图在 R 中进行地理编码
我尝试通过 Google 地图和此博文中的 XML
包运行代码来对 R 中的位置进行地理编码: http://www.r-chart .com/2010/07/maps-geocoding-and-r-user-conference.html
以下是他的函数:
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)
}
当我运行 gGeoCode()
时,出现以下错误:
> 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"
如果我只需将 API url 粘贴到浏览器中,并在末尾附加 Philadelphia, PA
,就像传递给 xmlParseTree
的字符串一样,当我下载它。
这是代码的问题,还是我配置失败?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否考虑过使用 json 调用来代替?查看您的代码,您可以实现相同的效果(您需要从 omegahat.com 安装软件包 RCurl 和 RJSONIO)。
将其复制并粘贴到 R 中:
以下是如何使用上述函数:
这是您得到的结果。我认为在原始代码中,lat 和 lng 混淆了?但希望这就是你想要的:
希望对小伙伴有所帮助,
托尼·布雷亚尔
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:
Here's how you use the above functions:
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:
Hope that helps a little mate,
Tony Breyal
此代码仅使用 XML 库即可工作
This code works using just the XML library
这是地理编码的另一个选项 - 它可能更容易解析:
https://webgis。 usc.edu/Services/Geocode/Default.aspx
This is another option for geocoding - it may be easier to parse:
https://webgis.usc.edu/Services/Geocode/Default.aspx
我修改了 Tony Breyal 解决方案,以便 gGeoCode 函数也将地址向量作为输入。在此版本中,您不仅可以执行
gGeoCode("Philadelphia, PA")
操作,还可以执行gGeoCode(c("Philadelphia, PA","New York, NY"))
具有此返回值。请注意,Google 地图 api 的每日限制为 2,500 个,因此您的矢量不应太长。这是更新后的函数:
编辑:代码中的小修正,以便 lat 和 lng 以数值形式返回。
I have modified Tony Breyal solution so that the
gGeoCode
function also takes a vector of addresses as input. With this version, you can not only dogGeoCode("Philadelphia, PA")
but alsogGeoCode(c("Philadelphia, PA","New York, NY"))
with this return value.Note that the google maps api has a daily limit of 2,500 so that your vector shouldn't be too long. Here is the updated function:
EDIT: Small correction in code so that lat and lng are returned as numerical values.
我需要从 geocode 获取所有返回的地址,而不仅仅是第一个地址,因此我编写了一个小函数来执行此操作。它可用于地理编码和反向地理编码
地理编码示例:
反向地理编码示例:
请注意,地址可以是格式化地址或 latlng(
反向
)未使用参数,但它包含供将来与其他地理编码服务一起使用I needed to get all the returned addresses from
geocode
not just the first one, so I wrote a small function to do so. It can be used to geocode and to reverse geocodeGeocode example:
Reverse Geocode example:
note that the address can be either formatted address or latlng, the
reverse
parameter is not used but it is including for future use with other geocoding services这也可以通过我的包 googleway 和有效的 Google 来完成地图 API 密钥
并进行反向地理编码
This can also be done with my package googleway and a valid Google Maps API key
And to reverse-geocode