0

I created a geotiff from a netCDF file through the following code, but when I try to access the resulting file in python again, this gives me the following error message: 'NoneType' object has no attribute 'GetRasterBand' and I do not understand the reason for such! However, the file opens correctly in other software, such as QGIS. Can you help me?

ncdf=netCDF4.Dataset(file,'r')

lat=ncdf.variables["LAT"][:]
lon=ncdf.variables["LON"][:]
var =ncdf.variables["VAR"][:]

(code to convert LAT and LON in X and Y UTM coordinates)

 X=...
 Y=...

 nx=len(X)
 a=numpy.shape(var)
 ny=a[1]

 xres=30
 yres=30

xmin, ymin, xmax, ymax = [X.min(), Y.min(), X.max(), Y.max()]

geotransform = (xmin, xres, 0, ymax, 0, -yres)

 Wkt="""PROJCS["WGS 84 / UTM zone 29N",
            GEOGCS["WGS 84",DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
            PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
            UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],
            AUTHORITY["EPSG","4326"]],
            UNIT["metre",1,
            AUTHORITY["EPSG","9001"]],
            PROJECTION["Transverse_Mercator"],
            PARAMETER["latitude_of_origin",0],
            PARAMETER["central_meridian",-9],
            PARAMETER["scale_factor",0.9996],
            PARAMETER["false_easting",500000],
            PARAMETER["false_northing",0],
            AUTHORITY["EPSG","32629"],
            AXIS["Easting",EAST],
            AXIS["Northing",NORTH]]"""


  dst_ds = gdal.GetDriverByName('GTiff').Create('./B1_CATM.tif', ny, nx, 1, gdal.GDT_Float32)
  dst_ds.GetRasterBand(1).WriteArray(refAerosol) 

  dst_ds.SetGeoTransform(geotransform) 

  srs = osr.SpatialReference()            
  srs.ImportFromWkt(Wkt)
  srs.ImportFromEPSG(32629)

  dst_ds.SetProjection(srs.ExportToWkt())


  # AND IN OTHER SCRIPT OPEN THIS GEOTIFF File:

  B=gdal.Open('directoryfile.tif')
  Band=B.GetRasterBand(1)

 # the following error message appears: 'NoneType' object has no attribute 
 'GetRasterBand'

2 Answers 2

2

You'll get more information about why GDAL can't open your file if you put import gdal; gdal.UseExceptions() at the top. If you don't do this, gdal.GetDriverByName('GTiff').Create() will return None with no explanation.

0
1

'NoneType' object has no attribute 'GetRasterBand' is Python's way of saying it all went wrong. The problem is that the first line returns None (or null to the rest of us non-python types), and then you tried to use that value in the second line.

dst_ds = gdal.GetDriverByName('GTiff').Create('./B1_CATM.tif', ny, nx, 1, gdal.GDT_Float32)
dst_ds.GetRasterBand(1).WriteArray(refAerosol) 

So you need to work out why GDAL is not reading in your tif file. It might be the name or location is wrong, or the type is wrong or that nx & ny are wrong.

In general, it is good practice to check the datastore returned by GDAL to make sure nothing went wrong before you attempt to use it.

1
  • I found this answer useful because it was indeed my location
    – Na_Na_Na
    Commented Feb 22, 2022 at 22:10

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.