1

I installed the "gdal-201-1800-core.msi" and "GDAL-2.1.2.win32-py3.4.msi" in WinPython. It looks like the gdal could run in python shell.( there is no error when type "import gdal" ). But I could not use any gdal function such as "GetRasterband()","ReadAsArray". what's wrong with the gdal?

import gdal

import ogr

file=gdal.Open(r'D:\GeoNE\temp\m2000049.tif')

band=file.GetRasterBand(1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-a8404d484b8e> in <module>()
----> 1 band=file.GetRasterband(1)

AttributeError: 'NoneType' object has no attribute 'GetRasterBand'
2
  • That just means GDAL couldn't open your tif. Use gdal. UseExceptions() to see the error message.
    – user2856
    Commented Dec 24, 2016 at 0:38
  • @Luke Such is the case! The tif without head file could not be open. Commented Dec 30, 2016 at 1:54

2 Answers 2

1

You are not specifying the access conditions after the name of the dataset. See here. Try to mention if it is GA_ReadOnly or GA_Update, like

filename='D:/GeoNE/temp/m2000049.tif'    
dataset=gdal.Open(filename,gdal.GA_ReadOnly)  # opening file

Also check if your raster is ok in any GIS software, and try to open with gdal any another raster.

1
  • 1
    GA_ReadOnly is the default access if not specified. gdal.Open(filename) is fine, assuming filename can be opened.
    – user2856
    Commented Dec 30, 2016 at 4:19
-3

Try using:

filename = 'D:/GeoNE/temp/m2000049.tif'
file=gdal.Open(filename)

instead of what you have provided.

1
  • There is no more explanation to give because it's clear that the difference between the two codes is in the using of the forward slashes instead of backslashes and I used the verb "try" because I was not sure about the result. Then, I think your downvote was unnecessary.
    – mgri
    Commented Dec 30, 2016 at 8:07

Your Answer

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

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