I have two datasets that I want to compare against each other within a defined shapefile boundary, within a defined yearly range EG (2000-2020)

a) A burned area raster for each year in the range.

b) A forest loss raster for each year in the range

These are classified as 1 = loss, 0 = no data.

My aim is to compare these against each other to see where they overlap for each year within the range. The output should be a CSV file which states the "Burned Forest Area" each year in hectares. EG 2000 - 450ha, 2001 - 1300ha, etc.

A solution could loop through for each year, and record a value where the area is both forest loss = 1, and burned area = 1

  1. Raster calculator each year, raster1*raster2
  2. Extract by mask
  3. Add all layers to the map

1 and 2 can ofc be done using python...

Iterate over them and try something like:

import csv, re

pixelsize = 500
yearly_data = []

for rasterlayer in QgsProject.instance().mapLayers().values():
    stats = processing.run("native:rasterlayerstatistics", {'INPUT':rasterlayer,'BAND':1,'OUTPUT_HTML_FILE':'TEMPORARY_OUTPUT'})['OUTPUT_HTML_FILE']
    with open(stats, 'r') as f:
            for i, line in enumerate(f):
                if i==5: #Line 5 is raster sum
                    sum_of_pixels = float(re.findall('\d*\.?\d+', line)[0])
                    area = sum_of_pixels*pixelsize**2
                    print('Area year {0} is: {1} m2'.format(rasterlayer.name(), area))
                    yearly_data.append([rasterlayer.name(), area])

with open("/home/bera/Desktop/out.csv", "w", newline="") as f:
    writer = csv.writer(f)

enter image description here

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.