2011-07-22 64 views
3

我正在利用python GDAL將一個柵格數據寫入.tif文件。下面的代碼:GDAL WriteArray問題

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

# register all of the GDAL drivers 
gdal.AllRegister() 

# open the image 
inDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\color_a1.tif",GDT_UInt16) 
if inDs is None: 
    print "couldn't open input dataset" 
    sys.exit(1) 
else: 
    print "opening was successful!" 
cols = inDs.RasterXSize 
rows = inDs.RasterYSize 
bands = inDs.RasterCount 
driver = inDs.GetDriver() 
driver.Create("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif",cols,rows,3,GDT_UInt16) 
outDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif") 

if outDs is None: 
    print "failure to create new file" 
    sys.exit(1) 


outBand1 = outDs.GetRasterBand(1) 
outBand2 = outDs.GetRasterBand(2) 
outBand3 = outDs.GetRasterBand(3) 
data1 = inDs.GetRasterBand(1).ReadAsArray() 
data2 = inDs.GetRasterBand(2).ReadAsArray() 
data3 = inDs.GetRasterBand(3).ReadAsArray() 

outBand1.WriteArray(data1,0,0) 
outBand2.WriteArray(data2,0,0) 
outBand3.WriteArray(data3,0,0) 

print "before closing out the file" 
print outDs.GetRasterBand(1).ReadAsArray(700,700,5,5) 
print outDs.GetRasterBand(2).ReadAsArray(700,700,5,5) 
print outDs.GetRasterBand(3).ReadAsArray(700,700,5,5) 

outDs.SetProjection(inDs.GetProjection()) 
outDs.SetGeoTransform(inDs.GetGeoTransform()) 

outDs = None 
outDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif") 
print "after reopening" 
print outDs.GetRasterBand(1).ReadAsArray(700,700,5,5) 
print outDs.GetRasterBand(2).ReadAsArray(700,700,5,5) 
print outDs.GetRasterBand(3).ReadAsArray(700,700,5,5) 

輸出數據集的關閉和重新打開之間得到的輸出是不同的:

before closing out the file 
[[ 36 35 55 121 0] 
[ 54 0 111 117 0] 
[ 0 117 152 56 0] 
[ 89 122 56 0 0] 
[102 107 0 25 53]] 
[[ 68 66 126 200 0] 
[ 78 0 166 157 0] 
[ 0 235 203 70 0] 
[229 251 107 0 0] 
[241 203 0 42 121]] 
[[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0]] 
after reopening 
[[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0]] 
[[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0]] 
[[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0] 
[0 0 0 0 0]] 

有一些命令我失蹤,以確保文件被寫入並保存在將變量設置爲None之前?我試着將沒有運氣以下兩種情況:

outband1.FlushCache() 
outDs.FlushCache() 
+0

我最初發布的回答然後我意識到我誤解了你在做什麼。所以問題:你是否嘗試過將'print'語句移動到'outDs.SetProjection(inDs.GetProjection())'和'outDs.SetGeoTransform(inDs.GetGeoTransform())'之後以確保這不是問題?如果你在'outDs = None'之後結束腳本,那麼看看這個文件,它是空的嗎?確認問題在寫,而不是如何讀回來。 – agf

+0

我想出了這個問題:爲了與新創建的文件一起工作並存儲信息,我需要將驅動程序.Create()分配給一個變量:即「outFile = driver.Create(」C:\\ Documents and Settings \\ patrick \\ Desktop \\ tiff elevation \\ EBK1KM \\ newfile.tif「,cols,rows,3,GDT_UInt16)」之後,我使用outFile的變量和數據成員來獲得所需的結果。謝謝你的幫助! – Pat

回答

12

你不需要Create然後Open光柵(你在讀GA_ReadOnly)。您在開始時也不需要gdal.AllRegister(),因爲在將GDAL加載到Python時已經調用它(請參閱GDAL API Tutorial)。

拿起某處以上(含修改):

# 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 

有時我添加此以確保文件被完全釋放,並防止遇到了一些gotchas

del outDs, outBand 
+0

這是你給我的第一行 - 設置Create()等於一個變量解決了所有事情,而且我只需要在那之後使用該變量。謝謝! – Pat

+0

很高興工作。不要忘了[爲這些步驟](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)回答你的問題 –

+0

我想@Pat忘了做那些腳步。 : - / – Richard