如何在GDAL红宝石绑定中显式关闭数据集? [英] How to explicitly close datasets in GDAL ruby binding?

查看:99
本文介绍了如何在GDAL红宝石绑定中显式关闭数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ruby1.9中的 GDAL 1.7.1生成GeoTIFF文件.他们在教程中建议使用GDALClose()关闭数据集并将所有剩余内容刷新到文件系统.数据集的析构函数中也会发生同样的情况.问题在于,ruby绑定依赖此析构函数机制来关闭数据集,并且我需要文件的结果已经在生成它的过程中.由于红宝石是垃圾收集的,因此似乎无法可靠地关闭我的文件,而又不退出红宝石进程.现在,我修补了GDAL版本以支持GDALClose方法,但这似乎不是一个好的长期解决方案.

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

[...]

红宝石或or饮中是否存在某些东西可能会迫使我摧毁了析构函数?

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

推荐答案

通常,Python中GDAL绑定的作用是将对象设置为None.因此在Ruby中,这将是nil:

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.

这篇关于如何在GDAL红宝石绑定中显式关闭数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆