1

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?

2

r.report from GRASS in the Processing Toolbox lets you get a count if you set the Units to c (number of cells).

r.report

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|
2
  • Thank you so much! – Manuel Esteban Nov 3 '20 at 15:35
  • QGIS, SAGA, and GRASS GIS all have reclassify algorithms (although there are some differences between them). Basically you can create a new raster where values in your range get values of 1 and values out of your range get values of 0. Then run your r.report on the output from the reclassification to sum the 1 values. You may need to convert your 0.3 to 0.7 values to an integer raster prior to reclassification so just multiply the values by 10 and save the result as an integer raster. – GBG Nov 3 '20 at 17:00
1

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')

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.