1

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")
rasterLyr.isValid()
#extent means QgsRectangle 
c = rasterLyr.extent()
qry = rasterLyr.dataProvider().identify(c,QgsRaster.IdentifyFormatValue)
qry.isValid()
qry.results()

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

asked Nov 22, 2017 at 9:10
5
  • 1
    QGIS comes with GDAL so use that. Here is another thread asking the same question essentially gis.stackexchange.com/questions/32995/… Commented Nov 22, 2017 at 9:35
  • However, I can not reach the value. I editted my question Commented Nov 22, 2017 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 Commented Nov 22, 2017 at 12:15
  • @Freighty I solved the problem! Yes you're right the link that you send to me is very helpful! Commented Nov 22, 2017 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 Commented Dec 20, 2017 at 22:09

1 Answer 1

1

@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
answered Nov 22, 2017 at 13:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.