2015-03-02 60 views
3

我试图从谷歌地理编码API获取经纬度,但当丹麦本地字符在地址中时请求失败。我怀疑这是因为httr :: GET函数会对url进行编码,但我不确定我是否正确。避免在R的网址编码

如果复制/直接粘贴此链接到浏览器中,你得到一个有效的结果: http://maps.googleapis.com/maps/api/geocode/json?address =Søholmen+ 9,+ 4500 +丹麦

但下面的代码是无效的,即使网址是同前它被解析成GET函数。如果我使用没有本地字符的地址,它会起作用。

library(httr) 
library(jsonlite) 
library(stringr) 

address <- "Søholmen 9, 4500 Denmark" 
# address <- "Kronprinsesse Sofies Vej 6, 2000 Denmark" 

base_url <- "http://maps.googleapis.com/maps/api/geocode/json?" 

# An address OR components 
geo_url <- paste0(base_url, "address=", str_replace_all(address, pattern = " ", replacement = "+")) 

# Get the result 
# get the content 
# Parse the JSON 
temp_geo_results <- httr::GET(url = URLencode(URL = geo_url), verbose()) 
temp_geo_results <- httr::content(temp_geo_results, as = "text") 
temp_geo_results <- jsonlite::fromJSON(temp_geo_results) 

这里是我的sessionInfo()

R version 3.1.2 (2014-10-31) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=Danish_Denmark.1252 LC_CTYPE=Danish_Denmark.1252  LC_MONETARY=Danish_Denmark.1252 
[4] LC_NUMERIC=C     LC_TIME=Danish_Denmark.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] stringr_0.6.2 jsonlite_0.9.10 httr_0.5  

loaded via a namespace (and not attached): 
[1] RCurl_1.95-4.3 tools_3.1.2 

编辑:我删除一行代码没有必要的问题,并增加我的sessionInfo。

+0

也许另一种选择是使用另一种编码选项,并建立使用功能build_url/parse_url网址从httr包,但我不知道如何做到这一点。 – KERO 2015-03-02 09:15:57

+0

此网址也提供了正确的回复: 'http://maps.googleapis.com/maps/api/geocode/json?address = S%C3%B8holmen + 9,+ 4500 + Denmark' 因此,您可能需要手动编码,像@KERO建议,然后它会工作。 – LauriK 2015-03-02 09:17:26

+0

@LauriK如果我复制/粘贴您的网址到我的浏览器和GET功能,我收到了一个“坏请求”/“零结果”回来。 – KERO 2015-03-02 09:23:55

回答

4

这似乎是一个编码问题。

对我来说,以下罚款作品:

address <- "Søholmen 9, 4500 Denmark" 
u <- sprintf("http://maps.googleapis.com/maps/api/geocode/json?address=%s", 
      gsub('\\s+', '+', enc2utf8(address))) 

fromJSON(content(GET(u), as='text')) 
+0

这解决了这个问题!谢谢! – KERO 2015-03-02 09:54:52

+2

我试图将地址封装在enc2utf8中,并且可以工作。我认为我可以通过将R文件保存为UTF-8来避免这些问题。显然不是。感谢您的帮助。非常感激! – KERO 2015-03-02 09:58:04

+0

好点 - 'enc2utf8'在这里可能更明智。我会编辑。 – jbaums 2015-03-02 10:05:27

-1

我可以分享原油路我如何在我的语言解决的非常相同的问题:

deencode <- function(text){ 
    output <- NULL 
    for(i in 1:length(text)){ 
    temp <- text[i] 
    temp <- gsub("ā", "a", temp) 
    temp <- gsub("Ā", "A", temp) 
    temp <- gsub("č", "c", temp) 
    temp <- gsub("Č", "C", temp) 
    temp <- gsub("ē", "e", temp) 
    temp <- gsub("Ē", "E", temp) 
    temp <- gsub("ģ", "g", temp) 
    temp <- gsub("Ģ", "G", temp) 
    temp <- gsub("ī", "i", temp) 
    temp <- gsub("Ī", "I", temp) 
    temp <- gsub("ķ", "k", temp) 
    temp <- gsub("Ķ", "K", temp) 
    temp <- gsub("ļ", "l", temp) 
    temp <- gsub("Ļ", "L", temp) 
    temp <- gsub("ņ", "n", temp) 
    temp <- gsub("Ņ", "N", temp) 
    temp <- gsub("š", "s", temp) 
    temp <- gsub("Š", "S", temp) 
    temp <- gsub("ū", "u", temp) 
    temp <- gsub("Ū", "u", temp) 
    temp <- gsub("ž", "z", temp) 
    temp <- gsub("Ž", "Z", temp) 
    output <- c(output, temp) 
    } 
    return(output) 
} 

这个简单的替代它的所有工作,至少在谷歌的地理编码API之后。

+0

'gsub'是矢量化的,所以你不需要遍历'text'的元素:) – jbaums 2015-03-02 09:56:26

0

可以使用rvest包

library(rvest); library(jsonlite) 
address <- "Søholmen 9, 4500 Denmark" 
# address <- "Kronprinsesse Sofies Vej 6, 2000 Denmark" 
base_url <- "http://maps.googleapis.com/maps/api/geocode/json?" 

# An address OR components 
geo_url <- paste0(base_url, "address=", str_replace_all(address, pattern = " ", replacement = "+")) 
geo_url <- iconv(geo_url, to="UTF-8") 

temp_geo_results <- html_text(html_nodes(html(geo_url) , "p")) 
temp_geo_results <- fromJSON(temp_geo_results) 
+0

感谢帕斯卡尔指出这一点。我已经更新了我的答案。 – 2015-03-03 08:37:36