
QGIS Python Programming Cookbook, Second Edition
By :

The ability to view rasters in a geospatial context relies on the conversion of pixel locations to coordinates on the ground. Sooner or later, when you use Python to write geospatial programs, you'll have to perform this conversion yourself.
We will use the SatImage raster available at https://github.com/GeospatialPython/Learn/raw/master/SatImage.zip
Place this raster in your /qgis_data/rasters
directory.
We will use GDAL to extract the information needed to convert pixels to coordinates and then use pure Python to perform the calculation. We'll use the center pixel of the image as the location to convert.
Start QGIS.
From the Plugins menu, select Python Console.
We need to import the gdal
module:
from osgeo import gdal
Then, we need to define the reusable function that does the conversion as accepting a GDAL GeoTransform
object, containing the raster georeferencing information and the pixel's x, y values:
...