8

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

1
  • What format are your rasters in? It is often possible to parse the resolution out of a raster's header.
    – sgrieve
    Nov 16, 2012 at 8:43

2 Answers 2

6

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

...
# assuming ras is a QgsRasterLayer object
pixelSizeX = ras.rasterUnitsPerPixelX()
pixelSizeY = ras.rasterUnitsPerPixelY()
1
  • 1
    Classic. The docs just need to have "(cell size)" added to the description of those two methods! Jan 11, 2020 at 14:51
0

To spell this one out in 2023, this code worked for me:

from qgis.core import QgsRasterLayer

ras = QgsRasterLayer('D:/Bigboy.tif')
pixelSizeX = ras.rasterUnitsPerPixelX()
pixelSizeY = ras.rasterUnitsPerPixelY()
print(pixelSizeX)
print(pixelSizeY)

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.