mmomtchev / node-gdal-async

Node.js bindings for GDAL (Geospatial Data Abstraction Library) with full async support
https://mmomtchev.github.io/node-gdal-async/
Apache License 2.0
129 stars 26 forks source link

Getting GeoTIFF Metadata #95

Open ivansalacceo opened 1 year ago

ivansalacceo commented 1 year ago

Hi, I'm planning to validate a COG file. Is it possible to get the GeoTIFF metadata like IFD_OFFSET and BLOCK_OFFSET_0_0 ? The python equivalent will be something like this overviewBand.GetMetadataItem('IFD_OFFSET', 'TIFF') Source: https://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/validate_cloud_optimized_geotiff.py

Thanks for this nice library porting.

mmomtchev commented 1 year ago

You can use ds.getMetadata('TIFF')['IFD_OFFSET'] and ds.bands.get(1).getMetadata('TIFF')['IFD_OFFSET'] to get IFD_OFFSET from the TIFF metadata domain

ivansalacceo commented 1 year ago

Tried that, but get undefined instead. Code:

const dataset = await gdal.openAsync(localFilePath);
await testCogeo(dataset)

async function testCogeo(dataset) {
  const datasetOffset = dataset.getMetadata('TIFF')['IFD_OFFSET'];
  const bandOffset = dataset.bands.get(1).getMetadata('TIFF')['IFD_OFFSET'];
  console.log('datasetOffset:', datasetOffset);
  console.log('bandOffset:', bandOffset);

  const info = await gdal.infoAsync(dataset);
  console.log('info:', info);
}

will yield undefined for both IFD_OFFSET, however the gdal.info() shows just fine.

>  datasetOffset: undefined
>  bandOffset: undefined

>  info: Driver: GTiff/GeoTIFF
>  Files: /tmp/test.tiff
>  Size is 321, 227
>  Coordinate System is:
>  GEOGCRS["WGS 84",
>      ENSEMBLE["World Geodetic System 1984 ensemble",
>          MEMBER["World Geodetic System 1984 (Transit)"],
>          MEMBER["World Geodetic System 1984 (G730)"],
>          MEMBER["World Geodetic System 1984 (G873)"],
>          MEMBER["World Geodetic System 1984 (G1150)"],
>          MEMBER["World Geodetic System 1984 (G1674)"],
>          MEMBER["World Geodetic System 1984 (G1762)"],
>          MEMBER["World Geodetic System 1984 (G2139)"],
>          ELLIPSOID["WGS 84",6378137,298.257223563,
>              LENGTHUNIT["metre",1]],
>          ENSEMBLEACCURACY[2.0]],
>      PRIMEM["Greenwich",0,
>          ANGLEUNIT["degree",0.0174532925199433]],
>      CS[ellipsoidal,2],
>          AXIS["geodetic latitude (Lat)",north,
>              ORDER[1],
>              ANGLEUNIT["degree",0.0174532925199433]],
>          AXIS["geodetic longitude (Lon)",east,
>              ORDER[2],
>              ANGLEUNIT["degree",0.0174532925199433]],
>      USAGE[
>          SCOPE["Horizontal component of 3D system."],
>          AREA["World."],
>          BBOX[-90,-180,90,180]],
>      ID["EPSG",4326]]
>  Data axis to CRS axis mapping: 2,1
>  Origin = (106.048680764617032,-2.952973618824988)
>  Pixel Size = (0.000595566901350,-0.000595566901350)
>  Metadata:
>    AREA_OR_POINT=Area
>    DataType=Generic
>  Image Structure Metadata:
>    COMPRESSION=LZW
>    INTERLEAVE=BAND
>    LAYOUT=COG
>  Corner Coordinates:
>  Upper Left  ( 106.0486808,  -1.9529736) (104d 2'55.25"E,  1d57'10.71"S)
>  Lower Left  ( 106.0486808,  -2.0881673) (104d 2'55.25"E,  2d 5'17.40"S)
>  Upper Right ( 106.2398577,  -1.9529736) (104d14'23.49"E,  1d57'10.71"S)
>  Lower Right ( 106.2398577,  -2.0881673) (104d14'23.49"E,  2d 5'17.40"S)
>  Center      ( 106.1442693,  -2.0205705) (104d 8'39.37"E,  2d 1'14.05"S)
>  Band 1 Block=512x512 Type=Float32, ColorInterp=Gray
>    Min=0.409 Max=7.366
>    Minimum=0.409, Maximum=7.366, Mean=4.893, StdDev=1.531
>    NoData Value=-3.402823e+38
>    Overviews: 161x114
>    Metadata:
>      RepresentationType=ATHEMATIC
>      STATISTICS_COVARIANCES=2.3444229762068
>      STATISTICS_MAXIMUM=7.3657851219177
>      STATISTICS_MEAN=4.8931533114601
>      STATISTICS_MEDIAN=5.265252
>      STATISTICS_MINIMUM=0.40947422385216
>      STATISTICS_SKIPFACTORX=1
>      STATISTICS_SKIPFACTORY=1
>      STATISTICS_STDDEV=1.5311508665729
>

Gdal-async version: 3.7.0, node version: nvm v16.20.0, running the node using m1 macbook, but switched to intel-based arch.

Any further hints on the undefined metadata?

mmomtchev commented 1 year ago

Alas, it seems that the TIFF domain items must be retrieved one by one - GDAL does not return the full list as it it is the case with the other domains. Currently, there is no way to retrieve individual metadata lines from GDAL, only the full object.