1

I got the following command that works fine:

gdal_rasterize -l type -a TOT_P_2018 -tr 1000.0 1000.0 -a_nodata -1.0 -te -2825000.0 -3077000.0 10028000.0 5418000.0 -ot Int32 -of GTiff -co COMPRESS=DEFLATE -co PREDICTOR=2 -co ZLEVEL=9 grid_1km_surf.gpkg output.tif

How do I convert it to Python code? This is my code:

vector = ogr.Open("grid_1km_surf.gpkg")
vlayer = vector.GetLayer()
xmin, xmax, ymin, ymax = vlayer.GetExtent()
gdal.Rasterize("output.tif", vector_file, xRes=tr, yRes=tr,
               outputBounds=[xmin, ymin, xmax, ymax],
               outputType=gdal.GDT_Int32,
               options=['COMPRESS=DEFLATE', 'ATTRIBUTE=TOT_P_2018'])

Got stuck on two things:

  1. xmin, xmax, ymin, ymax are all zero (QGiS somehow knows it)

  2. I am getting an error: ERROR 6: Too many command options 'COMPRESS=DEFLATE'

Could someone tell me how to fix that?

4
  • Maybe the answer in gis.stackexchange.com/questions/417338/… is useful.
    – user30184
    Mar 28, 2023 at 8:55
  • Could be but still can't set value by attribute, the same problem even when I will move "COMPRESS" to creationOptions - ERROR 6: Too many command options 'ATTRIBUTE=TOT_P_2018' Mar 28, 2023 at 9:01
  • Have you had a look at gdal.RasterizeOption in the documents? Maybe set the options with gdal.RasterizeOptions() and then assign it to options= in the gdal.Rasterize() function.
    – PyMapr
    Mar 28, 2023 at 13:35
  • I tried but it gave exactly the same result, but maybe did it wrong way, will try your answer asap, thanx Mar 28, 2023 at 16:51

1 Answer 1

2

Using gdal.RasterizeOption to set the options for gdal.Rasterize() usually works.

from osgeo import ogr, gdal

vector_file = "grid_1km_surf.gpkg"

out = "output.tif"
vector = ogr.Open(vector_file)
vlayer = vector.GetLayer()
xmin, xmax, ymin, ymax = vlayer.GetExtent()
print(xmin, xmax, ymin, ymax)
# Set the options for gdal.Rasterize() with gdal.RasterizeOptions()
options = gdal.RasterizeOptions(creationOptions=['COMPRESS=DEFLATE'], outputType=gdal.GDT_Int32, outputBounds=[xmin, ymin, xmax, ymax], xRes=10, yRes=10, targetAlignedPixels=True, attribute=None) # Replace None with your attribute value 

gdal.Rasterize(out, vector_file, options=options)

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.