消除单词直到定义结果

时间:2018-03-30 00:19:08

标签: r for-loop geolocation ggmap

我目前正在使用地理编码使代码返回地址的纬度和长度。

library(ggmap)

name <- "720-37, Chorok-ro, Yanggam-myeon, Hwaseong-si, Gyeonggi-do, Republic of Korea"
address <- geocode(name)
df <- data.frame(lat = as.numeric(address[2]), lon = as.numeric(address[1]))

如果名称句子中的地址在google中没有结果,则会自动返回NA为lat。

因此,如果有结果,我怎么能继续消除这些词(通常,如果地址太明确,则没有结果)。在这种情况下,如果&#34; 720-37,chorok-ro&#34;被淘汰了,它有效。

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,我可以使用包中的geocode函数对您提供的地址进行地理编码,因此这不是一个好的测试用例。因此,我通过在开头添加两个新单词来改变您的测试用例。

# Test case
name <- "Alpha, Beta, 720-37, Chorok-ro, Yanggam-myeon, Hwaseong-si, Gyeonggi-do, Republic of Korea"

这里我展示了标准的geocode功能不起作用。

library(ggmap)
geocode(name)
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Alpha,%20Beta,%20720-37,%20Chorok-ro,%20Yanggam-myeon,%20Hwaseong-si,%20Gyeonggi-do,%20Republic%20of%20Korea&sensor=false
  lon lat
1  NA  NA
Warning message:
geocode failed with status ZERO_RESULTS, location = "Alpha, Beta, 720-37, Chorok-ro, Yanggam-myeon, Hwaseong-si, Gyeonggi-do, Republic of Korea" 

然后我设计了一个函数来执行“逐步”地理编码,它使用while循环来检查是否有结果。如果没有,请删除第一个单词,然后再试一次,直到有结果。

# A function to perform the geocode by step-wise eliminating the word from the top
geocode_step <- function(name){
  # Perform geocode
  coords <- geocode(name)
  # Use while loop to check the result, if both lat and lon are NA
  # Remove the first word and then try again
  while (is.na(coords[[1]]) & is.na(coords[[2]])){
    name_vec <- strsplit(name, split = ",")[[1]][-1]
    # All words are eliminated, stop the function and return a data frame with NA and warning
    if (length(name_vec) == 0){
      break
    }
    # Re-combine all words
    name <- paste(name_vec, collapse = ", ")
    # Conduct geocode again
    coords <- geocode(name)
  }
  dat <- data.frame(lon = coords[[1]], lat = coords[[2]], name = name)
  return(dat)
}

我们可以按如下方式测试该功能。

geocode_step(name)
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Alpha,%20Beta,%20720-37,%20Chorok-ro,%20Yanggam-myeon,%20Hwaseong-si,%20Gyeonggi-do,%20Republic%20of%20Korea&sensor=false
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=%20Beta,%20%20720-37,%20%20Chorok-ro,%20%20Yanggam-myeon,%20%20Hwaseong-si,%20%20Gyeonggi-do,%20%20Republic%20of%20Korea&sensor=false
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=%20%20720-37,%20%20%20Chorok-ro,%20%20%20Yanggam-myeon,%20%20%20Hwaseong-si,%20%20%20Gyeonggi-do,%20%20%20Republic%20of%20Korea&sensor=false
       lon      lat
1 126.9827 37.11354
                                                                                       name
1   720-37,   Chorok-ro,   Yanggam-myeon,   Hwaseong-si,   Gyeonggi-do,   Republic of Korea
Warning messages:
1: geocode failed with status ZERO_RESULTS, location = "Alpha, Beta, 720-37, Chorok-ro, Yanggam-myeon, Hwaseong-si, Gyeonggi-do, Republic of Korea" 
2: geocode failed with status ZERO_RESULTS, location = " Beta,  720-37,  Chorok-ro,  Yanggam-myeon,  Hwaseong-si,  Gyeonggi-do,  Republic of Korea" 

最后,如果没有任何单词可行,该函数仍会返回NA的数据框。

geocode_step("aawsd")
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=aawsd&sensor=false
  lon lat  name
1  NA  NA aawsd
Warning message:
geocode failed with status ZERO_RESULTS, location = "aawsd"