geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
878 stars 183 forks source link

Confusing comment in the code regarding getGDALMetadata #264

Closed drnextgis closed 2 years ago

drnextgis commented 2 years ago

It is saying here:

If sample is passed to null, dataset-level metadata will be returned.

The question is what dataset-level metadata is? As I can see it simply returns metadata of the last band:

> img.fileDirectory.GDAL_METADATA;
'<GDALMetadata>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="0">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="0">161.13451304075</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="0">27</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="0">47.78883943453</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="0">98.67</Item>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="1">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="1">156.19044508527</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="1">17</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="1">45.653592048342</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="1">98.67</Item>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="2">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="2">156.71097900011</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="2">1</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="2">41.777101570921</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="2">98.67</Item>\n' +
  '</GDALMetadata>\n' +
  '\x00'
> JSON.stringify(img.getGDALMetadata(0)) == JSON.stringify(img.getGDALMetadata())
false
> JSON.stringify(img.getGDALMetadata(1)) == JSON.stringify(img.getGDALMetadata())
false
> JSON.stringify(img.getGDALMetadata(2)) == JSON.stringify(img.getGDALMetadata())
true

cc @rouault

DanielJDufour commented 2 years ago

fwiw, I'm happy to re-write this function to improve the user experience. @constantinius , just let me know what you'd like the intended functionality to be. thank you.

drnextgis commented 2 years ago

If I understood it correctly getGDALMetadata() (without specifying a band) should return:

{
  LOL: 'WUT'
}

in case of a raster with the following metadata:

> image.fileDirectory.GDAL_METADATA;
'<GDALMetadata>\n' +
  '  <Item name="LOL">WUT</Item>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="0">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="0">228.06239581697</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="0">0</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="0">72.29049275142</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="0">100</Item>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="1">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="1">223.45111593246</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="1">0</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="1">73.473561068089</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="1">100</Item>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="2">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="2">227.75670581369</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="2">0</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="2">73.296580537495</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="2">100</Item>\n' +
  '  <Item name="STATISTICS_MAXIMUM" sample="3">255</Item>\n' +
  '  <Item name="STATISTICS_MEAN" sample="3">73.349654800482</Item>\n' +
  '  <Item name="STATISTICS_MINIMUM" sample="3">0</Item>\n' +
  '  <Item name="STATISTICS_STDDEV" sample="3">115.34685952339</Item>\n' +
  '  <Item name="STATISTICS_VALID_PERCENT" sample="3">100</Item>\n' +
  '</GDALMetadata>\n' +
  '\x00'

But at the moment geotiff.js is mixing dataset-level metadata with the metadata of last sample:

> image.getGDALMetadata()
{
  LOL: 'WUT',
  STATISTICS_MAXIMUM: '255',
  STATISTICS_MEAN: '73.349654800482',
  STATISTICS_MINIMUM: '0',
  STATISTICS_STDDEV: '115.34685952339',
  STATISTICS_VALID_PERCENT: '100'
}
DanielJDufour commented 2 years ago

@drnextgis , thanks for drafting that up. It wouldn't be difficult to update the code to reflect this test case. I'll let @constantinius weigh in before working up a PR.

constantinius commented 2 years ago

Sorry, have missed this. I think it is most useful for getGDALMetadata() with no argument to only return global metadata, not any band specific metadata. Feel free to draft a PR @DanielJDufour

DanielJDufour commented 2 years ago

I'm on it