I'm just wanting to make a simple map algebra operation in the osgeo4w shell using gdal_calc but I get wrong results when I divide one raster with the other. When I use the same rasters in QGIS and perform the same map algebra operation with raster calculator, I get no problem.

  • Hi vascobrunes, could you clarify how the output is incorrect. Also, have you checked if you are using the cmdline syntax correctly for gdal_calc?
    – s_a
    Sep 10 '12 at 16:37
  • For example: I'm doing a simple calculation and getting this: Error! Dimensions of file are different from other files. Cannot proceed while if I do it in QGIS it works fine Sep 12 '12 at 9:27

I think my best guess would be that you should clip one raster using the other, and save the results as a .vrt to save time.

use this vrt in your previous gdal_calc calculation. that should work.

side-note: The answer is actually in the Error message. I dug a bit deeper and looked at the code here ( http://svn.osgeo.org/gdal/trunk/gdal/swig/python/scripts/gdal_calc.py ). A comment at the tops specifies that rasters should be of the same extent.

LATER EDIT: If your problem appears to be that the values are too big for your data type this is because gdalcalc determines the data type based on the input rasters not the appropriate output data size (i believe from the link I provide you can search data type and you'll see the comment).

I am not sure of the options to change data type in the gdalcalc syntax but you could precede your calculation by translating your input raster with gdaltranslate into a larger data type;32bit-float should be fine. saving as a vrt will also save time gdal_translate -ot Float32 -of VRT old.tif new.vrt

link: http://www.gdal.org/gdal_translate.html

Still Later Edit: I actually checked gdal_calc.py on my comp and found it has a --type parameter you can enter from the command line. I think this will work for your problem

Hope that helps :)

  • I solved it by first using gdalwarp on the raster files. Sep 12 '12 at 12:55
  • Hey, if you write up your solution other people might benefit. As well, I won't think it is 'unsolved' ;) btw, glad you figured it out.
    – s_a
    Sep 12 '12 at 13:01
  • It is still unsolved: I still get different results from using gdal_calc or the qgis raster calculator. With gdal_calc, in a raster with 0 and 1, if I do raster*88 its good. but if I do raster*2012257 the result is weird. I QGIS raster calculator the result is what was expected (2012257). Oct 1 '12 at 17:54
  • seems like the value I use in gdal_calc can't be bigger than 30999 Oct 1 '12 at 18:08
  • 2
    A part of the solution is here: trac.osgeo.org/gdal/ticket/4064 for this: "Gdal_calc performs calculations using the native data type of the original input layers, which can be problematic if, say, you are dividing two integers and expect a float output." Feb 28 '13 at 14:41

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.