我试图从WMS加载一个图像到mapnik,但是我得到了一个错误的Unhandled rejection Error: image_reader: can't determine type from input data。精简后的代码是
const request = require('request-promise');
const mapnik = require('mapnik');
request(`${wmsUrl}/GetMap`, {
qs: {
bbox: '-90,32,-89,33',
format: 'image/png',
height: 200,
layers: '5',
request: 'GetMap',
'srs(crs)': 'EPSG:4326,
styles: 'default',
version: '1.1',
width: 200,
},
}).then(res => {
const buffer = new Buffer(res);
return mapnik.Image.fromBytesSync(buffer); // This is the error line
});我已经手动运行了请求,它工作了,我已经检查了缓冲区,它看起来很好(即在开始时有'PNG‘)。我不确定还能尝试什么。
发布于 2016-06-05 15:17:26
如果其他人遇到类似的情况,问题是request (在本例中是request-promise)假定响应体应该是一个字符串,并隐式地对其执行toString。要解决这个问题,请求应该是
request(url, {
qs: { ... },
encoding: null,
}).then({ ... });点击此处查看更多详情:https://github.com/request/request#requestoptions-callback
发布于 2021-08-29 16:51:07
我通过同时指定responseType和Content-Type头解决了这个问题:
return axios.get(url, {
responseType: 'arraybuffer',
headers: {
'Content-Type': 'image/png',
},
});https://stackoverflow.com/questions/37632722
复制相似问题