-1

I want to extract red band, from a multispectral raster with 5 band, using PyQGIS.

I tried this code, using the GDAL rastercalculator. I tried to extract the 3 bands, and then calculate my index.

import processing
entry = r'C:/RASTER.tif'
out = r'C:/RESULT.tif'
parameters = {'INPUT_A': entry, 'BAND_A': '1', 'INPUT_B': entry, 'BAND_B': '2', 'INPUT_C': entry, 'BAND_C': '3', 'FORMULA': ('(BAND_B * 2)- BAND_A - BAND_C)'; 'OUTPUT': out}
feedback = QgsProcessingFeedback()
processing.runAndLoadResults('gdal:rastercalculator',parameters,feedback=feedback)

But I have syntax error.

2
  • If I understood you correctly, you want to get value of the red raster band, right? Or you want to erase-extract-delete the red raster band to make a 4-band raster?
    – Xi Jin
    Jan 2 at 17:47
  • I want to separate red band, for then calculate diferent RGB indices. Jan 2 at 17:52
4

You are using a semicolon within the parameters dictionary where it should be a comma and a wrong bracket. Try:

import processing
entry = r'C:/RASTER.tif'
out = r'C:/RESULT.tif'
parameters = {'INPUT_A': entry, 'BAND_A': '1', 'INPUT_B': entry, 'BAND_B': '2', 'INPUT_C': entry, 'BAND_C': '3', 'FORMULA': '((BAND_B * 2)- BAND_A - BAND_C)', 'OUTPUT': out}
feedback = QgsProcessingFeedback()
processing.runAndLoadResults('gdal:rastercalculator',parameters,feedback=feedback)

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.