1

as shown in the below posted code, i am processing a tiff file. the img object in the inner-most promise contains some data in a form of key-value. the object img.fileDirectory.GDAL_METADATA contains the data posted below.

now my question is, as i would like to have access to STATISTICS_MAXIMUM,STATISTICS_MEAN,...etc. how can i have access to contents shown in GDALMetadata

GDALMetadata

<GDALMetadata>
  <Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
  <Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
  <Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
  <Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>

code:

response.on('close', async()=>{
    console.log('Retrieved all data');
    readFile("./test-1.tiff")
    .then((data)=>{
        dataAsArrayBuffer = data.buffer
        fromArrayBuffer(dataAsArrayBuffer)
        .then((geoTIFF)=>{
            geoTIFF.getImage()
            .then((img)=> {
                console.log(img.fileDirectory.GDAL_METADATA);//<==============
                console.log(img.getWidth(), img.getHeight(), img.getSamplesPerPixel());
            })
            .catch((e)=>console.log("img.errorMessage:",e))
        })
        .catch((e)=> console.log("geoTIFF.errorMessage:",e))
    })
    .catch((e)=> console.log("data.errorMessage:",e))
});
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

2

NodeJS domparsers

For example jsdom

const jsdom = require("jsdom");
const meta = `<GDALMetadata>
  <Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
  <Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
  <Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
  <Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;

const dom = new jsdom.JSDOM(meta);
const stats = [...dom.window.document.querySelectorAll("Item")]
 .map(item => ({[item.getAttribute('name')]:+item.textContent})); // convert the string to number using unary plus
console.log(stats);

Built-in Browser DOMParser

const meta = `<GDALMetadata>
  <Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
  <Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
  <Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
  <Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;

const parser = new DOMParser();
const doc1 = parser.parseFromString(meta, "application/xml");
const stats = [...doc1.querySelectorAll("Item")]
 .map(item => ({[item.getAttribute('name')]:+item.textContent})); // convert the string to number using unary plus
console.log(stats)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Parsing HTML/XML on server side with Node.js. can be done with parsers such as cheerio

Try this, it loads your string, finds all Item elements, loops them and gets text value. You can then construct results object according to your needs:

const cheerio = require('cheerio');

const inputData = `<GDALMetadata>
  <Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
  <Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
  <Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
  <Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;

const $ = cheerio.load(inputData, {
      xmlMode: true
    });

const result = {};

// select all Item elements, get text value for each
$('Item').each(function(i, elm) {
    console.log($(this).text());
// construct object attributeName:value
    result[$(this).attr('name')] = $(this).text();    
});

console.log(result);
traynor
  • 5,490
  • 3
  • 13
  • 23