I'm trying to calculate the amount of pixels with the value of 0.3 to 0.7 between two different rasters (each raster of a different year) to calculate the loss of green spaces trough time.
Is it possible to do this in QGIS or SAGA GIS?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityr.report
from GRASS in the Processing Toolbox lets you get a count if you set the Units to c (number of cells).
You get a .txt
-file like:
+-----------------------------------------------------------------------------+
| RASTER MAP CATEGORY REPORT |
|LOCATION: temp_location Tue Nov 03 16:08:38 2020|
|-----------------------------------------------------------------------------|
| north: 1253081.94570147 east: 2768768.04053958 |
|REGION south: 1242521.94570147 west: 2758100.04053958 |
| res: 2 res: 2 |
|-----------------------------------------------------------------------------|
|MASK: none |
|-----------------------------------------------------------------------------|
|MAP: (untitled) (rast_5fa17273abbcf9 in PERMANENT) |
|-----------------------------------------------------------------------------|
| Category Information | cell|
| #|description | count|
|-----------------------------------------------------------------------------|
|98| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 4|
|97| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 30|
|96| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 192|
|94| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 324|
|95| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 460|
|93| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 604|
|92| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 827|
You can use the Python Console (Window > Python Console
) in QGIS to accomplish this. Change the file path to your NDVI and run the following in the Python Console:
import numpy as np
import gdal
# Your input NDVI data
tif = '/Path/to/your/ndvi.tif'
# Open the tif in GDAL and convert to a numpy array
raster = gdal.Open(tif)
array = np.array(raster.GetRasterBand(1).ReadAsArray())
# Get a count of pixels meeting your conditions
count = np.count_nonzero((0.3 <= array) & (array <= 0.7))
# Report the results
print(f'There are {count} pixels with values from 0.3 to 0.7')