在python中使用gdal将栅格导出到tif时重新加载tif时出错

时间:2016-07-12 17:45:14

标签: python-2.7 tiff raster gdal

我想通过以下步骤导出我处理过的栅格图层np数组为tif,在用驱动程序创建out.tif栅格之后我无法将创建的tif文件加载回python。我试图将'gdal.GDT_Int32'更改为其他类型,但它不起作用。我也尝试使用而不是'inds.GetDriver''GetDriverByName('GTiff'),但也没有做任何更改,消息如下:

In [13]: driver.Create('out.tif', cols, rows, 1, gdal.GDT_Int32)
Out[13]: <osgeo.gdal.Dataset; proxy of <Swig Object of type 
'GDALDatasetShadow *
' at 0x000000000F437360> >

In [18]: outds=gdal.Open('out.tif')
-----------------------------------------------------------------------
RuntimeError                            Traceback (most recent call last)
<ipython-input-18-82ed958ff66e> in <module>()
----> 1 outds = gdal.Open('out.tif')

RuntimeError: `out.tif' not recognised as a supported file format.

In [14]: outds = gdal.Open('out.tif')
ERROR 4: `out.tif' not recognised as a supported file format.

我的代码:

#load inds for processing
inds = gdal.Open('in.tif', gdal.GA_ReadOnly)

cols = inds.RasterXSize
rows = inds.RasterYSize
driver = inds.GetDriver()


driver.Create('out.tif', cols, rows, 1, gdal.GDT_Int32)
**#The step with errors!**
**outds = gdal.Open('out.tif')**
outband = outds.GetRasterBand()
data = inds.GetRasterBand().ReadAsArray()

outds.SetGeoTransform(inds.GetGeoTransform())
outds.SetProjection(inds.GetProjection())

outband = outds.GetRasterBand()
outband.WriteArray(data)

1 个答案:

答案 0 :(得分:0)

使用本讨论中使用的相同方法解决了这个问题: python-GDAL Write Array-issue

通过这种方式修改代码:

outds = driver.Create('day.tif', cols, rows, 1, gdal.GDT_Int32)

而不是在预览文章中:

driver.Create('out.tif', cols, rows, 1, gdal.GDT_Int32)
outds = gdal.Open('out.tif')

错误已解决。