简体   繁体   English

R function 在 system() 调用后停止

[英]R function stops after system() call

I've written a very easy wrapper around GDAL in R.我在 R 中围绕 GDAL 编写了一个非常简单的包装器。 It utilises a prewritten statement which is passed to system, creating an output, which I then want to read into the R environment again.它利用传递给系统的预先编写的语句,创建 output,然后我想再次将其读入 R 环境。

It works by creates a temporary directory in the working directory, printing out an ESRI shape file of our area of interest, and then cuts a raster by this, with some preset information.它的工作原理是在工作目录中创建一个临时目录,打印出我们感兴趣的区域的 ESRI 形状文件,然后以此切割光栅,并带有一些预设信息。

My problem: after successfully running the system() call and creating the output file, the function stops.我的问题:成功运行 system() 调用并创建 output 文件后,function 停止。 It doesn't execute the next call and read the output into the R environment.它不执行下一个调用,并将 output 读入 R 环境。

gdalwarpwarp <- function(source_file, source_srs, newfilename, reread=TRUE, clean=TRUE, cutline){

  #Create tempfolder if it doesn't exist in the working directory.
  if (!dir.exists("./tempfolder")){
    dir.create("./tempfolder")
  }

  #Write temporary shape file
  terra::writeVector(cutline, filename = "./tempfolder/outline_AOI.shp" , filetype='ESRI Shapefile',overwrite=TRUE)

  #Warp!

  if(reread==FALSE){
    system(paste0("gdalwarp -cutline ./tempfolder/outline_AOI.shp -dstnodata -9999 -s_srs EPSG:3006 ",source_file, " ",paste0("./tempfolder/",newfilename)))
    message('warp warped TRUE')

  } else if(reread==TRUE){
    system(paste0("gdalwarp -cutline ./tempfolder/outline_AOI.shp -dstnodata -9999 -s_srs EPSG:3006  ",source_file, " ",paste0("./tempfolder/",newfilename)))
    newfilename <- terra::rast(paste0("./tempfolder/",newfilename))
  }

}

This doesn't run:这不运行:

newfilename <- terra::rast(paste0("./tempfolder/",newfilename))

The function did not return anything. function 没有返回任何东西。 Here is a somewhat improved version of your function.这是您的 function 的稍微改进的版本。 If you want to keep the output it would make more sense to provide a full path, rather then saving it to a temp folder.如果您想保留 output 提供完整路径会更有意义,而不是将其保存到临时文件夹。 I also note that you are not using the argument source_srs我还注意到您没有使用参数source_srs

gdalwarpwarp <- function(source_file, source_srs, newfilename, reread=TRUE, clean=TRUE, cutline){

  #Write temporary shape file
  shpf <- file.path(tempdir(), "aoi.shp")
  terra::writeVector(cutline, filename = shpf, filetype='ESRI Shapefile',overwrite=TRUE)

   outf <- file.path(tempdir(), newfilename)
    system(paste0("gdalwarp -cutline shpf -dstnodata -9999 -s_srs EPSG:3006 ",source_file, " ", outf)
   
    if (reread) {
        terra::rast(outf)
    } else {
       message('warp warped TRUE')
       invisible(filename)
    }
}

I wonder why you don't use terra::resample or terra::project ;我想知道你为什么不使用terra::resampleterra::project perhaps preceded or followed by mask (I may not understand the benefit of using cutline .可能在mask之前或之后(我可能不明白使用cutline的好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM