0

In python, I'm using the following line of code:

gdal.RasterizeLayer(outDs, [1], Shp, burn_values=[ii])

My shapefile 'Shp' has a field named 'landuse', which stores strings (class name). I need to burn a number for different class name. I was thinking to use a "where" clause, like: where 'landuse'='new concession':

for ii in range(class)):    
    gdal.RasterizeLayer(outDs, [1], Shp, burn_values=[ii], where='"landuse"="class[ii]"')

Parameter "where" does not seem to be recognized. How can I pass it to this function? I suppose it is available, according to the C API doc: http://gdal.org/python/osgeo.gdal-module.html#RasterizeOptions

Bruno von Paris
  • 882
  • 1
  • 7
  • 26

2 Answers2

1

RasterizeLayer is a bit of a pain to perform 'quickly', so this is without testing.

I spot two potential issues. Based on your example code, you don't actually set the desired landuse value, but pass the "class[ii]" as a string. That would mean GDAL selects the features where the landuse attribtue is "class[ii]" (literally that string). Use string formatting to insert the actual value, so something like:

for ii in range(class)):    
    gdal.RasterizeLayer(outDs, [1], Shp, burn_values=[ii], where='"landuse"="{class}"'.format(class=class[ii]))

Secondly, when using RasterizeLayer, you probably should provide a layer as the input, not a Shapefile like you seem to suggest. Either load the layer from your Shapefile, or consider using Rasterize.

gdal.Rasterize(outds, dataset)
gdal.RasterizeLayer(outds, layer)
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
0

I'm not sure about the RasterizeOptions and where not working (invalid keyword), but it seems like you can add the where-clause to the Shp with SetAttributeFilter:

for ii in range(class)):
    Shp.SetAttributeFilter("landuse='{}'".format(class[ii]))
    gdal.RasterizeLayer(outDs, [1], Shp, burn_values=[ii])
Heiko
  • 94
  • 1
  • 3