【问题标题】:Saving GeoTiff with color scheme使用配色方案保存 GeoTiff
【发布时间】:2014-06-26 16:10:51
【问题描述】:

我正在尝试将 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

【问题讨论】:

  • 如果您可以添加正确的图像和具有错误配色方案的图像,这可能会有所帮助。这样,即使没有可能运行您的代码的用户也可能能够识别出问题所在。

标签: arrays numpy geotiff


【解决方案1】:

可以使用band的SetNoDataValue方法设置无数据值:

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

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

【讨论】:

  • 谢谢! SetNoDataValue 似乎可以解决问题(我之前已经将它放在那里,但肯定没有设置正确的东西,因为它不起作用。现在它是!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 2015-11-05
  • 2013-04-19
  • 1970-01-01
  • 2020-03-02
  • 2010-12-28
  • 2012-02-12
相关资源
最近更新 更多