skip to Main Content

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))
});

2

Answers


  1. Use a 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)
    Login or Signup to reply.
  2. 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 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search