英文:
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]: <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
}
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>
code:
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);
})
...
...
}
答案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('data', (chunk)=>{
data.push(Buffer.from(chunk,'binary'))
});
so the final code should look like this:
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);
})
...
...
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论