I want to divide each band of the hyperspectral data by a factor and then save it into a new raster. The function driver.Create() is showing an error and outDS raster is giving Nonetype value. outRefR is the numpy array of result after applying the factor.
Here is the code block,
from osgeo import gdal
import numpy as np
img_more_bands = gdal.Open("/content/drive/file1")
data = img_more_bands.ReadAsArray()
ang=gdal.Open(r"/content/drive/MyDrive/file2")
angle=ang.ReadAsArray()
outRefR=data/np.cos(angle)
driver=gdal.GetDriverByName('ENVI')
outFN = '/content/drive/MyDrive/output.tif' # Specify a file name with the appropriate extension
outDS = driver.Create(outFN, outRefR.shape[2], outRefR.shape[1], outRefR.shape[0], gdal.GDT_Float32)
for i in range(outRefR.shape[0]):
bnd = outDS.GetRasterBand(i + 1)
bnd.WriteArray(outRefR[i, :, :])
outDS = None
And here is the error:
AttributeError Traceback (most recent call last)
<ipython-input-76-aa7e987b6715> in <cell line: 5>()
4
5 for i in range(outRefR.shape[0]):
----> 6 bnd = outDS.GetRasterBand(i + 1)
7 bnd.WriteArray(outRefR[i, :, :])
8
AttributeError: 'NoneType' object has no attribute 'GetRasterBand'
Ananya SrivastavaAnanya Srivastava
asked Nov 6, 2023 at 21:41
lang-py
outDS = driver.Create(outFN, outRefR.shape[2], outRefR.shape[1], outRefR.shape[0], gdal.GDT_Float32)
has failed and therefore outDS is of type NoneType. In this answer the number of bands is also defined gis.stackexchange.com/a/348515/30184.