0

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'
asked Nov 6, 2023 at 21:41
3
  • 2
    I suppose that 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. Commented Nov 6, 2023 at 23:27
  • @user30184 I have given the number of bands from the shape of the original dataset as outRefR.shape[0] Commented Nov 7, 2023 at 15:06
  • @SonofaBeach I have included the complete code here. I have defined the driver in the code Commented Nov 7, 2023 at 15:07

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.