Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#5805 closed enhancement (fixed)

Add command-line ability to recalculate raster statistics

Reported by: Mike Taves Owned by: warmerdam
Priority: normal Milestone: 2.0.0
Component: Utilities Version: unspecified
Severity: normal Keywords:
Cc: antonio


If a raster does not have statistics, gdalinfo -stats myfile.tif can be used to calculate and add statistics. However, if values in the raster dataset are modified, further attempts to use gdalinfo -stats myfile.tif do not force recalculation of statistics, since they already exist.

One strategy would be to allow gdalinfo to force recalculation of statistics with either -stats or -approx_stats flags. (However, doesn't the the "info" part of this utility suggests that it is intended to not modify the file? Also, very minor and almost non-ticket-worthy: -approx_stats is missing from usage string in gdalinfo.c)

A second and more intuitive strategy is to add this functionality to gdal_edit. That is, add both -stats and -approx_stats flags to this utility, and call ComputeStatistics(approx_stats). Note that this utility has a related and conflicting -unsetstats option (changeset:27608) which needs to be considered.

Attachments (1)

stats.diff (6.7 KB) - added by Mike Taves 7 years ago.
git diff --no-prefix 3fcf12b..HEAD > stats.diff

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 years ago by antonio

Cc: antonio added

comment:2 Changed 7 years ago by Mike Taves

Also noteworthy, the gdalinfo -mm flag is documented as "Force computation of the actual min/max values for each band in the dataset" (via GDALComputeRasterMinMax). However, this option does not store the new partial statistics, it only displays the information in the output beside the previously computed and stored statistics. E.g., I modified a "2" pixel to a "3", then gdalinfo -mm:

Band 1 Block=2x2 Type=Byte, ColorInterp=Gray
  Min=2.000 Max=20.000   Computed Min/Max=3.000,20.000
  Minimum=2.000, Maximum=20.000, Mean=11.750, StdDev=6.647

In case it isn't clear, the intentions of this ticket are to calculate and store raster statistics.

comment:3 Changed 7 years ago by Mike Taves

See pull request for patch

comment:4 Changed 7 years ago by Mike Taves

A correction to a previous comment is that gdalinfo -stats does not modify the GeoTIFF file, but creates an .aux.xml file; see #5807. Unfortunately, the raster statistics do not appear to be updated by ComputeStatistics if the .aux.xml file is present.

Changed 7 years ago by Mike Taves

Attachment: stats.diff added

git diff --no-prefix 3fcf12b..HEAD > stats.diff

comment:5 Changed 7 years ago by Mike Taves

Ready for merge to trunk with patch -p0 < stats.diff

comment:6 Changed 7 years ago by Even Rouault

Milestone: 2.0
Resolution: fixed
Status: newclosed

trunk r28343 " add -stats and -approx_stats flags (patch by mwtoews, #5805)"

comment:7 Changed 7 years ago by Even Rouault

trunk r28347 "Fix test_gdal_edit_py_5 when gdalnumeric is not available"

Hum I thought this was in a modified version of your patch. Weird.

comment:8 Changed 7 years ago by Mike Taves

Yes, it was modified, then lost somewhere in subsequent commit in git (where I did git push -f, because I didn't understand other oddities). Thanks!

comment:9 Changed 6 years ago by Even Rouault


Milestone renamed

Note: See TracTickets for help on using tickets.