UnhandledPromiseRejectionWarning: RangeError: Invalid field type

huangapple go评论61阅读模式
英文:

UnhandledPromiseRejectionWarning: RangeError: Invalid field type

问题

您的代码中似乎存在一些问题,特别是在尝试获取Tiff图像时出现了错误。以下是您提供的代码部分的翻译:

// 使用`geotiff.js`库来处理Tiff文件。请参考官方文档:https://github.com/geotiffjs/geotiff.js

// 在官方文档的“Example Usage”部分,您可以看到以下代码:
// 从URL、ArrayBuffer或Blob加载数据块,以便进行处理:
const tiff = await fromArrayBuffer(...);
const image = await tiff.getImage(); // 默认情况下,读取第一个图像。

// 在我下面发布的代码中,我遵循了相同的模式,但在`.getImage()`处应用程序崩溃,生成了下面发布的错误。

// 我还发布了`.log(data)`和`.log(arrBuf)`的输出。

// 请帮助我学习如何正确地使用`.getImage`获取Tiff图像

// data.output
data: ArrayBuffer {
  [Uint8Contents]: <49 49 2a 00 08 00 00 00 ... 91625 more bytes>,
  byteLength: 91725
}

// arrBuf.output
arrBuf: GeoTIFF {
  source: ArrayBufferSource {
    arrayBuffer: ArrayBuffer {
      [Uint8Contents]: <49 49 2a 00 08 00 00 00 ... 91625 more bytes>,
      byteLength: 91725
    }
  },
  littleEndian: true,
  bigTiff: false,
  firstIFDOffset: 8,
  cache: false,
  ifdRequests: [],
  ghostValues: null
}

// error
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)

// 代码部分
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("未从服务器获取OK。代码:", response.statusCode);
    console.error("未从服务器获取OK。消息:", 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(); // <=== 生成上面发布的错误
      console.log("tiff:", tiff);
    })
    // ...其他代码...
  }
  // ...其他回调代码...
}

请检查代码中的错误,特别是关于Tiff图像的处理部分,以确保正确获取图像并避免出现错误。

英文:

i am using geotiff.js lib to perform some processing on tiff files. referring to the official doc of the aforementioned lib here https://github.com/geotiffjs/geotiff.js

you can see in section Example Usage the following code:

// 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.

in my code posted below, i followed the same pattern, but the app crashs at .getImage() generating the error posted below.

i also posted the output of either of .log(data) and .log(arrBuf)

please help me to learn how to correctly get the tiff-image using .getImage

data.output:

data: ArrayBuffer {
[Uint8Contents]: &lt;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&gt;,
byteLength: 91725
}

arrBuf.output:

arrBuf: GeoTIFF {
source: ArrayBufferSource {
arrayBuffer: ArrayBuffer {
[Uint8Contents]: &lt;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&gt;,
byteLength: 91725
}
},
littleEndian: true,
bigTiff: false,
firstIFDOffset: 8,
cache: false,
ifdRequests: [],
ghostValues: null
}

error:

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&gt;

code:

var options = {
protocol:&#39;https:&#39;,
host: envVars.END_POINT_HOST_JKI,
path: encodeURI(envVars.END_POINT_PATH_RASTER_IN_POLYGON + envVars.WKT_EPSG4326 + envVars.END_POINT_PATH_RESOLUTION),
method:&#39;GET&#39;,
headers: {
&#39;Content-Type&#39;: &#39;application/json&#39;
}
};
var callback = (response)=&gt;{
if (response.statusCode !== 200) {
console.error(&quot;Did not get an OK from the server. Code:&quot;,response.statusCode);
console.error(&quot;Did not get an OK from the server. Msg:&quot;,response.statusMessage);
res.resume();
return;
}
var data = &#39;&#39;;
response.on(&#39;data&#39;, (chunk)=&gt;{
data += chunk;
});
response.on(&#39;close&#39;, async()=&gt;{
fs.writeFileSync(&quot;test.tiff&quot;, data)
fs.readFile(&quot;test.tiff&quot;,async (err,data) =&gt; {
console.log(&quot;data:&quot;,data.buffer)
const arrBuf = await fromArrayBuffer(data.buffer)
console.log(&quot;arrBuf:&quot;,arrBuf);
const tiff = await arrBuf.getImage()&lt;=== generated the error posted above
console.log(&quot;tiff:&quot;,tiff);
})
...
...
}

答案1

得分: 0

问题出在如何构建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);
        })
    ... 
    ...
    })
};

请注意,上述代码中已经按照您的要求将HTML实体编码转换回相应的字符,以使其可读。

英文:

the problem was lurking in the way of how the tiff-file data was constructed.i mean,in response.on(data,(chunk)) it should look like the following because we are constructing a tiff which is buffered contents:

response.on(&#39;data&#39;, (chunk)=&gt;{
data.push(Buffer.from(chunk,&#39;binary&#39;))
});

so the final code should look like this:

var options = {
protocol:&#39;https:&#39;,
host: envVars.END_POINT_HOST_JKI,
path: encodeURI(envVars.END_POINT_PATH_RASTER_IN_POLYGON + 
envVars.WKT_EPSG4326 + envVars.END_POINT_PATH_RESOLUTION),
method:&#39;GET&#39;,
headers: {
&#39;Content-Type&#39;: &#39;application/json&#39;
}
};
var callback = (response)=&gt;{
if (response.statusCode !== 200) {
console.error(&quot;Did not get an OK from the server. 
Code:&quot;,response.statusCode);
console.error(&quot;Did not get an OK from the server. Msg:&quot;,response.statusMessage);
res.resume();
return;
}
var data = [];
response.on(&#39;data&#39;, (chunk)=&gt;{
data.push(Buffer.from(chunk,&#39;binary&#39;))
});
response.on(&#39;close&#39;, async()=&gt;{
const bufferedData = Buffer.concat(data)
fs.writeFileSync(&quot;test.tiff&quot;, bufferedData)
fs.readFile(&quot;test.tiff&quot;,async (err,contents) =&gt; {
const arrBuffer = await fromArrayBuffer(contents.buffer)
console.log(&quot;arrBuffer:&quot;,arrBuffer)
const tiff = await arrBuffer.getImage()
console.log(&quot;tiff:&quot;,tiff);
})
... 
...
})

huangapple
  • 本文由 发表于 2023年6月26日 19:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定