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

问题描述 投票:0回答:2

我想通过以下步骤将处理后的栅格图层(即 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)
python-2.7 tiff raster gdal
2个回答
0
投票

这个问题的解决方法与本次讨论中使用的方法相同: python-GDAL 写入数组问题

通过这样修改代码:

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

错误已解决。


0
投票

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

在[13]中:driver.Create('out.tif', cols, rows, 1, gdal.GDT_Int32) 输出[13]: >

在[18]中:outds=gdal.Open('out.tif')

运行时错误回溯(最近一次调用最后一次) 在 () ----> 1 outds = gdal.Open('out.tif')

运行时错误:“out.tif”未被识别为受支持的文件格式。 或者... 所以。我是 darrals phome 上的 Lindsey。地铁电话 那部 iPhone 在哪里? jmeg?

© www.soinside.com 2019 - 2024. All rights reserved.