【问题标题】:R: looping to save multiple shapefilesR:循环保存多个shapefile
【发布时间】:2018-09-24 20:44:53
【问题描述】:

当我尝试使用 writeOGR 和循环来保存我的 shapefile 时,它​​什么也没做,只是给我一条错误消息:

writeOGR(plot.locationsSP_DROUGHT, dsn, layer1, driver = "ESRI Shapefile") 出错:图层存在,使用新图层名称

基本上,我将每个对象转换为 CSV 文件,然后转换为 shapefile,并希望同时保存 CSV 文件和 shapefile。这是我的代码片段:

for (m in 1:500){
#First I want to save my CSV files:
drought.slice <- rotate(drought.array[m,,])
drought.vec <- as.vector(drought.slice)
length(drought.vec)
drought.df01 <- data.frame(cbind(lonlat, drought.vec))
names(drought.df01) <- c("lon", "lat", paste(dname, as.character(m), sep = "_"))
head(na.omit(drought.df01))
csvfile<-paste0("cru_drought_",m,".csv")

#Next I want to create shapefiles from the CSV files:
plot.locations_DROUGHT <- read.csv(paste0("cru_drought_",m,".csv"), stringsAsFactors = FALSE)
plot.locationsSP_DROUGHT <- SpatialPointsDataFrame(plot.locations_DROUGHT[,1:2], plot.locations_DROUGHT)
proj4string(plot.locationsSP_DROUGHT) <- CRS("+init=epsg:4326")
dsn <- layer1 <- gsub(".csv","cru_drought_",m)
writeOGR(plot.locationsSP_DROUGHT, dsn, layer1, driver="ESRI Shapefile")
}

这是我正在使用的完整代码:

    #Open and read the NCDF file, along with longitude and latitude
rm(list=ls())
library(lattice)
library(ncdf4)
library(chron)
library(rgdal)
library(sp)
library(raster)
library(RColorBrewer)
setwd('/Users/Neil/Dropbox/Drought Maps')
ncname <- "owda-orig"
ncfname <- paste(ncname,".nc",sep="")
dname <- "pdsi"
ncin <- nc_open(ncfname)
print(ncin)

lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)
head(lon)

lat <- ncvar_get(ncin, "lat", verbose = F)
nlat <- dim(lat)
head(lat)

print(c(nlon, nlat))

t <- ncvar_get(ncin, "time")
nt <- dim(t)
head(t)

drought.array <- ncvar_get(ncin, dname)
dlname <- ncatt_get(ncin, dname, "long_name")
dunits <- ncatt_get(ncin, dname, "units")
#fillvalue <- ncatt_get(ncin, dname, "_FillValue")
dim(drought.array)

creation_date <- ncatt_get(ncin, 0, "creation_date")
Description <- ncatt_get(ncin, 0, "Description")

nc_close(ncin)



rotate <- function(x) t(apply(x, c(1, 2), rev))

m <- 333
drought.slice <- rotate(drought.array[m,,])
image(lon, lat, drought.slice, col = brewer.pal(10, "BrBG"))

lonlat <- expand.grid(lon, lat)
drought.vec <- as.vector(drought.slice)
length(drought.vec)

drought.df01 <- data.frame(cbind(lonlat, drought.vec))
names(drought.df01) <- c("lon", "lat", paste(dname, as.character(m), sep = "_"))
head(na.omit(drought.df01))


for (m in 1:500){
    drought.slice <- rotate(drought.array[m,,])
    drought.vec <- as.vector(drought.slice)
    length(drought.vec)
    drought.df01 <- data.frame(cbind(lonlat, drought.vec))
    names(drought.df01) <- c("lon", "lat", paste(dname,         as.character(m), sep = "_"))
    head(na.omit(drought.df01))
    csvfile<-paste0("cru_drought_",m,".csv")
    plot.locations_DROUGHT <- read.csv(paste0("cru_drought_",m,".csv"), stringsAsFactors = FALSE)
    plot.locationsSP_DROUGHT <- SpatialPointsDataFrame(plot.locations_DROUGHT[,1:2], plot.locations_DROUGHT)
    proj4string(plot.locationsSP_DROUGHT) <- CRS("+init=epsg:4326")
    dsn <- layer1 <- gsub(".csv","cru_drought_",m)
    writeOGR(plot.locationsSP_DROUGHT, dsn, layer1, driver="ESRI Shapefile")
}

我们将不胜感激。我可能正在做一些非常愚蠢的事情。

【问题讨论】:

    标签: r shapefile rgdal ncdf4


    【解决方案1】:

    我很确定您的问题出在您创建 shapefile 名称的那一行。使用gsub(".csv","cru_drought_",m) 会将字符串m 中的“.csv”替换为“cru_drought_”,这只是您要循环的整数。没有找到那个字符串,所以它只是将整数m 分配给dsn,所以你最终会尝试编写名称为“1”、“2”等的 shapefile。我想你只有@987654326 @arguments 有点乱。

    我注释掉了您的代码中特定于您的文件的部分,并尝试将看起来像您所追求的文件名放在一起。

    for (m in 1:5){
        # drought.slice <- rotate(drought.array[m,,])
        # drought.vec <- as.vector(drought.slice)
        # length(drought.vec)
        # drought.df01 <- data.frame(cbind(lonlat, drought.vec))
        # names(drought.df01) <- c("lon", "lat", paste(dname,         as.character(m), sep = "_"))
        # head(na.omit(drought.df01))
        csvfile<-paste0("cru_drought_",m,".csv")
        # plot.locations_DROUGHT <- read.csv(paste0("cru_drought_",m,".csv"), stringsAsFactors = FALSE)
        # plot.locationsSP_DROUGHT <- SpatialPointsDataFrame(plot.locations_DROUGHT[,1:2], plot.locations_DROUGHT)
        # proj4string(plot.locationsSP_DROUGHT) <- CRS("+init=epsg:4326")
        dsn <- layer1 <- gsub(".csv","_shape",csvfile)
        # writeOGR(plot.locationsSP_DROUGHT, dsn, layer1, driver="ESRI Shapefile")
        print(dsn)
    }
    #> [1] "cru_drought_1_shape"
    #> [1] "cru_drought_2_shape"
    #> [1] "cru_drought_3_shape"
    #> [1] "cru_drought_4_shape"
    #> [1] "cru_drought_5_shape"
    

    reprex package (v0.2.0) 于 2018 年 4 月 14 日创建。

    【讨论】:

    • 非常感谢您,这行得通!我还有一个问题:这个过程似乎为每个 shapefile 创建了单独的文件夹。有没有办法简单地做到这一点,只需将 shapefile 保存在工作目录文件夹中?
    • dsn 参数 writeOGR 是您保存 shapefile 的目录。尝试更改为writeOGR(shapefile, "folder_of_shapes", layer1, driver="ESRI Shapefile")
    【解决方案2】:

    使用raster::shapefile 比使用writeOGR 更容易一些

    shapefile(plot.locationsSP_DROUGHT, dsn)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-26
      • 2020-02-14
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2021-02-25
      • 2020-10-08
      • 1970-01-01
      相关资源
      最近更新 更多