1
我試圖將一個numpy數組輸出保存爲GeoTiff,並且代碼運行大部分成功,但輸出圖像的數據具有棕褐色調的比例(而不是像正常的顏色方案我的代碼爲圖像生成)和黑色背景(而不是我的代碼爲圖像生成的白色/無數據背景)。保存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
這可能是有益的,如果你可以添加正確的圖像和錯誤配色方案的圖像。這樣即使沒有運行你的代碼的用戶也許能夠識別出什麼是錯誤的。 – DrV