如何在python中将numpy列表转换为geoTIFF?

时间:2018-03-19 01:33:07

标签: python numpy geotiff

我还没有找到这个简单问题的答案。请帮忙。如何将Qcal(numpy list)转换为TIFF图像?我发现的一切都不起作用。

import math
import numpy as np
from osgeo import gdal

substr1 = 'RADIANCE_MULT_BAND_10'
substr2 = 'RADIANCE_ADD_BAND_10'
substr3 = 'K1_CONSTANT_BAND_10'
substr4 = 'K2_CONSTANT_BAND_10'

RADIANCE_MULT_BAND_10 = 1
RADIANCE_ADD_BAND_10 = 1
K1_CONSTANT_BAND_10 = 1
K2_CONSTANT_BAND_10 = 1

with open('LC08_L1TP_180028_20170623_20170630_01_T1_MTL.txt') as file:
    for line in file:
        if substr1 in line:
            startIndex = line.find('=')
            RADIANCE_MULT_BAND_10 = float((line[startIndex+2:]))
        if substr2 in line:
            startIndex = line.find('=')
            RADIANCE_ADD_BAND_10 = float((line[startIndex + 2:]))
        if substr3 in line:
            startIndex = line.find('=')
            K1_CONSTANT_BAND_10 = float((line[startIndex + 2:]))
        if substr4 in line:
            startIndex = line.find('=')
            K2_CONSTANT_BAND_10 = float((line[startIndex + 2:]))

ds = gdal.Open("B10.tif")
Qcal = np.array(ds.GetRasterBand(1).ReadAsArray()) # Quantized and calibrated standard product pixel values (DN)
for i in range(Qcal.shape[0]):
   for j in range(Qcal.shape[1]):
       Qcal[i][j] = RADIANCE_MULT_BAND_10 * Qcal[i][j] + RADIANCE_ADD_BAND_10
       Qcal[i][j] = K2_CONSTANT_BAND_10 / math.log1p(K1_CONSTANT_BAND_10/Qcal+1)

1 个答案:

答案 0 :(得分:0)

是否要修改现有图片?

如果是这样,你应该使用像这样的更新选项打开它

gdal.Open("B10.tif", gdal.GA_Update)

接下来就像你一样对你的np数组进行修改。你实际上只是编辑numpy数组Qcal而不是tif中的实际栅格带。

现在要将修改保存到同一个频段,您可以执行以下操作

ds.GetRasterBand(1).WriteArray(Qcal)

这是将更新的Qcal数组写入tif的栅格带。

如果您想保存为新图片

您可以创建现有图像的副本,并将Qcal数组保存到其中,如此

driver = gdal.GetDriverByName('Gtiff')
dst_ds = driver.CreateCopy("example.tif", ds, 1)
dst_ds.GetRasterBand(1).WriteArray(Qcal)