I want to read the every cell value in a raster file. When I run the script, it should give me the cell location and it's value.

I can reach center point of the raster and read it's value with this script:

rasterLyr = QgsRasterLayer("C:\qgis_data\SatImage\SatImage.tif",
                                "Sat Image")

#extent means QgsRectangle 
c = rasterLyr.extent()

qry = rasterLyr.dataProvider().identify(c,QgsRaster.IdentifyFormatValue)


If there is a toolbox which solves this problem in QGIS 3.0, it also helps me.

I use QGIS 3.0 as I mentioned and python 3.6

  • 1
    QGIS comes with GDAL so use that. Here is another thread asking the same question essentially gis.stackexchange.com/questions/32995/…
    – Liam G
    Nov 22 '17 at 9:35
  • However, I can not reach the value. I editted my question Nov 22 '17 at 10:17
  • QgsRasterLayer is purely for rendering in the data (see the docs qgis.org/api/classQgsRasterLayer.html). There's no functions (that I am aware of) that can change the data format from QgsRasterLayer object. Most suggestions centre on simply using GDAL
    – Liam G
    Nov 22 '17 at 12:15
  • @Freighty I solved the problem! Yes you're right the link that you send to me is very helpful! Nov 22 '17 at 12:34
  • 1
    @LiamG You can access raw data within the QGIS API using QgsRasterBlock. It allows you to get all raster values in an array, without using GDAL.The question is about QGIS 3. qgis.org/api/classQgsRasterBlock.html
    – etrimaille
    Dec 20 '17 at 22:09

@Freighty's answer was so helpful and I want to summarize it for others.

with rasterio.open('C:\oda_data\slope_srtm.tif', 'r+') as r:
    arr = r.read()

It reads all raster values and create and array whose name is arr

If you want to learn the band, row and column numbers you can use it arr.shape

And last if you reach a specific raster value in a specific band you can use that:

print(arr[band number, row number, column number])

Note: If rasterio is not installed it gives module error. You can install it according to your system requirement by using this link

I used pip tool in cmd:

python3 -m pip install filename.whl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.