Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中拆分光栅迭代或循环_R_Loops_Split_Raster - Fatal编程技术网

在R中拆分光栅迭代或循环

在R中拆分光栅迭代或循环,r,loops,split,raster,R,Loops,Split,Raster,R编程语言(此语言为新语言) 我试图循环通过splitRaster输出的许多平铺光栅。在循环过程中,我想对每个光栅执行一些处理 但是下面的代码抛出了一个错误 library(ForestTools) library(raster) library(sp) library(rgdal) library(SpaDES) rm(list = ls()) tmpdir <- file.path(tempdir(), "splitRaster") lin <- functi

R编程语言(此语言为新语言)

我试图循环通过splitRaster输出的许多平铺光栅。在循环过程中,我想对每个光栅执行一些处理

但是下面的代码抛出了一个错误

library(ForestTools)
library(raster)
library(sp)
library(rgdal)
library(SpaDES)
rm(list = ls())
tmpdir <- file.path(tempdir(), "splitRaster")
lin <- function(x){x * 0.1 + 0.6}
inCHM <- raster("input raster path and name.tif")
split <- splitRaster(inCHM, 5, 5, c(0.05, 0.05), tmpdir)
files <- list.files(path=tmpdir, pattern="*.grd", full.names=FALSE, recursive=FALSE)
file.names <- dir(tmpdir, pattern ="*.grd")

for(file.names in files ){
name <- file.names
ttops <- vwf(name, winFun = lin, minHeight = 5)
writeOGR(ttops, "output folder", name, driver = "ESRI Shapefile")
}

更多问题(2020年7月24日)

我删除了用于故障排除的循环,而只是选择了一个拆分器输出,该输出将用于循环ie文件[[3]]

当我运行以下代码时,错误是相同的

library(ForestTools)
library(raster)
library(sp)
library(rgdal)
library(SpaDES)
rm(list = ls())
# set temp directory
tmpdir <- "C:\\R-Test\\Temp_Output"
# get raster
r <- raster("C:\\Lidar\\grid_treeheight_max_1m_nofill.tif")
# define projection
projection(r) <- "+proj=utm +zone=50 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
# split raster brick
y <- splitRaster(r, 8, 8, c(0.05, 0.05), tmpdir)
# Get the complete file locations with full.names = T
files <- list.files(path=tmpdir, pattern="*.grd", full.names=FALSE, recursive=FALSE)
tmpfile <- paste(tmpdir, "\\", files[[3]], sep="")
lin <- function(x){x * 0.06 + 0.6}
ttops <- vwf(tmpfile, winFun = lin, minHeight = 5)
当我使用上述代码中的一个splitRaster输出(文件[[3]])运行以下代码时,它不会出错,并且我能够绘制TTOP

rm(list = ls())
# set temp directory
tmpdir <- "D:\\R-Test\\Temp_Output"
# get raster
r <- raster("D:\\R-Test\\Temp_Output\\Xgrid_treeheight_max_1m_nofill_tile11.grd")
lin <- function(x){x * 0.06 + 0.6}
ttops <- vwf(r, winFun = lin, minHeight = 5)
rm(list=ls())
#设置临时目录

tmpdir我认为问题在于,您试图用文件名而不是光栅对象为
vwf
函数提供数据。我还建议对循环使用
lappy
而不是
for
。下面是一个应该可以工作的代码

library(raster)
library(ForestTools)
library(rgdal)

# Get the complete file locations with full.names = T
files <- list.files(path=tmpdir, pattern="*.grd", full.names=T, recursive=FALSE)

# Loop over each item of the list, i.e., each raster
lapply(files, function(x){
  # Load the image as raster
  image <- raster(x)
  # Calculate vwf (I added a dummy function for winFun)
  ttops <- vwf(image, winFun = function(x){x * 0.06 + 0.5}, minHeight = 5)
  # Write the file with the name of each raster
  writeOGR(ttops, "output_dir", names(x), driver = "ESRI Shapefile")
})
库(光栅)
图书馆(森林工具)
图书馆(rgdal)
#使用full.names=T获取完整的文件位置

文件我认为问题在于,您试图使用文件名而不是光栅对象为
vwf
函数提供数据。我还建议对循环使用
lappy
而不是
for
。下面是一个应该可以工作的代码

library(raster)
library(ForestTools)
library(rgdal)

# Get the complete file locations with full.names = T
files <- list.files(path=tmpdir, pattern="*.grd", full.names=T, recursive=FALSE)

# Loop over each item of the list, i.e., each raster
lapply(files, function(x){
  # Load the image as raster
  image <- raster(x)
  # Calculate vwf (I added a dummy function for winFun)
  ttops <- vwf(image, winFun = function(x){x * 0.06 + 0.5}, minHeight = 5)
  # Write the file with the name of each raster
  writeOGR(ttops, "output_dir", names(x), driver = "ESRI Shapefile")
})
库(光栅)
图书馆(森林工具)
图书馆(rgdal)
#使用full.names=T获取完整的文件位置

文件writeOGR中的names(x)变量似乎导致循环失败。可能所有光栅都有相同的名称?这可能导致R试图覆盖同一文件。为避免为文件名创建另一个对象,例如:
names\u file,这是writeOGR中的错误消息error(ttops,“D:\\R-Test\\Temp\u Output”,names(x),driver=“ESRI Shapefile”):CHAR()只能应用于“CHARSXP”,而不能应用于“NULL”打印(name(x)显示由NULL和[[1]]NULL[[2]]NULL组成的多个值。etcMmm似乎没有名称。您可以尝试使用我在Comments中发布的其他代码。如果名称(x)出现writeOGR中的变量导致循环失败。可能所有光栅都具有相同的名称?这可能会导致R尝试覆盖相同的文件。为避免为文件名创建另一个对象,例如:
names\u file,这是writeOGR中的错误消息error(ttops,“D:\\R-Test\\Temp\u Output”,names(x),driver=“ESRI Shapefile”):CHAR()只能应用于“CHARSXP”,而不能应用于“NULL”打印(名称(x)显示由NULL和[[1]]NULL[[2]]NULL组成的多个值etcMmm看起来好像没有名称。您可以尝试使用我在注释中发布的其他代码
library(raster)
library(ForestTools)
library(rgdal)

# Get the complete file locations with full.names = T
files <- list.files(path=tmpdir, pattern="*.grd", full.names=T, recursive=FALSE)

# Loop over each item of the list, i.e., each raster
lapply(files, function(x){
  # Load the image as raster
  image <- raster(x)
  # Calculate vwf (I added a dummy function for winFun)
  ttops <- vwf(image, winFun = function(x){x * 0.06 + 0.5}, minHeight = 5)
  # Write the file with the name of each raster
  writeOGR(ttops, "output_dir", names(x), driver = "ESRI Shapefile")
})