Search code examples
pythontiffgdalgeotiff

Why geotiff could not be opened by gdal?


I'm newbie in python and in geoprocessing. I'm writing some program to calculate ndwi. To make this, I try to open geotiff dataset with gdal, but dataset can't be opened. I tried to open different tiff files (Landsat8 multiple data, Landsat7 composite, etc), but dataset is always None. What reason to this could be? Or how can i find it out?

Here's a part of code:

import sys, os, struct
import gdal, gdalconst

from gdalconst import *
import numpy as np
from numpy import *

class GDALCalcNDWI ():

  def calcNDWI(self, outFilePath):

    gdal.AllRegister()
    # this allows GDAL to throw Python Exceptions
    gdal.UseExceptions()

    filePath = "C:\\Users\\Daria\\Desktop.TIF\\170028-2007-05-21.tif"

    # Open
    dataset = gdal.Open(filePath, gdal.GA_ReadOnly)
    # Check
    if dataset is None:
      print ("can't open tiff file")
      sys.exit(-1)

Thanks


Solution

  • Whenever you have a well-known file reader that is returning None, make sure the path to your file is correct. I doubt you have a directory called Desktop.TIF, I'm assuming you just made a typo in your source code. You probably want C:\\Users\\Dara\\Desktop\\TIF\\170028-2007-05-21.tif as the path (note that Desktop.TIF ==> Desktop\\TIF).

    The safest thing to do is right click on the file, go to properties, and copy/paste that path into your python source code.