2012-11-22 27 views
4

我從ruby1.9使用GDAL 1.7.1生成GeoTIFF文件。在tutorial中,他們建議使用GDALClose()來關閉數據集並將剩餘內容清除到文件系統。在數據集的析構函數中也是如此。問題在於ruby綁定依賴於這個析構函數來關閉數據集,並且我需要生成該文件的進程中已有文件的結果。由於紅寶石是垃圾收集,似乎我不能可靠地關閉我的文件,而不退出紅寶石進程。現在我修補了我的GDAL版本來支持GDALClose方法,但這似乎不是一個好的長期解決方案。如何在GDAL ruby​​綁定中顯式關閉數據集?

require 'gdal/gdal' 

[...] 

# open the driver for geotiff format 
driver = Gdal::Gdal.get_driver_by_name('GTiff') 
# create a new file 
target_map = driver.create(output_path, 
       xsize, 
       ysize, 3, 
       Gdal::Gdalconst::GDT_UINT16, ["PHOTOMETRIC=RGB"]) 

# write band data 
3.times do |i| 
    band = target_map.band(i + 1) 
    target_map.write_band(i + 1, mapped_data) 
end 

# now I would like to use the file in output_path, but at this point 
# large parts of the data still resides in memory it seems until 
# target_map is destroyed 

file = File.open(output_path, "r") 

[...] 

在ruby或swig中是否有強制析構函數調用的東西,我可能忽略了?

+0

顯示代碼的一個小示例,演示該問題。你要求我們想象你寫的內容,我們沒有辦法做到這一點。 –

回答

1

正常情況下,Python中的GDAL綁定是將對象設置爲None。因此在Ruby中,這將是nil

band = nil 
target_map = nil 

這是保存/平/關閉數據一個有趣的方式,但它是如何做。

+4

這對Ruby不起作用。 Python是引用計數,因此只要所有引用都未設置,對象就會被刪除。 Ruby被垃圾收集,因此將變量設置爲零是不夠的。有人可能會認爲這樣做,並調用GC.start會工作,但這是不可靠的。 Ruby GC掃描C堆棧以查找對象引用,並且很可能GDAL對象仍然在那裏。 –