【问题标题】:Python-gdal write a GeoTiff with binary color and NaNPython-gdal 用二进制颜色和 NaN 编写 GeoTiff
【发布时间】:2018-02-06 04:54:48
【问题描述】:

我正在从一个具有 3 个值的 numpy 数组生成一个简单的两类(二进制)geotiff:1、2 和 NaN。我想用不同的颜色显示 geotiff,所以我使用了颜色表。但是,颜色表只支持 Byte 或 UInt16 数据类型,这会将 NaN 转换为零。

有没有办法编写一个二进制 geotiff,包括带有颜色的 NaN?谢谢。

filename = 'test.tif'
ds = gdal.Open(filename, gdal.GA_ReadOnly)
outfile = 'classify.tif'
driver = gdal.GetDriverByName("GTiff")
outdata = driver.Create(outfile, cols, rows, 1, gdal.GDT_Byte)     
outdata.SetGeoTransform(ds.GetGeoTransform())
outdata.SetProjection(ds.GetProjection())
outband = outdata.GetRasterBand(1)
ct = gdal.ColorTable()
ct.SetColorEntry(1, (0,0,102,255))      
ct.SetColorEntry(2, (0,255,255,255))    
outband.SetColorTable(ct)
outband.WriteArray(result)
outband.FlushCache() 
outdata = None
outband = None
ds = None

【问题讨论】:

    标签: python gdal geotiff


    【解决方案1】:

    Byte 仅接受 8 位值,因此不支持 NaN。一种解决方案是将 NaN 转换为 8 位值(如 255)并添加这行代码(outband.WriteArray(result) 之前):

    outband.SetNoDataValue(255)
    

    您创建的数组中的所有 255 值现在都将被视为 nodata。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 2014-01-24
      • 2020-10-23
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2017-06-05
      相关资源
      最近更新 更多