How to explicitly close datasets in GDAL ruby binding?

我是研究僧i 提交于 2020-01-03 15:57:09

问题


I am using GDAL 1.7.1 from ruby1.9 to generate GeoTIFF files. In the tutorial they recommend to use GDALClose() to close the datasets and flush any remaining content to the filesystem. The same happens in the destructor for the dataset. The problem is that the ruby bindings rely on this destructor mechanism to close the dataset, and I need the result of the file already in the process that generates it. Since ruby is garbage collected, it seems I can not reliably close my files, without exiting the ruby process. For now I patched my version of GDAL to support the GDALClose method, but this doesn't seem to be a good long term solution.

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" )

[...]

Is there something in either ruby or swig to force the destructor call, that I may have overlooked?


回答1:


Normally what is done with the GDAL bindings in Python is to set the objects to None. So in Ruby, this would be nil:

band = nil
target_map = nil

It's a funny way to save/flush/close the data, but it is how it is done.



来源:https://stackoverflow.com/questions/13509405/how-to-explicitly-close-datasets-in-gdal-ruby-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!