Instead, I think that you need to specify all the properties in the rasterize command in order to obtain a correct result.
As example, if need to rasterize the sample_shape.shp
shapefile and you want to use the sample_raster.tif
raster as a reference for the new output, you may use its properties to create your output (called output_raster
):
from osgeo import gdal, ogr
# Filename of input OGR file
sample_vector = 'sample_shape.shp'
# Filename of the raster Tiff that will be created
sample_raster = 'sample_raster.tif'
# Filename of the raster Tiff that will be created
output_raster = 'output_raster.tif'
# Define pixel_size and NoData value of new raster
pixelSizeX = sample_raster.rasterUnitsPerPixelX()
pixelSizeY = sample_raster.rasterUnitsPerPixelY()
NoData_value = -9999
# Open the data source and read in the extent
source_ds = ogr.Open(sample_vector)
source_layer = source_ds.GetLayer()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
# Create the destination data source
x_res = int((x_max - x_min) / pixelSizeX)
y_res = int((y_max - y_min) / pixelSizeY)
target_ds = gdal.GetDriverByName('GTiff').Create(output_raster, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixelSizeX, 0, y_max, 0, -pixelSizeY))
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)
# Rasterize
gdal.RasterizeLayer(output_raster, [1], source_layer, burn_values=[0])
(This code is adapted from Convert an OGR File to a Raster).