当我尝试使用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")
}
非常感谢帮助。我可能做的事非常愚蠢。
答案 0 :(得分:0)
我非常确定您的问题出在您创建shapefile名称的行中。使用gsub(".csv","cru_drought_",m)
将取代&#34; .csv&#34;与&#34; cru_drought _&#34;在字符串m
中,这只是您循环的整数。找不到该字符串,因此它只是将整数m
分配给dsn
,因此您最终会尝试编写名称为&#34; 1&#34;,&#34的shapefile ; 2&#34;等等。我认为你只有gsub
个参数有点混乱。
我注释掉了代码中特定于您的文件的部分,并尝试将文件名放在一起,看起来就像您之后的那样。
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-04-14。
答案 1 :(得分:0)
使用raster::shapefile
而不是writeOGR
shapefile(plot.locationsSP_DROUGHT, dsn)