1

我正在尝试将 numpy 数组输出保存为 GeoTiff,并让代码大部分成功运行,但输出图像具有棕褐色调的数据比例(而不是像我的代码为图像生成的正常配色方案),和黑色背景(而不是我的代码为图像生成的白色/无数据背景)。

这是将我的数组保存到 GeoTiff 的代码。我可以在某处添加一条关于使 no-data = 0 并使数据方案着色的行吗?

from osgeo import gdal, osr, ogr, os 
from gdalconst import *  

def array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array):

    cols = array.shape[1]
    rows = array.shape[0]
    originX = rasterOrigin[0]
    originY = rasterOrigin[1]

    driver = gdal.GetDriverByName( 'GTiff' )
    outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32)
    outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
    outband = outRaster.GetRasterBand(1)
    outband.WriteArray(array)
    # outRaster = driver.Create( 'CORDC_GTIFF/working_CA.tiff', 300, 300, 1,     gdal.GDT_Int32)
    proj = osr.SpatialReference()  
    proj.ImportFromEPSG(4326) 
    outRaster.SetProjection(proj.ExportToWkt())  
    # geotransform = (1,0.1,0,40,0,0.1)  


rasterOrigin = (-127,42)
pixelWidth = .01
pixelHeight = .01
newRasterfn = 'CORDC_GTIFF/cordc_working_CA.tif'
array = np.array(spd)


reversed_arr = array[::-1] # reverse array so the tif looks like the array
array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,reversed_arr) # convert array to raster
4

1 回答 1

1

您可以使用波段的SetNoDataValue方法设置无数据值:

outband = outRaster.GetRasterBand(1)
outband.SetNoDataValue(0)
outband.WriteArray(array)

匹配无数据值的区域应显示为透明

于 2014-06-27T09:58:34.357 回答