1

I have a Python script to run gdal_grid using the subprocess command at the moment with certain parameters defined as variables earlier in the script

subprocess.call(f'gdal_grid -a nearest:radius1={cell_size}:radius2={cell_size}:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of GTiff -txe {txextent} -tye {tyextent} -tr {resolution} -zfield z {shapefile} {output_file}.tif')

It works perfectly and the raster produced is exactly as intended. However, I've decided rather than use the subprocess method that it would be better practice to use Gdal.Grid as intended for Python.

So far I have this

rasterDs = gdal.Grid(gridded_raster, 
                     shapefile, 
                     format='GTiff',
                     algorithm='nearest:radius1={cell_size}:radius2={cell_size}:angle=0.0:min_points=0:nodata=0.0', 
                     outputType = 'GDT_Float32',
                     zfield='z')
rasterDs.FlushCache()

But I'm wondering, how do I provide the -tye and -txe along with -tr in order to ensure I get the exact same results? I can't seem to find them in the documentation https://gdal.org/python/osgeo.gdal-module.html#GridOptions

2 Answers 2

1

You can pass your gdal_grid options directly to gdal.Grid() as a string, including the -tr arg.

options = f"-txe {xmin} {xmax} -tye {ymin} {ymax} -tr {cellsize} {cellsize} -ot Float32 -l grid -a nearest:radius1={cellsize}:radius2={cellsize}:angle=0.0:nodata=0.0"
rasterDs = gdal.Grid(gridded_raster, shapefile, options=options)

From the doc:

Grid(destName, srcDS, **kwargs)
    etc...
    Keyword arguments are :
      options --- return of gdal.GridOptions(), string or array of strings <-- note "string" means a gdal_grid commandline string

1

There doesn't seem to be an option to set -tr. I could be wrong. However, you could get the correct resolution by calculating the raster width and height in pixels form the bounding box coordinates. For setting -tye and -txe use the outputBounds=[ulx, uly, lrx, lry]. Then using the bounds, calculate the raster size using something like

minx = ulx
maxx = lrx
miny = lry
maxy = uly
pixel_size = 10 # User specific

width = maxx-minx
height = maxy-miny

cols = width/pixel_size
rows = height/pixel_size

It might be necessary to ceil the width/height and/or cols/rows. Note, cols and rows will be the array sizes in pixels used to specify width and height.

rasterDs = gdal.Grid(gridded_raster, 
                     shapefile, 
                     format='GTiff',
                     algorithm='nearest:radius1={cell_size}:radius2={cell_size}:angle=0.0:min_points=0:nodata=0.0', 
                     outputType = 'GDT_Float32',
                     zfield='z',
                     outputBounds=[ulx, uly, lrx, lry],
                     width=cols,
                     height=rows)
0

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.