Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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)
band.WriteArray(data)
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:

<PAMDataset>
  <PAMRasterBand band="1">
    <Metadata>
      <MDI key="STATISTICS_MINIMUM">0</MDI>
      <MDI key="STATISTICS_MAXIMUM">10</MDI>
      <MDI key="STATISTICS_MEAN">5.0189833333333</MDI>
      <MDI key="STATISTICS_STDDEV">2.9131294111984</MDI>
    </Metadata>
  </PAMRasterBand>
</PAMDataset>

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

1 Answer 1

up vote 7 down vote accepted

You can use GetStatistics Method to get the stats.

eg.

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

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

so the xml can be read:

<PAMDataset>
  <PAMRasterBand band="1">
    <Metadata>
      <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>
    </Metadata>
  </PAMRasterBand>
</PAMDataset>

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
4  
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

 
discard

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.