写入栅格时保留图层名称

时间:2019-04-22 21:05:28

标签: raster writing

想要在编写栅格堆栈时保留栅格图层的名称

我尝试使用显示的代码,该代码未显示错误,但该位置未显示任何写在此处的文件。

MosaicedA<- mosaic(Stacked1A,Stacked2A, fun=mean, tolerance=0.5)
names(MosaicedA)<-c('one','Two','three')
################################################################
writeRaster(MosaicedA,filename = names(MosaicedA),bylayer=TRUE,format="ENVI",overwrite=TRUE, "C:/Users/rajeev.bhattarai/Documents/Research/Testing/Rajeev.envi" )

1 个答案:

答案 0 :(得分:0)

这是一种变通方法:

writeENVI <- function(x, f, ...) {
    writeRaster(x, f, overwrite=TRUE, ...)
    cat("band names = {", paste(names(x),collapse=","), "}", "\n", file=extension(f, "hdr"), append=TRUE)
}

示例数据

library(raster)
s <- stack(system.file("external/rlogo.grd", package="raster")) 
names(s)
#[1] "red"   "green" "blue" 

使用功能

writeENVI(s, "test.envi")

检查它是否有效

brick(f)
#class      : RasterBrick 
#dimensions : 77, 101, 7777, 3  (nrow, ncol, ncell, nlayers)
#resolution : 1, 1  (x, y)
#extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#crs        : +proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
#source     : test.envi 
#names      : red, green, blue 
#min values :   0,     0,    0 
#max values : 255,   255,  255 
相关问题