0

I am using this recipe from r-bloggers.com to plot a basic map.

geocode() returns "NA" for several of the adresses that I am trying to plot. All adresses returning "NA" have the common attribute that they contain Danish letters "æ,Æ, ø,Ø, å,Å", so I strongly suspect that this is what is causing me problems.

Error message returned for address "Eskærvej 50, 5700 Svendborg, denmark"

cannot open URL 'http://maps.googleapis.com/maps/api/geocode/json?
address=Esk%E6rvej%2050,%205700%20Svendborg,%20denmark&sensor=false': HTTP 
status was '400 Bad Request'
2: In FUN(X[[i]], ...) :
    geocoding failed for "Eskærvej 50, 5700 Svendborg, denmark".
  if accompanied by 500 Internal Server Error with using dsk, try google.

Do you know of any way to work around this?

An example from my code:

    # CREATE DATA FRAME
df.svendborg_locations <- tibble(location = c("Belvedere 60, 5700 Svendborg, denmark"
                                          ,"Fruerstuevej 17 B, 5700 Svendborg, denmark"
                                          ,"Enghavevej 40, 5700 Svendborg, denmark"
                                          ,"Engdraget 2, 5700 Svendborg, denmark"
                                          ,"Belvedere 54, 5700 Svendborg, denmark"
                                          ,"Eskærvej 50, 5700 Svendborg, denmark"
                                          ,"Lundbyvej 43, 5700 Svendborg, denmark"
                                          ,"Eskærvej 63 B, 5700 Svendborg, denmark"
                                          ,"Skovbrynet 1, 5700 Svendborg, denmark"
                                          ,"Wiggers Park 201, 5700 Svendborg, denmark"
                                          ,"Hellet 3, 5700 Svendborg, denmark"
                                          ,"Gambøtvej 2, 5700 Svendborg, denmark"
                                          ,"Oluf Rings Vej 19, 5881 Skårup Fyn, denmark"
                                          ,"Rantzausmindevej 172 C, 5700 Svendborg"))
# GEOCODE
geo.svendborg_locations <- geocode(df.svendborg_locations$location)

# COMBINE DATA
df.svendborg_locations <- cbind(df.svendborg_locations, geo.svendborg_locations)

session info

sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

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

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

other attached packages:
 [1] ggmap_2.6.1          gganimate_0.1.0.9000 gridExtra_2.3        dplyr_0.7.3          purrr_0.2.3          readr_1.1.1         
 [7] tidyr_0.7.1          tibble_1.3.4         ggplot2_2.2.1        tidyverse_1.1.1     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13      lubridate_1.6.0   lattice_0.20-35   png_0.1-7         assertthat_0.2.0  digest_0.6.12     psych_1.7.8       mime_0.5         
 [9] R6_2.2.2          cellranger_1.1.0  plyr_1.8.4        httr_1.3.1        RgoogleMaps_1.4.1 rlang_0.1.2       lazyeval_0.2.0    readxl_1.0.0     
[17] geosphere_1.5-7   miniUI_0.1.1      proto_1.0.0       labeling_0.3      stringr_1.2.0     foreign_0.8-69    munsell_0.4.3     shiny_1.0.5      
[25] broom_0.4.2       compiler_3.4.1    httpuv_1.3.5      modelr_0.1.1      pkgconfig_2.0.1   mnormt_1.5-5      htmltools_0.3.6   grid_3.4.1       
[33] nlme_3.1-131      jsonlite_1.5      xtable_1.8-2      gtable_0.2.0      magrittr_1.5      scales_0.5.0      stringi_1.1.5     mapproj_1.2-5    
[41] reshape2_1.4.2    bindrcpp_0.2      sp_1.2-5          xml2_1.1.1        rjson_0.2.15      tools_3.4.1       forcats_0.2.0     glue_1.1.1       
[49] maps_3.2.0        hms_0.3           jpeg_0.1-8        parallel_3.4.1    yaml_2.1.14       colorspace_1.3-2  rvest_0.3.2       ggExtra_0.7      
[57] knitr_1.17        bindr_0.1         haven_1.1.0  
Steen Harsted
  • 1,802
  • 2
  • 21
  • 34

1 Answers1

2

Use enc2utf8

geo.svendborg_locations <- geocode(enc2utf8(df.svendborg_locations$location))

That worked for me, although I got rate limited mid testing...

With OP's code, I got 5 rows with NAs, accompanied by 500 error codes.

With enc2utf8, I got 2 rows with NAs, both with error geocode failed with status OVER_QUERY_LIMIT, location = "Lundbyvej 43, 5700 Svendborg, denmark"

anotherfred
  • 1,330
  • 19
  • 25
  • "That worked for me" is typically not a useful anecdote when dealing with encoding troubles. Your success now depends on the encoding of the copy paste operation you did from the browser, the text file you saved it in and the R session encoding. – trosendal Nov 15 '17 at 11:11
  • True, although I understand `enc2utf8` is designed to handle most scenarios. – anotherfred Nov 15 '17 at 11:14
  • The enc2uft8() worked to solve the issue with danish letters. I also got the "over-query-limit" error. I solved this by getting an API Key from the Google API Console, and then using the register_google() function available in devtools::install_github("dkahle/ggmap"). The approach is well explained in the top answer to this question: https://stackoverflow.com/questions/36175529/getting-over-query-limit-after-one-request-with-geocode Thank you. – Steen Harsted Nov 15 '17 at 12:00
  • Glad it helped - sorry I didn't explain about the rate limiting but I sort of assumed you had an API key already – anotherfred Nov 15 '17 at 12:04