Here is some code which should give you an idea of how to do it.
For gdal.Rasterize only the first two parameters are required. The rest of the parameters are **kwargs
from RasterizeOptions .
The **kwargs
will be user/project specific.
import gdal
ulx, xsize, rotx, uly, roty, ysize = geotransform # from gdal's GetGeoTransform()
XSize = 100 # Raster size in pixels (columns)
YSize = 100 # Raster size in pixels (rows)
lrx = ulx + (XSize*abs(xsize)) # Calculate the lower right x coordinate
lry = uly - (YSize*abs(ysize)) # Calculate the lower right y coordinate
outMemRas = '/vsimem/raster_name.tif'
shpFilePath = 'shapefile_path.shp'
gdal.Rasterize(outMemRas, shpFilePath,
outputType=gdal.GDT_Byte,
outputSRS='EPSG:32632',
width=XSize, height=YSize,
outputBounds=[ulx, lry, lrx, uly],
attribute='column_name',
allTouched=True)
shapeRasDS = gdal.Open(outMemRas) # Open raster file in memory with gdal
shape_arr = shapeRasDS.ReadAsArray() # Read into numpy array
gdal.Unlink(outMemRas) # remove file from memory