[Qgis-user] [pyqgis] looping through a raster

andrea antonello andrea.antonello at gmail.com
Fri May 5 07:48:45 PDT 2023


Hello,
I am trying to understand how to easily loop through the values of a
raster layer using pyqgis.

In the docs I see the sample and identify methods of the dataprovider,
but can't spot anything that uses rows/cols as a way of iteration.
Is the most efficient way to use transformCoordinates to get the world
coordinates and then use the sample function?

Something like this:

cols = dtmLayer.width();
rows = dtmLayer.height();
transformType = QgsRasterDataProvider.TransformType.TransformImageToLayer
for row in range(rows):
    for col in range(cols):
        point =
dtmLayer.dataProvider().transformCoordinates(QgsPoint(col, row),
transformType)
        value, res =
dtmLayer.dataProvider().sample(QgsPointXY(point.x(), point.y()), 1)

The above for some reason doesn't work yet though.

Thanks,
Andrea


More information about the QGIS-User mailing list