I am trying to rasterize a polygon layer currently as a feature class in a file geodatabase using OGR/GDAL (Python script). So far I have managed to do the rasterization with the output tif file having the right projection. However, I don't seem to be able to set the default value (any value outside polygons) to any value I want (it's always 0) and also to compress the output file (the output file produced has 1GB, whereas the rasterized file produced, for instance, by ArcGIS Pro has 16KB).
This is the code I am trying with:
import os from osgeo import gdal from osgeo import ogr from osgeo import osr from osgeo import gdalconst fc_path = r'E:\...\Processing\INPUT\DefendedAreas.gdb' tif_path = r'E:\...\Processing\INPUT\template.tif' ras = gdal.Open(tif_path, gdalconst.GA_ReadOnly) geo_transform = ras.GetGeoTransform() x_min = geo_transform y_max = geo_transform x_max = x_min + geo_transform * ras.RasterXSize y_min = y_max + geo_transform * ras.RasterYSize x_res = ras.RasterXSize y_res = ras.RasterYSize pixel_width = geo_transform driver = ogr.GetDriverByName("OpenFileGDB") gdb = driver.Open(fc_path, 0) polygs = gdb.GetLayer("my_layer") SpatialRef = polygs.GetSpatialRef() out_tif = r'E:\...\Processing\INPUT\test.tif' target = gdal.GetDriverByName('GTiff').Create(out_tif, x_res, y_res, 1, gdal.GDT_Byte) target.SetGeoTransform((x_min, pixel_width, 0, y_max, 0, -pixel_width)) target.SetProjection(SpatialRef.ExportToWkt()) band = target.GetRasterBand(1) NoData = -1 band.SetNoDataValue(NoData) band.FlushCache() gdal.RasterizeLayer(target, , polygs, options=["ATTRIBUTE=SoP", "burnValues=[-1]", "creationOptions=['COMPRESS=LZW', 'BIGTIFF=YES', 'blockxsize=256', 'blockysize=256']"]) target = None
As you can see I have 3 arguments in the options list of gdal.RasterizeLayer:
"ATTRIBUTE=SoP" (it works) "burnValues=[-1]" (it's being ignored) "creationOptions=['COMPRESS=LZW', 'BIGTIFF=YES', 'blockxsize=256', 'blockysize=256']" (it's being ignored)
NOTE: Does anyone know any good GDAL/OGR documentation? I find "https://gdal.org/python/index.html" and "https://pcjericks.github.io/py-gdalogr-cookbook/index.html" not very helpful.