4

Is it possible to set the data type (Byte, Float32) when using gdal.Rasterize in gdal 2.1? Currently, I use gdal.Translate to convert to Byte, but this is inefficient.

tif = my/target.tif
shp = my/source.shp
tiftemp = my/temp/solution.tif
rasterizeOptions = gdal.RasterizeOptions(xRes=20, yRes=20, allTouched=True etc.)
gdal.Rasterize(tiftemp, shp, options=rasterizeOptions)
#translate to Byte data type (not supported by Rasterize?)
gdal.Translate(tif, tiftemp, outputType=gdal.GDT_Byte,
                       creationOptions=['COMPRESS=PACKBITS')

I am aware that it is possible to use

subprocess.check_call('gdal_rasterize', '-ot', 'byte' ...)

but I would prefer to avoid this if possible. Any ideas?

Johan
  • 406
  • 6
  • 20
  • 1
    As long as `tifftemp` - the dataset into which you are rasterizing, is set to the correct type, you should be fine. – Benjamin Jul 19 '16 at 17:12
  • gdal.RasterizeOptions(outputType=gdal.GDT_Byte) raises TypeError: RasterizeOptions() got an unexpected keyword argument 'outputType' – Johan Jul 22 '16 at 07:31

2 Answers2

3

This has been fixed in GDAL 2.1.3. Previously the data type was hard-coded and not exposed in the Python bindings. See https://trac.osgeo.org/gdal/ticket/6710.

Benjamin
  • 11,560
  • 13
  • 70
  • 119
3

If you are in gdal 2.1.2, use gdal.RasterizeOptions() as if you were using it on the command line:

opts_str = '-ot Byte'
rast_opts = gdal.RasterizeOptions(options=opts_str)
Johan
  • 406
  • 6
  • 20