3

I am hoping someone can help me with an issue I am having with gdal_calc. I am looking to create NDVI rasters from 4 band 8 bit ortho tiles. I have processed the tiles using the following calculation:

 gdal_calc -A DDE1.tif --A_band=4 -B DDE1.tif --B_band=1 --outfile=DDE1_ndvi.tif --type=Float32 --calc="(A-B)/(A+B)"

The results from the calculation produces integer values that are not typical of NDVI. However, when I use the same data in QGIS raster calculator using the expression below a different, more typical result is created.

 ("DDE1@4"-"DDE1@1")/("DDE1@4"+"DDE1@1")

Is there a different setting or function I need to call in gdal_calc to get values that have decimals? I thought the --type=Float32 would ensure output values as decimals, but they are integers. Below is a graphic with the two results.

NDVI difference

  • Input rasters are not float, right? – Vladimir Dec 21 '17 at 9:48
  • No, as stated the ortho tiles are 8 bit, which are integers, not float. – Ryan Garnett Dec 22 '17 at 13:47
  • Then I suggest you to convert the input to float first. --calc="(asarray(A, dtype=float32)-asarray(B, dtype=float32))/(asarray(A, dtype=float32)+asarray(B, dtype=float32))" – Vladimir Dec 23 '17 at 11:31
  • That doesn't make sense, as it would increase the size of the dataset 4x without providing any benefit. The point of the question was not to understand how to make it work, but to understand why there is a difference between the two processes. Making temp data is never the best approach, especially when the task can be completed properly, which is the case in QGIS raster calc. – Ryan Garnett Dec 28 '17 at 19:52
  • 1
    This is exactly what I answered. Use numpy function 'asarray' within within 'gdal_calc' command "to get values that have decimals". To my best knowledge QGIS raster calculator writes float internally by default. Keep in mind that gdal_calc is itself a higher level of abstraction than internal process in QGIS which uses GDAL API. Additional insights can be found in the source: github.com/qgis/QGIS/blob/master/src/analysis/raster/… – Vladimir Dec 29 '17 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.