我有一个具有纬度和经度坐标的csv文件。样本:
Lat Lon
94.2 13.4
32.2 12.4
89.3 24.4下面的代码循环通过这些Lat/Lon坐标,在Google上找到这个位置的相关图像,然后我可以在代码单元格下面的R中看到它。
但是,使用上面的示例数据,将返回3幅图像。我想将它们保存到我工作目录外的一个特定的“图像”文件夹中的硬盘驱动器中。有办法这样做吗?
# install.packages('googleway')
myfunction <- function(Lat, Lon){
google_streetview(
location = c(Lat, Lng), # lat/lon coordinates
size = c(600, 400), # w x h
)
}
purrr::map2(data$Lat, data$Lon, myfunction)发布于 2021-06-27 12:54:11
lapply依次将该函数应用于列表中的每个元素例如,以下(未经测试)代码应该将您的图像保存在一系列名为image00001.jpg、img00002.jpg等文件中。
library(tidyverse)
positions <- list(c("lat"=94.2, "lon"=13.4),c("lat"=32.2, "lon"=12.4),c("lat"=89.3, "lon"=24.2))
imgCount <- 0
lapply(
positions,
function(x) {
google_streetview(
location = c(x$lat, x$lon), # lat/lon coordinates
size = c(600, 400), # w x h
)
imgCount <<- imgCount + 1
ggsave(paste0("image", sprintf("%05d", imgCount), ".jpg"))
}
)注意使用<<-确保计数器增量正确。
https://stackoverflow.com/questions/68150652
复制相似问题