Opened 3 years ago

Closed 3 years ago

Last modified 2 years ago

#5807 closed defect (fixed)

Statistics in .aux.xml do not get updated from ComputeStatistics

Reported by: Mike Toews Owned by: warmerdam
Priority: normal Milestone: 1.11.4
Component: default Version: unspecified
Severity: normal Keywords: tiff pam metadata
Cc: antonio

Description

Consider a raster:

$ cat > myraster.xyz
X Y Z
0 0 2
1 0 10
0 1 15
1 1 20
$ gdal_translate myraster.xyz  myraster.tif
$ gdalinfo -stats myraster.tif

Note that the -stats flag causes a myraster.tif.aux.xml file to be generated, which does not appear in the "Files" listing at the top the first time, but a subsequent gdalinfo lists this file.

Now, modify a pixel value to change the minimum.

from osgeo import gdal
fname = 'myraster.tif'

ds = gdal.Open(fname, gdal.GA_Update)
band = ds.GetRasterBand(1)
ar = band.ReadAsArray()
ar[0, 0] = 4  # change from 2 to 4
band.WriteArray(ar)
print(band.ComputeStatistics(0))  # [4.0, 20.0, 12.25, 5.931905258852336]
print(band.GetMetadata()['STATISTICS_MINIMUM'])  # 4
ds = band = None

However, note that the statistics are not stored. Not to the .aux.xml file or the GDAL metadata.

ds = gdal.Open(fname)
band = ds.GetRasterBand(1)
ar = band.ReadAsArray()
print(ar[0, 0])  # 4
print(band.GetMetadata()['STATISTICS_MINIMUM'])  # 2
print(band.GetStatistics(0, 1))  # [2.0, 20.0, 11.75, 6.6473679001542]

A workaround is to delete the .aux.xml file and use ComputeStatistics(0) in GA_Update mode.

Change History (4)

comment:1 Changed 3 years ago by antonio

Cc: antonio added

comment:2 Changed 3 years ago by Even Rouault

trunk r28338 "GTiff: when overriding metadata in update mode, make sure to clear it from PAM file (#5807)"

comment:3 Changed 3 years ago by Even Rouault

Keywords: tiff pam metadata added
Milestone: 1.11.2
Resolution: fixed
Status: newclosed

Backported in branches/1.11 r28339

comment:4 Changed 2 years ago by Even Rouault

Milestone: 1.11.21.11.4

trunk r32208, branches/2.0 r32209, branches/1.11 r32210 "GTiff: SetMetadata?() doesn't properly clear existing PAM metadata (complement to #5807)"

Note: See TracTickets for help on using tickets.