Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I regularly create my own GeoTIFF rasters with GDAL in Python, e.g.:

from osgeo import gdal
from numpy import random
data = random.uniform(0, 10, (300, 200))
driver = gdal.GetDriverByName('GTiff')
ds = driver.Create('MyRaster.tif', 200, 300)
band = ds.GetRasterBand(1)
ds = band = None # save, close

however when the result is viewed with ArcCatalog/ArcGIS, it looks either black or grey, since it has no statistics. This is solved either by right-clicking the raster and choosing "Calculate Statistics..." in ArcCatalog (there are several other ways to do this), or using gdalinfo in a command prompt:

gdalinfo -stats MyRaster.tif

will generate MyRaster.tif.aux.xml, which is used by ArcGIS to properly scale the raster. The PAM (Persistent Auxiliary Metadata) file contains the statistics, most notably the minimum and maximum values:

  <PAMRasterBand band="1">
      <MDI key="STATISTICS_MEAN">5.0189833333333</MDI>
      <MDI key="STATISTICS_STDDEV">2.9131294111984</MDI>

My question: is there a built-in way of getting GDAL to create a statistics file (other than using the gdalinfo -stats command)? Or do I need to write my own?

share|improve this question
up vote 8 down vote accepted

You can use GetStatistics Method to get the stats.


stats =   ds.GetRasterBand(1).GetStatistics(0,1)

it will return (Min, Max, Mean, StdDev)

so the xml can be read:

  <PAMRasterBand band="1">
      <MDI key="STATISTICS_MINIMUM">stats[0]</MDI>
      <MDI key="STATISTICS_MAXIMUM">stats[1]</MDI>
      <MDI key="STATISTICS_MEAN">stats[2]</MDI>
      <MDI key="STATISTICS_STDDEV">stats[3]</MDI>

I dont know any pythonic way to create/manipulate xml file.But given the simplistic nature of the accompanying xml it should pretty trival to create one it with file I/O operations

share|improve this answer
It turns out that band.GetStatistics(0,1) will actually calculate the statistics, and add it to the GeoTIFF metadata in the single file. No other files required. However from testing with Esri products, it only works with ArcGIS 10.0 and up, not ArcGIS 9.3 or before. – Mike T Jul 17 '12 at 8:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.