5

I'm using gdal lib for Python in a Jupyter Notebook environment.

I'd like to retrieve the resolution of a raster to use it in a gdal.warp call.

At the moment I do the following :

src = gdal.Open(mask)
ulx, xres, xskew, uly, yskew, yres  = src.GetGeoTransform()

but I get too much information. Is there a way to only retreive xres and yres?

Vince
  • 20,017
  • 15
  • 45
  • 64
Pierrick Rambaud
  • 2,788
  • 1
  • 14
  • 49

3 Answers3

9

If you only want to avoid the unused variables use this code:

import gdal
...
src = gdal.Open(mask)
_, xres, _, _, _, yres  = src.GetGeoTransform()
Zoltan
  • 7,325
  • 17
  • 27
3

Google says no. But a "workaround" could be to use operator.itemgetter:

import gdal, operator
src = gdal.Open(mask)
xres, yres = operator.itemgetter(1,5)(src.GetGeoTransform())
BERA
  • 72,339
  • 13
  • 72
  • 161
0

This worked for me in gdal version 3.4.1:

src = gdal.Open(mask)
xres = src.RasterXSize
yres = src.RasterYSize
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Zipzap
  • 1
  • 1