0

I'm working on getting the NDVI value of Landsat imagery 2018 using band 4 and 5 but I have encountered a slight problem.

import osgeo
from osgeo import gdal#gdal, data conversion tool
from matplotlib import pyplot as plt
import sys
import numpy as np#for reading numbers in python
b8a= gdal.Open('C:\\Users\\zack\\GIS PROGRAMMINGWORKS\\LC08_L1TP_168061_20171228_20180103_01_T1_B5.tiff')             
b4= gdal.Open('C:\\Users\zack\\GIS PROGRAMMINGWORKS\\LC08_L1TP_168061_20171228_20180103_01_T1_B4.tiff')
d8a = b8a.GetRasterBand(1)
d4 = b4.GetRasterBand(1)

Error returned:

AttributeError                            Traceback (most recent call last)
<ipython-input-22-cb1cfc43017d> in <module>
 ----> 1 d8a = b8a.GetRasterBand(1)
  2 d4 = b4.GetRasterBand(1)
AttributeError: 'NoneType' object has no attribute 'GetRasterBand'
2

GDAL couldn't open your file and returned None instead of raising an exception. And Nonedoesn't have a GetRasterBand attribute (method or property).

This is a common "gotcha" - Python bindings do not raise exceptions unless you explicitly call UseExceptions().

So call gdal.UseExceptions() before you try to open the tiff and make sure the path is correct (should the file extension be .tif?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.