通过gdal将numpy 2d数组转换为ENVI二进制文件

Conversion of numpy 2d array to ENVI binary file through gdal

我有SAR CEOS格式的文件,包括数据文件、头文件、空卷目录文件和卷目录文件。

我正在使用 gdal ReadAsArray 读取数据文件,然后我正在对这个二维数组进行操作,现在我想将这个二维数组保存为一个 ENVI 二进制文件。

请指导如何在 Python 3.5 中执行此操作。

  • 查找教程网站的帮助:https://pcjericks.github.io/py-gdalogr-cookbook/
  • 如例子

    import gdal, ogr, os, osr
    import numpy as np
    
    def array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array):
        cols = array.shape[1]
        rows = array.shape[0]
        originX = rasterOrigin[0]
        originY = rasterOrigin[1]
    
        driver = gdal.GetDriverByName('ENVI')
        outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Byte)
        outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
        outband = outRaster.GetRasterBand(1)
        outband.WriteArray(array)
        outRasterSRS = osr.SpatialReference()
        outRasterSRS.ImportFromEPSG(4326)
        outRaster.SetProjection(outRasterSRS.ExportToWkt())
        outband.FlushCache()
    
    def main(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array):
        reversed_arr = array[::-1] # reverse array so the tif looks like the array
        array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,reversed_arr) # convert array to raster
    
    if __name__ == "__main__":
        rasterOrigin = (-123.25745,45.43013)
        pixelWidth = 10
        pixelHeight = 10
        newRasterfn = 'test.tif'
        array = np.array([[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                          [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                          [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1],
                          [ 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1],
                          [ 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1],
                          [ 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1],
                          [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1],
                          [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                          [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                          [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
    
        main(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array)