【问题标题】:Sample and moving rasters between folders在文件夹之间采样和移动栅格
【发布时间】:2019-08-27 15:13:03
【问题描述】:

我想在一个目录中采样栅格并移动到一个新的 (train) 并创建另一个未采样的栅格 (test1)。

为此我做了:

library(raster)

# Example data
r <- raster(ncol=10, nrow=10)

# 10 layers
s <- stack(lapply(1:10, function(i) setValues(r, runif(ncell(r)))))

#Create GeoTIFF for each layer
sl<-1:10
for (i in 1:length(sl)){
writeRaster(s[[i]],filename=paste(sl[i],sep=""),
                  format="GTiff",datatype="FLT4S",overwrite=TRUE)
}

#Imagens crete in batch
f <- list.files(getwd(), pattern = ".tif") 
ras <- lapply(f,raster)


#Sample 80% of images for calibration
rasS<-sample(ras,round(length(ras)*0.8,digits=0))
dir.create("train")
file.copy(list.files(rasS),"train")

Error in list.files(rasS) : invalid 'path' argument

#Not sample images - 20%
rasT<- ras[ras!=rasS]

Error in ras != rasS : comparison of these types is not implemented
In addition: Warning message:
In ras != rasS :
  longer object length is not a multiple of shorter object length

dir.create("test1")
file.copy(list.files(rasT),"train")

我必须解决问题,首先,我的 rasS 列表是无效的“路径”参数,并且 != 参数不适用于非样本栅格选择。有什么想法,请!

【问题讨论】:

    标签: r raster r-raster


    【解决方案1】:

    您的问题在这里file.copy(list.files(rasS),"train")。 rasS 列表对象不是目录,不能传递给list.files。您还试图错误地采样和索引。

    尝试类似:

    ( sidx <- sample(1:length(ras), round(length(ras)*0.8,digits=0)) )
      ( rasS <- ras[sidx] )
      ( rasT <- ras[-sidx] )
    
    dir.create("train")
    
    lapply(rasS, FUN=function(x) {
      writeRaster(x, file.path(getwd(),"train", paste0(names(x), ".tif")), )
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多