Is there a way to rasterize each polygon instance with its own burn value? So what I am trying to do is create a mask of the polygons in mask.shp using the projection and geotransform information from raster.tif. Each polygon mask needs to have a unique value. My current approach burns all of them as 255. Here is my code:
vector_layer = "mask.shp"
raster_layer = "raster.tif"
target_layer = "mask.tif"
# open the raster layer and get its relevant properties
raster_ds = gdal.Open(raster_layer, gdal.GA_ReadOnly)
xSize = raster_ds.RasterXSize
ySize = raster_ds.RasterYSize
geotransform = raster_ds.GetGeoTransform()
projection = raster_ds.GetProjection()
# create the target layer (1 band)
driver = gdal.GetDriverByName('GTiff')
target_ds = driver.Create(target_layer, xSize, ySize, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform(geotransform)
target_ds.SetProjection(projection)
source_ds = ogr.Open(vector_layer)
source_layer = source_ds.GetLayer()
ds = gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[255])
target_ds = 0