I'm attempting to rasterize a layer of a raster by using the different features inside a shapefile(In this case the different features are lines).

The problem I am running into is that RasterizeLayer does not like it when I pass it a feature rather than the layer. My code is below:

line_ds = ogr.Open(r"Lines.shp")

for feat in lyr:
                        target_ds,  # raster file
                        [1],  #My raster band
                        feat, #The feature in the shapeful
                        burn_values=[0] # my burn values

This is a fairly standard piece of code that I have used before, but only when using whole shapefiles rather than the individual features.

Also I am trying to invert the rasterization, but RasterizeLayer doesn't seem to have an option for this, I have tired adding in:




does anyone know if there is a way to make it work?

1 Answer 1


you may have to create a layer in memory to pass to rasterizLayer, something like -

# Create a temporary vector layer in memory
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_layer = mem_ds.CreateLayer('poly', srs=srcproj, geom_type=ogr.wkbPolygon)
mem_poly = ogr.Feature(mem_layer.GetLayerDefn())

then pass mem_layer into the rasterizeLayer. example line328

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.