NodeJS 如何使用geotiff.js读取tiff图像

to94eoyn  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(499)

我正在使用geotiff.js库对tiff文件执行一些处理。参考上述库的官方文档https://github.com/geotiffjs/geotiff.js
你可以在Example Usage部分看到下面的代码:

// Load our data tile from url, arraybuffer, or blob, so we can work with it:
const tiff = await fromArrayBuffer(...);
const image = await tiff.getImage(); // by default, the first image is read.

在我下面发布的代码中,我遵循了相同的模式,但应用程序在.getImage()处崩溃,生成下面发布的错误。
我还发布了.log(data).log(arrBuf)的输出
请帮助我学习如何使用.getImage正确获取tiff-image

data.output

data: ArrayBuffer {
  [Uint8Contents]: <49 49 2a 00 08 00 00 00 13 00 00 01 03 00 01 00 00 00 ef bf bd 00 00 00 01 01 03 00 01 00 00 00 ef bf bd 00 00 00 02 01 03 00 01 00 00 00 20 00 00 00 03 01 03 00 01 00 00 00 05 00 00 00 06 01 03 00 01 00 00 00 01 00 00 00 11 01 04 00 0a 00 00 00 1a 01 00 00 15 01 03 00 01 00 00 00 01 00 00 00 16 01
... 91625 more bytes>,
  byteLength: 91725
}

arrBuf.output

arrBuf: GeoTIFF {
  source: ArrayBufferSource {
    arrayBuffer: ArrayBuffer {
      [Uint8Contents]: <49 49 2a 00 08 00 00 00 13 00 00 01 03 00 01 00 00 00 ef bf bd 00 00 00 01 01 03 00 01 00 00 00 ef bf bd 00 00 00 02 01 03 00 01 00 00 00 20 00 00 00 03 01 03 00 01 00 00 00 05 00 00 00 06 01 03 00 01 00 00 00 01 00 00 00 11 01 04 00 0a 00 00 00 1a 01 00 00 15 01 03 00 01 00 00 00 01 00 00 00 16 01 ... 91625 more bytes>,
      byteLength: 91725
    }
  },
  littleEndian: true,
  bigTiff: false,
  firstIFDOffset: 8,
  cache: false,
  ifdRequests: [],
  ghostValues: null
}

错误

file:///C:/Users/xx/projects/nodejs/http/processTIFF-0/node_modules/geotiff/dist-module/geotiff.js:67
      throw new RangeError(`Invalid field type: ${fieldType}`);
            ^

RangeError: Invalid field type: 257
    at getFieldTypeLength (file:///C:/Users/xx/projects/nodejs/http/processTIFF-0/node_modules/geotiff/dist-module/geotiff.js:67:13)
    at GeoTIFF.parseFileDirectoryAt (file:///C:/Users/xx/projects/nodejs/http/processTIFF-0/node_modules/geotiff/dist-module/geotiff.js:386:31)
    at async GeoTIFF.getImageCount (file:///C:/Users/xx/projects/nodejs/http/processTIFF-0/node_modules/geotiff/dist-module/geotiff.js:492:9)
PS C:\Users\xx\projects\nodejs\http\processTIFF-0>

编码

var options = {
    protocol:'https:',
    host: envVars.END_POINT_HOST_JKI,
    path: encodeURI(envVars.END_POINT_PATH_RASTER_IN_POLYGON + envVars.WKT_EPSG4326 + envVars.END_POINT_PATH_RESOLUTION),
    method:'GET',
    headers: {
        'Content-Type': 'application/json'
      }
};
  
var callback = (response)=>{
    if (response.statusCode !== 200) {
        console.error("Did not get an OK from the server. Code:",response.statusCode);
        console.error("Did not get an OK from the server. Msg:",response.statusMessage);
        res.resume();
        return;
      }
    var data = '';
    response.on('data', (chunk)=>{
        data += chunk;
    });
    
response.on('close', async()=>{

    fs.writeFileSync("test.tiff", data)

    fs.readFile("test.tiff",async (err,data) => {
        console.log("data:",data.buffer)
        const arrBuf = await fromArrayBuffer(data.buffer)
        console.log("arrBuf:",arrBuf);
        const tiff = await arrBuf.getImage()<=== generated the error posted above
        console.log("tiff:",tiff);
    })
    ...
    ...
}
xlpyo6sf

xlpyo6sf1#

问题是潜伏在tiff文件数据的构造方式中.我的意思是,在response.on(data,(chunk))中,它应该看起来像下面这样,因为我们正在构造一个tiff,它是缓冲的内容:

response.on('data', (chunk)=>{
    data.push(Buffer.from(chunk,'binary'))
});

所以最终的代码应该是这样的:

var options = {
protocol:'https:',
host: envVars.END_POINT_HOST_JKI,
path: encodeURI(envVars.END_POINT_PATH_RASTER_IN_POLYGON + 
envVars.WKT_EPSG4326 + envVars.END_POINT_PATH_RESOLUTION),
method:'GET',
headers: {
    'Content-Type': 'application/json'
  }
};

var callback = (response)=>{
    if (response.statusCode !== 200) {
        console.error("Did not get an OK from the server. 
Code:",response.statusCode);
    console.error("Did not get an OK from the server. Msg:",response.statusMessage);
    res.resume();
    return;
  }
var data = [];
response.on('data', (chunk)=>{
    data.push(Buffer.from(chunk,'binary'))
});

response.on('close', async()=>{
    const bufferedData = Buffer.concat(data)
    fs.writeFileSync("test.tiff", bufferedData)

    fs.readFile("test.tiff",async (err,contents) => {
        const arrBuffer = await fromArrayBuffer(contents.buffer)
        console.log("arrBuffer:",arrBuffer)
        const tiff = await arrBuffer.getImage()
        console.log("tiff:",tiff);
    })
... 
...
})

相关问题