I am trying to rasterize a layer as follows:

attr = 'N_CT'

tif_driver = gdal.GetDriverByName('MEM')
raster = tif_driver.Create(path, x_res, y_res, num_bands, gdal.GDT_UInt16)

shp_driver = ogr.GetDriverByName('ESRI Shapefile')
shp_mem = shp_driver .Open('x.shp')

# loop through each layer in shapefile, add to raster band(s)
for idx in len(shp_mem.GetLayerCount()):
  lyr = shp_mem.GetLayer(i)
  gdal.RasterizeLayer(raster, [i+1], lyr, options=['ATTRIBUTE=' + attr])

*Do some raster computations*

# create .TIF
raster = createRaster(shp,'GTiff', path)

# configure band
band = raster.GetRasterBand(1)

# write band

raster = None

What happens above is I rasterize a shapefile called 'x.shp' based on a field called N_CT (integers)

However, the output contains what I desire but surrounding it is what I imagine is no data (with value 0 - in black).

I could set my band no_data value to 0. However, is there a way I can change this default (0) to something that I don't need?

enter image description here

  • Try options=['init=somenumber', 'a_nodata=thesamenumber', 'ATTRIBUTE=' + attr]. Haven't added as an answer as I can't test.
    – user2856
    Jan 19, 2020 at 10:26

1 Answer 1


I found my solution. The issue was that I was setting the band.fill(NO_DATA) only when creating the final output.

In order to fix this, I simply had to set fill value at the time of rasterizing the layer

  • 2
    You mean you have put band = raster.GetRasterBand(1) band.Fill(NO_DATA_VALUE) in front of RasterizeLayer? May 20, 2021 at 8:26

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.