消除单词直到定义结果
eliminating words until the result is defined
我目前正在使用地理编码编写返回地址经纬度的代码。
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]))
如果name语句中的地址在google中没有结果,它会自动returns NA for lat an long。
所以我怎么一直删字直到有结果(一般地址指定的太细了,是没有结果的)。在这种情况下,如果“720-37,chorok-ro”被消除,它就起作用了。
正如我在评论中提到的,我可以使用 ggmap 包中的 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"
然后我设计了一个函数来进行"stepwise"地理编码,它使用while-loop来检查是否有结果。如果没有,请删除第一个单词,然后重试直到有结果。
# 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"
最后,如果没有任何词会起作用,该函数仍将 return 一个数据框 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"
我目前正在使用地理编码编写返回地址经纬度的代码。
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]))
如果name语句中的地址在google中没有结果,它会自动returns NA for lat an long。
所以我怎么一直删字直到有结果(一般地址指定的太细了,是没有结果的)。在这种情况下,如果“720-37,chorok-ro”被消除,它就起作用了。
正如我在评论中提到的,我可以使用 ggmap 包中的 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"
然后我设计了一个函数来进行"stepwise"地理编码,它使用while-loop来检查是否有结果。如果没有,请删除第一个单词,然后重试直到有结果。
# 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"
最后,如果没有任何词会起作用,该函数仍将 return 一个数据框 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"