I'm trying to calculate the logarithm of each cell in a raster. Is there a way to do this with F/OSS GIS? gdal_calc.py only has basic operators and the QGIS raster calculator feature doesn't seem to have a log function either.

I suppose I could still look into PostGIS raster functions but I'd prefer something that I could easily visualize in QGIS.

2 Answers 2


Try with:

gdal_calc.py -A test.tif --calc "log(A)" --type Float32 --outfile log_test.tif

Other available solutions with FOSS GIS in order to calculate the logarithm of a raster (just to name a few):

  • r.mapcalc in GRASS GIS;
  • Grid calculator in SAGA;
  • Raster calculator in SEXTANTE for gvSIG;
  • etc.

You could loop through each cell of the raster and use the math module to get the logarithm. Something like this:

import gdal
import math

dataset = gdal.Open( 'test.tif')

cols = dataset.RasterXSize
rows = dataset.RasterYSize

band = dataset.GetRasterBand(1)
data = band.ReadAsArray(0, 0, cols, rows)
for row in data:
    for item in row:
        value = math.log10(item)
        print value
        # write value to new dataset

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.