df %>% mutate(id = row_number()) -> df
library(parallel)
makeCluster(5) -> cl
dir.create("rds") clusterExport(cl, "%>%") clusterExport(cl, "df") parLapply(cl, 1:nrow(df), function(x){ try({ if(!file.exists(paste0("rds/", df$id[x], ".json"))) { download.file(paste0("https://api.map.baidu.com/geocoding/v3/?address=", df$name[x], "&output=json&ak=dAAFbkBggv7yKtqwmRIEMoDPw07oYHgJ&ret_coordtype=gcj02ll"), paste0("rds/", df$id[x], ".json")) } }) }) -> res
fs::dir_ls("rds") -> ls length(ls) parLapply(cl, ls, function(x){ jsonlite::fromJSON(x) -> lst lst$result$location %>% paste0(collapse = ",") -> loc tibble::tibble(id = x, location = loc) }) %>% bind_rows() -> df2
df2 %>% mutate(addid = basename(id), addid = str_remove_all(addid, "\\.json")) %>% filter(str_detect(location, ",") & !str_detect(location, "rror")) -> df2
df2 %>% select(id = addid, location) %>% type_convert() %>% arrange(id) -> df2
df %>% anti_join(df2) -> df
source("坐标转换.R")
df2 %>% separate(location, into = c("lng", "lat"), sep = ",", remove = F) %>% mutate(经度 = as.numeric(lng), 纬度 = as.numeric(lat), value2 = map2_chr(经度, 纬度, GCJ02_WGS84)) %>% select(-contains("度")) %>% separate(value2, into = c("经度", "纬度"), sep = ",") %>% type_convert() %>% select(-lat, -lng) %>% select(id, lat = 纬度, lng = 经度, everything()) %>% select(-location) -> dfall2
dfall2
|
评论