I'm creating some floating point rasters in GDAL with the GeoTIFF driver. When I load the resulting images into QGIS or Arc the default symbology puts the min at -3.40282e38 and max to 3.40282e38, so the raster looks exactly gray. Is there a way I can write the actual range into the GeoTIFF directly so when its loaded into a GIS program it automatically scales the histogram nicely? I've tried creating a default histogram like this:

rasterMin, rasterMax = raster.GetRasterBand(1).ComputeRasterMinMax()
raster.GetRasterBand(1).SetDefaultHistogram(rasterMin, rasterMax, 255)

But I get a error that looks like a binding issue:

  File "/usr/lib/python2.6/dist-packages/osgeo/gdal.py", line 846, in SetDefaultHistogram
    return _gdal.Band_SetDefaultHistogram(self, *args)
TypeError: not a sequence

Any suggestions on what I could do, or if I've made a mistake?


If you're generating the data, then you can keep track of the min and max on the fly. If you don't have direct control of the source data, say if you are compositing existing data, then using ComputeRasterMinMax(0) is fine.

Either way, after you've got your min and max values, you should call SetStatistics() on the band, and ensure you set your dataset to None when you've finished to ensure everything gets written to disk.

Also, something in the back of my mind is telling me using dataset.GetRasterBand(1).<some band function> won't work properly because of the way GDAL Python handles band references. I can't remember the details, but there was something on the GDALDev mailing list some time ago. So it's best to create a band variable explicitly: band = dataset.GetRasterBand(1), then call band.SetStatistics(min, max, mean, stddev).

| improve this answer | |
  • Thanks, this did the trick, and thanks for the context. By the way, I was able to nest the band reference without a temporary variable with no problems. – Rich Nov 19 '11 at 6:56
  • 1
    Note that band.ComputeStatistics(0) also stores statistics. It's an easy 1-line. – Mike T Jan 9 '15 at 0:24

I'm not aware of a way to do it using GDAL, but you can use python to call the 'calculate statistics' tool in Arc.

| improve this answer | |
  • 2
    Hi Cyrus, welcome to GIS.se :) While correct your answer is unlikely to attract votes for two reasons. The arcpy calculate statistics tool puts the stats in an external .aux file and the poster asked for "into the geotiff directly". Secondly, although Arcgis is mentioned in the title, the body of the question and it's tags indicate the poster is really more interested in a gdal/qgis solution. In any case, thanks for contributing and please keep it up. It's a bit of learning curve to get started but I think you'll find over time that the effort repays itself. – matt wilkie Nov 14 '11 at 17:52

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.