作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在利用 python GDAL 将栅格数据写入 .tif 文件。这是代码:
import numpy, sys
from osgeo import gdal, utils
from osgeo.gdalconst import *
# register all of the GDAL drivers
gdal.AllRegister()
# open the image
inDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\color_a1.tif",GDT_UInt16)
if inDs is None:
print "couldn't open input dataset"
sys.exit(1)
else:
print "opening was successful!"
cols = inDs.RasterXSize
rows = inDs.RasterYSize
bands = inDs.RasterCount
driver = inDs.GetDriver()
driver.Create("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif",cols,rows,3,GDT_UInt16)
outDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif")
if outDs is None:
print "failure to create new file"
sys.exit(1)
outBand1 = outDs.GetRasterBand(1)
outBand2 = outDs.GetRasterBand(2)
outBand3 = outDs.GetRasterBand(3)
data1 = inDs.GetRasterBand(1).ReadAsArray()
data2 = inDs.GetRasterBand(2).ReadAsArray()
data3 = inDs.GetRasterBand(3).ReadAsArray()
outBand1.WriteArray(data1,0,0)
outBand2.WriteArray(data2,0,0)
outBand3.WriteArray(data3,0,0)
print "before closing out the file"
print outDs.GetRasterBand(1).ReadAsArray(700,700,5,5)
print outDs.GetRasterBand(2).ReadAsArray(700,700,5,5)
print outDs.GetRasterBand(3).ReadAsArray(700,700,5,5)
outDs.SetProjection(inDs.GetProjection())
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs = None
outDs = gdal.Open("C:\\Documents and Settings\\patrick\\Desktop\\tiff elevation\\EBK1KM\\newfile.tif")
print "after reopening"
print outDs.GetRasterBand(1).ReadAsArray(700,700,5,5)
print outDs.GetRasterBand(2).ReadAsArray(700,700,5,5)
print outDs.GetRasterBand(3).ReadAsArray(700,700,5,5)
关闭和重新打开输出数据集的结果输出是不同的:
before closing out the file
[[ 36 35 55 121 0]
[ 54 0 111 117 0]
[ 0 117 152 56 0]
[ 89 122 56 0 0]
[102 107 0 25 53]]
[[ 68 66 126 200 0]
[ 78 0 166 157 0]
[ 0 235 203 70 0]
[229 251 107 0 0]
[241 203 0 42 121]]
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
after reopening
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
是否缺少一些命令来确保在将变量设置为 None 之前写入并保存文件?我试过添加以下两项但没有成功:
outband1.FlushCache()
outDs.FlushCache()
最佳答案
您不需要创建
然后打开
一个栅格(您正在读取GA_ReadOnly
)。您也不需要在开始时使用 gdal.AllRegister()
,因为当您将 GDAL 加载到 Python 时它已经被调用(参见 Raster API tutorial )。
在上面某处拾取(有修改):
# Create a new raster data source
outDs = driver.Create(out_fname, cols, rows, 3, gdal.GDT_UInt16)
# Write metadata
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())
# Write raster data sets
for i in range(3):
outBand = outDs.GetRasterBand(i + 1)
outBand.WriteArray(data[i])
# Close raster file
outDs = None
有时我添加这个以确保文件被完全释放,并防止遇到一些 gotchas :
del outDs, outBand
关于python - GDAL WriteArray 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6791233/
我正在利用 python GDAL 将栅格数据写入 .tif 文件。这是代码: import numpy, sys from osgeo import gdal, utils from osgeo.g
我有两个类(writeArray 和 readArray),它们应该写入一个 {10, 20, 30} 数组并在演示类中打印它,但它只打印出 10,没有 20 或 30,而且我不打印了解它是否没有读取
我是一名优秀的程序员,十分优秀!