Skip to content

GPKG raster: fix band metadata/statistics mixed up between bands - #15434

Open
alonfaraj wants to merge 1 commit into
OSGeo:masterfrom
alonfaraj:fix/gpkg-band-metadata
Open

alonfaraj wants to merge 1 commit into
OSGeo:masterfrom
alonfaraj:fix/gpkg-band-metadata

Conversation

@alonfaraj

Copy link
Copy Markdown
Contributor

What does this PR do?

On a multi band GPKG raster, every band reads band 1 metadata. So after gdal raster edit --stats, all bands show band 1 stats. Opening in update mode saves wrong values to the file.

Repro

gdal raster create --of GPKG --co TILE_FORMAT=PNG --size 1,1 --bbox 0,0,1,1 --band-count 3 --burn 40 --burn 120 --burn 200 rgb.gpkg
gdal raster edit --stats rgb.gpkg
gdal raster info rgb.gpkg | grep -E "^Band|Minimum="
Band 1 Block=256x256 Type=Byte, ColorInterp=Red
  Minimum=40.000, Maximum=40.000, Mean=40.000, StdDev=0.000
Band 2 Block=256x256 Type=Byte, ColorInterp=Green
  Minimum=40.000, Maximum=40.000, Mean=40.000, StdDev=0.000    <--120
Band 3 Block=256x256 Type=Byte, ColorInterp=Blue
  Minimum=40.000, Maximum=40.000, Mean=40.000, StdDev=0.000    <- 200
Band 4 Block=256x256 Type=Byte, ColorInterp=Alpha
  Minimum=40.000, Maximum=40.000, Mean=40.000, StdDev=0.000    <- 255

Reproduced on 3.13.3 and master.

What are related issues/pull requests?

AI tool usage

  • AI (Y-a-t-il-un-Copilot-dans-l'avion, Chat-j'ai-pété, Jean-Claude Dusse or something similar) supported my development of this PR. See our policy about AI tool use. Use of AI tools must be indicated.

Tasklist

  • Make sure code is correctly formatted (cf pre-commit configuration)
  • Add test case(s)
  • Add documentation
  • Updated Python API documentation (swig/include/python/docs/)
  • Review
  • Adjust for comments
  • All CI builds and checks have passed
  • ADD YOUR TASKS HERE

Environment

Provide environment details, if relevant:

  • OS: Ubuntu 26.04
  • Compiler: gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0

@jratike80

Copy link
Copy Markdown
Collaborator

It would be kind to add a bit more information about what happens, even you do have a good test case.
So, before creating statistics correct statistics are reported because GDAL computes them from the source data

gdalinfo -stats rgb.gpkg
...
Band 1 Block=256x256 Type=Byte, ColorInterp=Red
  Minimum=40.000, Maximum=40.000, Mean=40.000, StdDev=0.000
...
Band 2 Block=256x256 Type=Byte, ColorInterp=Green
  Minimum=120.000, Maximum=120.000, Mean=120.000, StdDev=0.000
...
Band 3 Block=256x256 Type=Byte, ColorInterp=Blue
  Minimum=200.000, Maximum=200.000, Mean=200.000, StdDev=0.000
 ...
Band 4 Block=256x256 Type=Byte, ColorInterp=Alpha
  Minimum=255.000, Maximum=255.000, Mean=255.000, StdDev=0.000

After running gdal raster edit --stats rgb.gpkg the gpkg_metadata table has correct statistics

1	1	dataset	http://gdal.org	text/xml	<GDALMultiDomainMetadata>
  <Metadata domain="BAND_1">
    <MDI key="STATISTICS_MAXIMUM">40</MDI>
    <MDI key="STATISTICS_MEAN">40</MDI>
    <MDI key="STATISTICS_MINIMUM">40</MDI>
    <MDI key="STATISTICS_STDDEV">0</MDI>
    <MDI key="STATISTICS_VALID_PERCENT">100</MDI>
  </Metadata>
  <Metadata domain="BAND_2">
    <MDI key="STATISTICS_MAXIMUM">120</MDI>
    <MDI key="STATISTICS_MEAN">120</MDI>
    <MDI key="STATISTICS_MINIMUM">120</MDI>
    <MDI key="STATISTICS_STDDEV">0</MDI>
    <MDI key="STATISTICS_VALID_PERCENT">100</MDI>
  </Metadata>
  <Metadata domain="BAND_3">
    <MDI key="STATISTICS_MAXIMUM">200</MDI>
    <MDI key="STATISTICS_MEAN">200</MDI>
    <MDI key="STATISTICS_MINIMUM">200</MDI>
    <MDI key="STATISTICS_STDDEV">0</MDI>
    <MDI key="STATISTICS_VALID_PERCENT">100</MDI>
  </Metadata>
  <Metadata domain="BAND_4">
    <MDI key="STATISTICS_MAXIMUM">255</MDI>
    <MDI key="STATISTICS_MEAN">255</MDI>
    <MDI key="STATISTICS_MINIMUM">255</MDI>
    <MDI key="STATISTICS_STDDEV">0</MDI>
    <MDI key="STATISTICS_VALID_PERCENT">100</MDI>
  </Metadata>
</GDALMultiDomainMetadata>

However, now gdalinfo -stats rgb.gpkg´ and gdalinfo rgb.gpkg` tries to use existing statistics without recomputing them, and the statistics are wrongly taken from the band 1 statistics for all layers.

gdalinfo -stats rgb.gpkg
...
Band 1 Block=256x256 Type=Byte, ColorInterp=Red
  Min=40.000 Max=40.000
...
Band 2 Block=256x256 Type=Byte, ColorInterp=Green
  Min=40.000 Max=40.000
 ...
Band 3 Block=256x256 Type=Byte, ColorInterp=Blue
  Min=40.000 Max=40.000
  ...
Band 4 Block=256x256 Type=Byte, ColorInterp=Alpha
  Min=40.000 Max=40.000


This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants