1

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")
lyr=line_ds.GetLayer()    

for feat in lyr:
    gdal.RasterizeLayer(
                        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:

options=["INVERSE=TRUE"] 

and

inverse=True

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

2

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())
mem_poly.SetGeometryDirectly(feat.GetGeometryRef())
mem_layer.CreateFeature(mem_poly)

then pass mem_layer into the rasterizeLayer. example line328

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.