1

How do I subtract two raster using gdal_calculate command?

Note that gdal_calc is different from gdal_calculate.py.

The rasters I am using have different dimensions.

5

Just worked out how to do this with gdal_calc.py - to get around issues with files with different dimensions (and resolutions) you can use a VRT.

gdalbuildvrt -separate combined.vrt dem1.tif dem2.tif
gdal_calc.py -A combined.vrt -A_band=1 -B combined.vrt -B_band=2 --calc="A-B" --outfile diff.tif
1
  • 1
    One small change here is that "-A_band" should actually have two dashes before it, like "--A_band". – Brideau Mar 16 '17 at 20:49
3

The basic syntax is similar to gdal_calc.py

i.e.

gdal_calculate -a a.tif -b b.tif --calc="a - b" --outfile c.tif

If your input rasters are unsigned (i.e Byte or UInt16 etc) and the result may contain negative values, you need to specifically cast to a signed type :

gdal_calculate -a a.tif -b b.tif --calc="Int16(a) - b" --outfile c.tif

If your rasters are multiband, you need to specify the band (unless you want multiband output)

gdal_calculate -a a.tif -b b.tif --calc="Int16(a[0]) - b[0]" --outfile c.tif

If your rasters have defined NoData values, use the --nodata flag. You don't need to specify the NoData values as the script will determine them from the rasters:

gdal_calculate --nodata -a a.tif -b b.tif --calc="Int16(a) - b" --outfile c.tif
5
  • What if -a has a nodata value of -32767 and -b has a nodata value of -3.40282e+38? – lovelyvm Jan 14 '15 at 8:34
  • I tried to load the raster in qgis and check the properties under transparency and observed the nodata value box has been marked x and it says -nodata value is -32767. Is that what you meant with as long as it's defined as nodata? – lovelyvm Jan 14 '15 at 8:51
  • I don't know how qgis determines nodata. If gdalinfo says the nodata value is -32767 then the script will use that value. – user2856 Jan 14 '15 at 8:57
  • Can I use gdal_calculate in batch processing? – lovelyvm Jan 29 '15 at 8:20
  • Yes. But if you know python it's probably easier to use the API code.google.com/p/gdal-calculations/wiki/CalculationsLibrary – user2856 Jan 29 '15 at 8:45
2

On the help page an example is given:

gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"

This is addition, but subtraction is just as easy:

gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A-B"
11
  • gdal_calc is different from gdal_calculate. Tried gdal_calc and it is only applicable to raster with the same dimensions. – lovelyvm Jan 8 '15 at 3:06
  • Fair enough. Surely though the answer would only be valid where there is overlap, you can extract using GDAL_Translate with -projwin switch to get rasters the same dimension. Are their pixel sizes the same? – Michael Stimson Jan 8 '15 at 3:16
  • the 3 A raster has this pixel sizes, Pixel Size = (0.009474281391831,-0.009489211800969) Pixel Size = (0.009698025551684,-0.009698469846985) Pixel Size = (0.011755044580009,-0.011769524849809) while the B raster has it's fixed pixel size which is Pixel Size = (0.010067480700000,-0.010067480700000). What should I do? – lovelyvm Jan 8 '15 at 5:18
  • Conceivably you could resample all rasters to match the largest cell size as this is the least accurate. GDAL_Translate will do that too. – Michael Stimson Jan 8 '15 at 5:21
  • Can I used gdalwarp in resampling?or just the gdal_translate? – lovelyvm Jan 8 '15 at 5:34

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.