[GIS] How to get pixel size/cell size of raster layer

pyqgisqgisraster

After much searching, I've still not been able to figure out how to perform what seems like a simple function: I'd like to get the pixel size/cell size of a raster layer using PyQgis. Is there any native way to do this through QgsRasterLayer or a helper class? Or will I need to read the file in using GDAL functions, e.g.:

geotransform = dataset.GetGeoTransform()
if not geotransform is None:
    print 'Pixel Size = (',geotransform[1], ',',geotransform[5],')'

Thanks for your help.

Alex

Best Answer

You tagged your question with pyqgis. Here is a small solution using the Pyqgis modules

...
# Get a QgsRasterLayer object
ras = [yourObject]
pixelSizeX = ras.rasterUnitsPerPixelX()
pixelSizeY = ras.rasterUnitsPerPixelY()