R: Write RasterStack and preserve layer names

前端 未结 3 1379
你的背包
你的背包 2021-02-01 18:20

I have a raster stack, stk, consisting of three raster images in R. Here is a simple example

# set up a raster stack with three layers
> library(         


        
3条回答
  •  伪装坚强ぢ
    2021-02-01 19:03

    I wrote my files as ENVI files and changed the band names in the ENVI header file. The files can now be opened in ENVI and ArcGis and the layer names are preserved.

    #write ENVI file (.envi; .hdr; .envi.aux.xml) with automatic layer names
    writeRaster(stk, "myStack" , format="ENVI")
    
    #change layer names in ENVI header (.hdr):
    n="myStack.hdr"  
    x <- readLines(n)
    x <- gsub("Band 1,", "one,", x) 
    x <- gsub("Band 2,", "two," , x)
    x <- gsub("Band 3", "three", x)  
    cat(x, file=n, sep="\n") #overwrites the old ENVI header
    

    /edit I just noticed that when the .envi file is imported back into R the layer names are removed again. Same problem in SAGA.

    image <- stack("myStack.envi")  
    names(image)
    #[1] "myStack.1" "myStack.2" "myStack.3"
    
    image = readGDAL("myStack.envi") 
    names(image)
    #[1] "band1" "band2" "band3"
    

提交回复
热议问题