[gdal-dev] removing/updating stats and histogram when editing raster

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] removing/updating stats and histogram when editing raster

dengo
Hi,

when you have a raster where stats and histogram have already been computed and later on you edit some pixels programmatically (GDAL C API), previously computed stats/histogram are not automatically invalidated/removed. For performance reasons it probably makes sense, that this is not done automatically and must be explicitly handled, but I'm not sure how to correctly handle removing or updating stats and histogram.

I would actually prefer just to remove the stats and histogram, but if that's not possible I can also recompute them.

Removing:
If I'm not mistaken, stats can be removed by setting the "STATISTICS_MINIMUM", ... Metadata Items to null, or would that only work correctly for some raster formats?
Is there a better way to remove previously computed statistics?
How to remove the histogram?

Updating:
Stats can be recomputed by calling GDALComputeRasterStatistics which will also update the corresponding metadata items.
For the histogram calling GDALGetDefaultHistogramEx with bForce=1 will not recompute the histogram (it seems bForce is only relevant in cases where the histogram has not been computed before).
What's the correct way to recompute the default histogram?

Kind regards,
Dennis

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: removing/updating stats and histogram when editing raster

Even Rouault-2
Dennis,

> Removing:
> If I'm not mistaken, stats can be removed by setting the
> "STATISTICS_MINIMUM", ... Metadata Items to null, or would that only work
> correctly for some raster formats?
> Is there a better way to remove previously computed statistics?
> How to remove the histogram?

In most cases, stats and histographs will be in a .aux.xml sidecar file that
you may just delete (or edit to remove those elements). If they are internal
to the raster format, that's more difficult and you may need to translate it.

>
> Updating:
> Stats can be recomputed by calling GDALComputeRasterStatistics which will
> also update the corresponding metadata items.
> For the histogram calling GDALGetDefaultHistogramEx with bForce=1 will not
> recompute the histogram (it seems bForce is only relevant in cases where
> the histogram has not been computed before).
> What's the correct way to recompute the default histogram?

You may use
GDALGetRasterHistogramEx() + GDALSetDefaultHistogramEx()

Even

--
Spatialys - Geospatial professional services
http://www.spatialys.com
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: removing/updating stats and histogram when editing raster

dengo
Hi Even,

Thanks for the quick reply.

Regarding removing stats: I just noticed that the gdal_edit.py script also just removes all metadata items starting with the 'STATISTICS_'. So that's probably the way to go.

For removing the histogram there is no such option in gdal_edit.py

Your suggestion with GDALGetRasterHistogramEx() + GDALSetDefaultHistogramEx() will not work for updating the histogram.
GDALGetRasterHistogramEx will not recompute the histogram, but instead will fetch the existing histogram if the settings (min,max,bincount) match.
In cases the settings don't match (e.g. different max) it will compute a new histogram and add it to the aux.xml, but the old invalid histogram will still be there.
When GDALSetDefaultHistogramEx is called the invalid histogram will still not be removed, but instead just the order of the histograms will be changed.

So it seems separately (not using GDAL) removing/editing the aux.xml file is currently the only option.
In my opinion that is a bit odd.
I think it would be good to have methods to remove statistics and histograms. 
Having those methods would also highlight that these methods need to be explicitly called when the raster is edited and that they are not automatically invalidated.

Another small question: Is there a GDAL config option, to make GDAL save computed statistics to the aux.xml file instead of the raster file itself when the raster is opened in EditMode?

Kind regards,
Dennis


Am Fr., 28. Dez. 2018 um 04:12 Uhr schrieb Even Rouault <[hidden email]>:
Dennis,

> Removing:
> If I'm not mistaken, stats can be removed by setting the
> "STATISTICS_MINIMUM", ... Metadata Items to null, or would that only work
> correctly for some raster formats?
> Is there a better way to remove previously computed statistics?
> How to remove the histogram?

In most cases, stats and histographs will be in a .aux.xml sidecar file that
you may just delete (or edit to remove those elements). If they are internal
to the raster format, that's more difficult and you may need to translate it.

>
> Updating:
> Stats can be recomputed by calling GDALComputeRasterStatistics which will
> also update the corresponding metadata items.
> For the histogram calling GDALGetDefaultHistogramEx with bForce=1 will not
> recompute the histogram (it seems bForce is only relevant in cases where
> the histogram has not been computed before).
> What's the correct way to recompute the default histogram?

You may use
GDALGetRasterHistogramEx() + GDALSetDefaultHistogramEx()

Even

--
Spatialys - Geospatial professional services
http://www.spatialys.com

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev