You can retrieve raster pixel values with any of the following scripts. List of X,Y coordinates (as tuples) are in the python list named 'points_list'.
With Python GDAL:
from osgeo import gdal
driver = gdal.GetDriverByName('GTiff')
filename = "/home/zeito/pyqgis_data/aleatorio.tif" #path to raster
dataset = gdal.Open(filename)
band = dataset.GetRasterBand(1)
cols = dataset.RasterXSize
rows = dataset.RasterYSize
transform = dataset.GetGeoTransform()
xOrigin = transform[0]
yOrigin = transform[3]
pixelWidth = transform[1]
pixelHeight = -transform[5]
data = band.ReadAsArray(0, 0, cols, rows)
points_list = [ (355278.165927, 4473095.13829), (355978.319525, 4472871.11636) ] #list of X,Y coordinates
for point in points_list:
col = int((point[0] - xOrigin) / pixelWidth)
row = int((yOrigin - point[1] ) / pixelHeight)
print row,col, data[row][col]
With PyQGIS:
filename = "/home/zeito/pyqgis_data/aleatorio.tif" #path to raster
layer = QgsRasterLayer(filename,
"my_raster")
provider = layer.dataProvider()
extent = layer.extent()
xmin, ymin, xmax, ymax = extent.toRectF().getCoords()
cols = layer.width()
rows = layer.height()
pixelWidth = layer.rasterUnitsPerPixelX()
pixelHeight = layer.rasterUnitsPerPixelY()
block = provider.block(1, extent, cols, rows)
points_list = [ (355278.165927, 4473095.13829), (355978.319525, 4472871.11636) ]#list of X,Y coordinates
for point in points_list:
col = int((point[0] - xmin) / pixelWidth)
row = int((ymax - point[1] ) / pixelHeight)
print row,col, block.value(row, col)
I tried them with my particular raster and they worked. Result was, for both cases, the following:
4 4 36
7 13 42
The first and second valor (each line) are indices of row, column (for verification purposes). The third one is raster value.