'NoneType' object has no attribute 'SetGeoTransform'

hello,
I want to crop netCDF using shapeFile, but ‘NoneType’ object has no attribute ‘SetGeoTransform’ appears. The code is as follows:
def makeMask(lon, lat, res, shapefile):
source_ds = ogr.Open(shapefile)

source_layer = source_ds.GetLayer()

# Create high res raster in memory
mem_ds = gdal.GetDriverByName('MEM').Create('', lon.size, lat.size, gdal.GDT_Byte)
mem_ds.SetGeoTransform((lon.min(), res, 0, lat.max(), 0, -res))

band = mem_ds.GetRasterBand(1)

# Rasterize shapefile to grid
gdal.RasterizeLayer(mem_ds, [1], source_layer, burn_values=[1])

# Get rasterized shapefile as numpy array
array = band.ReadAsArray()

# Flush memory file
mem_ds = None
band = None
return array

Hi @nxdsx ,

NoneType errors like this indicate that there was an error in the last command issued. In this case, something probably went wrong in your driver.Create() call, and there should be some error information printed to standard error so I’d check your terminal.

Since you’re using the python api, you can also try gdal.UseExceptions() and the error should be raised as an exception.

James

1 Like