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
-
1QGIS comes with GDAL so use that. Here is another thread asking the same question essentially gis.stackexchange.com/questions/32995/…Liam G– Liam G2017年11月22日 09:35:20 +00:00Commented Nov 22, 2017 at 9:35
-
However, I can not reach the value. I editted my questionMustafa Uçar– Mustafa Uçar2017年11月22日 10:17:13 +00:00Commented 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 GDALLiam G– Liam G2017年11月22日 12:15:41 +00:00Commented 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!Mustafa Uçar– Mustafa Uçar2017年11月22日 12:34:30 +00:00Commented 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.htmletrimaille– etrimaille2017年12月20日 22:09:12 +00:00Commented Dec 20, 2017 at 22:09
1 Answer 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