<div dir="ltr"><br><br>On Sat, Jun 16, 2018 at 10:00 PM, Even Rouault <<a href="mailto:even.rouault@spatialys.com">even.rouault@spatialys.com</a>> wrote:<br>><br>> ><br>> > I checked, results with gdalinfo -stats are wrong because existing<br>> > STATISTICS_* metadata are reported even if approximate statistics are not<br>> > allowed.<br>><br>> No, if STATISTICS_APPROXIMATE=YES and is set in .aux.xml (because initial<br>> computation was done with -approx_stats) and you do gdalinfo -stats after,<br>> then statistics will be recomputed on all samples and<br><div>> STATISTICS_APPROXIMATE=YES  will be cleared</div><div><br></div><div>IMHO, metadata can not be trusted because it is (often) not known which software generated these metadata with which method. The only safe assumption is that statistics in metadata can be approximations. Therefore statistics from metadata should only be reported if approximations are ok, no matter if STATISTICS_APPROXIMATE=YES exists or not.<br></div><div><br></div>><br>> > The problem is, STATISTICS_APPROXIMATE is not set. Other software<br>> > using GDAL to create raster datasets may use<br>> > GDALRasterBand::SetStatistics() which does not indicate if stats are<br>> > approximations., i.e. stats are approximations but there is no<br>> > STATISTICS_APPROXIMATE=YES.<br>><br>> The idea is that if you use GDALRasterBand::SetStatistics()  then you are<br>> assumed to provide exact statistics. If they are only approximate, then you<br>> should also set STATISTICS_APPROXIMATE=YES with GDALSetMetadataItem()<br><div><br></div><div>What about 1) all the datasets that have already been created, 2) all the third-party software packages providing some sort of statistics in metadata?<br></div><div><br></div><div>IMHO, the STATISTICS_APPROXIMATE=YES mechanism does not work because of 1) and 2).<br></div><div>></div>> ><br>> > GDAL assumes that STATISTICS_* metadata represent stats on all pixels, this<br>> > is IMHO wrong. You can only hope that STATISTICS_* metadata represent stats<br>> > on all pixels if a respective metadata item has been set to boolean true,<br>> > something like STATISTICS_ALL_PIXELS=YES.<br>><br>> I'm really confused. Why introducing yet another item whereas<br>> STATISTICS_APPROXIMATE=YES is there for that purpose ?<br><div><br></div><div>Simpler: statistics in metadata can be approximations, also if STATISTICS_APPROXIMATE=YES is not set. If exact statistics are requested, scan all pixels. A new metadata item like STATISTICS_APPROXIMATE or STATISTICS_ALL_PIXELS is not needed.<br></div><div><br></div><div>></div>> > Even in this case, an option to<br>> > force recomputing raster band stats would be very nice to have (verifying<br>> > metadata).<br>><br>> ComputeStatistics() will recompute statistics. It is true that with gdalinfo -<br>> stats, they are not recomputed if they already exist and were not approximate<br>> since it calls GetStatistics() and not ComputeStatistics(). An easy workaround<br>> is to delete the .aux.xml to force recomputation.<br><div><br></div><div>At least recomputation of min/max can already be forced with gdalinfo -mm.</div><div><br></div><div>It would be nice if gdalinfo -stats would also trigger forced recomputation of exact statistics. Currently there is no difference between gdalinfo with and without -stats if statistics already exist in metadata and STATISTICS_APPROXIMATE=YES is absent (standard case for existing data): -stats has no effect here. Forced recomputation would be a change in the behaviour of gdalinfo -stats.<br></div><div><br></div><div>The purpose of gdalinfo -approx-stats would be (already is?) to quickly get stats for a raster band, either from metadata or by approximation.<br></div><div><br></div><div>Trying to avoid another option like gdalinfo -exact-stats.<br></div><div><br></div><div>Markus<br></div><br></div>