2013-01-25 89 views
0

我有一個數據庫目錄讀取圖像路徑以及其他屬性,並有一個部分,試圖打開代碼中的數據集,以便其他進程可以繼續,如果開放是成功的但我碰到了一個絆腳石,關於如何告訴程序在下面的代碼後繼續執行,代碼運行順利,但是當它遇到圖像時,它不能打開它停止,而不是開始再次讀取數據庫並打開一個新的形象。python和gdal圖像處理

try: 
    hDataset = gdal.Open(pszFilename, gdal.GA_ReadOnly) 
    except IOError: 
    print("gdalinfo failed - unable to open '%s'." % pszFilename) 
    status = "UPDATE %s SET job = 11 WHERE id = %s" % (table,row[2]) 
    setstatus = conn.cursor() 
    setstatus.execute(status) 
    conn.commit() 
    setstatus.close() 
else: 
    print "file opened sucessfully" 
    hDataset.close() 
+1

嘗試確定哪一行引起的掛起(少數診斷打印語句可能會告訴你)。請注意,並非所有'gdal.Open'失敗都會生成一個'IOError'異常(例如,如果該文件不存在,則不會拋出異常),因此您可能還需要捕獲一般異常。 – bogatron

回答

0

GDAL通常不會拋出異常,這是一種遺憾。隨着gdal.UseExceptions()開啓,它有時會拋出RuntimeError(僅!),但我還沒有發現這個功能非常可靠。

GDAL的一些函數返回None,如果不成功,其他函數返回一個狀態整數,其中0是好的,非0是錯誤代碼。

我用一個典型的形式是這樣的:

hDataset = gdal.Open(pszFilename, gdal.GA_ReadOnly) 
if hDataset is None: 
    raise IOError("Could not open '%s'" % (pszFilename,)) 

band_num = 1 
band = hDataset.GetRasterBand(band_num) 
if band is None: 
    raise AttributeError("Raster band %s cannot be fetched" % (band_num,)) 
...