GDAL WriteArray issue

前端 未结 1 458
失恋的感觉
失恋的感觉 2021-02-15 01:06

I\'m utilizing python GDAL to write a raster data into a .tif file. Here\'s the code:

import numpy, sys
from osgeo import gdal, utils
from osgeo.gdalconst impor         


        
1条回答
  •  感情败类
    2021-02-15 01:19

    You don't need to Create then Open a raster (which you were reading GA_ReadOnly). You also don't need gdal.AllRegister() at the beginning, as it has already been called when you load GDAL into Python (see the Raster API tutorial).

    Picking up somewhere above (with modifications):

    # Create a new raster data source
    outDs = driver.Create(out_fname, cols, rows, 3, gdal.GDT_UInt16)
    
    # Write metadata
    outDs.SetGeoTransform(inDs.GetGeoTransform())
    outDs.SetProjection(inDs.GetProjection())
    
    # Write raster data sets
    for i in range(3):
        outBand = outDs.GetRasterBand(i + 1)
        outBand.WriteArray(data[i])
    
    # Close raster file
    outDs = None
    

    Sometimes I add this to ensure the file is fully deallocated, and to prevent running into some gotchas:

    del outDs, outBand
    

    0 讨论(0)
提交回复
热议问题