如何在[r]中编写多个栅格? [英] How do you write multiple rasters in [r]?

查看:135
本文介绍了如何在[r]中编写多个栅格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用lapply()加载144个栅格,就像我以前的文章一样: 如何在[ r]使用for循环吗?

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?

library(raster)
rastlist <- list.files(path=path, pattern='tif$', full.names=TRUE)
allrasters <- lapply(rastlist, raster)

allrasters最终是一个包含144个元素的大型列表,其中的名称"看起来像是其中一个属性,我在下面粘贴了最后一个(第144个)元素输出.

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.

[[144]] 类别:RasterLayer 尺寸:405、345、139725(nrow,ncol,ncell) 分辨率:30,30(x,y) 范围:-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 来源:T://abbreviatedpath/sample.tif 名称:wildcard1_name_wildcard2 值:-32768、32767(最小值,最大值)

[[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)

我想基于名称中的通配符对这些栅格的子集进行一些处理.即,我想用通配符1 = x屏蔽那些,并将输出保存为输入栅格名称并附加"_m".稍后,我想基于wildcard2 = y拼接我的过敏名单的一个子集.

I would like to do some processing on subsets of these rasters based on wildcards in the names. i.e., I would like to mask those with wildcard1=x and save that output as the input raster name appended with "_m". Later I would like to mosaic a subset of my allrasters list based on wildcard2=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) 

提供了6个列表编号的矢量,这些矢量与我要提取到子集中的栅格相关.也许这是一个更好的方法?

yeilds a vector of the 6 list numbers that correlate to the rasters I am trying to extract into a subset. Maybe this is a better approach?

我发现的唯一解决方法是采用不同的流程/管道: 首先创建子集列表,然后使用lapply加载子集列表中的栅格,然后使用lapply将函数应用于子集列表中的栅格,例如:

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:

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)

此过程似乎正在工作,除了现在我被困在如何在列表allrast中写栅格.HIGH_m
帖子 https://gis.stackexchange.com/questions/301956/write-multiple- r栅格似乎从未解决过,也许因为它在S4对象上使用了循环而无法使用.
编写所有这些栅格的lapply()方法似乎不起作用:

This process seems to be working, except now I am stuck on how to write the rasters in the list 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.
The lapply() approach to write all these rasters does not seem to be working:

lapply(allrast.HIGH_m, writeRaster(filename=paste0(path),"/masked/",names(allrast.HIGH_m),"_m"), format="GTiff"))

这段代码给我错误:错误:"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"))"

关于如何编写列表中未堆叠或相同范围的多个栅格的任何想法?并给他们起输入名称后面加上一些东西的名称吗?谢谢!!

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!!

推荐答案

尝试使用此代码,它应该可以工作.

Try with this code, it should work.

lapply(allrast.HIGH_m, function (x) writeRaster(x, filename=paste0(path,"/masked/",names(x),"_m"), format="GTiff"))

这篇关于如何在[r]中编写多个栅格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆