[英]How do you write multiple rasters in [r]?
I am using lapply() to load 144 rasters as in my previous post: How do you load multiple rasters in [r] using a for loop?我正在使用 lapply() 加载 144 个栅格,就像我之前的帖子一样: 如何使用 for 循环在 [r] 中加载多个栅格?
library(raster)
rastlist <- list.files(path=path, pattern='tif$', full.names=TRUE)
allrasters <- lapply(rastlist, raster)
allrasters ends up being a large list with 144 elements, of which 'name' looks like one of the attributes, I pasted the last (144th) element output below. allrasters 最终成为一个包含 144 个元素的大列表,其中 'name' 看起来像是属性之一,我粘贴了下面的最后一个(第 144 个)元素输出。
[[144]] class : RasterLayer dimensions : 405, 345, 139725 (nrow, ncol, ncell) resolution : 30, 30 (x, y) extent : -971895, -961545, 1463535, 1475685 (xmin, xmax, ymin, ymax) crs : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0 source : T://abbreviatedpath/sample.tif names : wildcard1_name_wildcard2 values : -32768, 32767 (min, max) [[144]] 类:RasterLayer 尺寸:405、345、139725(nrow、ncol、ncell)分辨率:30、30(x、y)范围:-971895、-961545、1463535、1475685、最小x ymax) crs : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84= 0,0,0 来源:T://abbreviatedpath/sample.tif 名称:wildcard1_name_wildcard2 值:-32768、32767(最小值、最大值)
I would like to do some processing on subsets of these rasters based on wildcards in the names.我想根据名称中的通配符对这些栅格的子集进行一些处理。 ie, I would like to mask those with wildcard1=x and save that output as the input raster name appended with "_m".
即,我想用通配符1 = x 屏蔽那些并将该输出保存为附加有“_m”的输入栅格名称。 Later I would like to mosaic a subset of my allrasters list based on wildcard2=y.
后来我想根据通配符 2=y 镶嵌我的所有栅格列表的一个子集。
My efforts to extract a list of rasters based on a pattern in the names from the already-loaded list of rasters (allrasters) failed.我从已加载的栅格列表(所有栅格)中根据名称中的模式提取栅格列表的努力失败了。
rast.x<-grep("x",allrasters)
yeilds a vector of the 6 list numbers that correlate to the rasters I am trying to extract into a subset.产生与我试图提取到子集中的栅格相关的 6 个列表数字的向量。 Maybe this is a better approach?
也许这是一个更好的方法?
The only work-around I have found is to approach with a different process/pipeline: First create subset lists, then load rasters in subset lists with lapply, then apply functions to rasters in subset lists with lapply, like here:我发现的唯一解决方法是使用不同的流程/管道:首先创建子集列表,然后使用 lapply 加载子集列表中的栅格,然后使用 lapply 将函数应用于子集列表中的栅格,如下所示:
rastlist.HIGH <- list.files(path=path, pattern='HIGH', full.names=TRUE)
allrast.HIGH <- lapply(rastlist.HIGH, raster)
allrast.HIGH_m<-lapply(allrast.HIGH,mask,HIGH_mask,updatevalue=NA,updateNA=FALSE)
This process seems to be working, except now I am stuck on how to write the rasters in the list allrast.HIGH_m这个过程似乎正在起作用,但现在我被困在如何在列表 allrast.HIGH_m 中编写栅格
The post https://gis.stackexchange.com/questions/301956/write-multiple-rasters-in-r never seemed to be resolved, and perhaps it didn't work because it of the using-a-for-loop-on-an-S4-object issue.帖子https://gis.stackexchange.com/questions/301956/write-multiple-rasters-in-r似乎从未得到解决,也许它不起作用,因为它使用了 using-a-for-loop-关于 S4 对象的问题。
The lapply() approach to write all these rasters does not seem to be working: lapply() 编写所有这些光栅的方法似乎不起作用:
lapply(allrast.HIGH_m, writeRaster(filename=paste0(path),"/masked/",names(allrast.HIGH_m),"_m"), format="GTiff"))
This code gives me the error: Error: unexpected ')' in "lapply(allrast.HIGH_m, writeRaster(filename=paste0(path),"/masked/",names(allrast.HIGH_m),"_m"), format="GTiff"))"这段代码给了我错误:错误:意外 ')' in "lapply(allrast.HIGH_m, writeRaster(filename=paste0(path),"/masked/",names(allrast.HIGH_m),"_m"), format= "GTiff"))"
Any ideas on how to write multiple rasters that are not stacked, nor that are the same extent, from a list?关于如何从列表中编写多个未堆叠或范围相同的栅格的任何想法? And give them names which are the input name appended with something?
并给他们命名,即输入名称后附加一些东西? Thank you!!
谢谢!!
尝试使用此代码,它应该可以工作。
lapply(allrast.HIGH_m, function (x) writeRaster(x, filename=paste0(path,"/masked/",names(x),"_m"), format="GTiff"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.