首页 > 解决方案 > gdallocationinfo 的插值结果?

问题描述

这个:

gdallocationinfo -valonly -wgs84 file longitude latitude 

在解析的像素处提供文件中的值。

是否有gdal可以从相邻像素提供插值的函数?

例如,这些调用读取伦敦格林威治公园的海拔高度:

gdallocationinfo -wgs84 srtm_36_02.tif 0 51.4779
47

gdallocationinfo -wgs84 srtm_36_02.tif 0 51.4780
37

对于 0.0001° 的运动,海拔下降 10 米,大约向北 11 米。

文件中的像素相当粗糙——对应于地面约 80 米。我想获得更平滑的值而不是突然的大跳跃。

我目前使用的解决方法是使用此转换以四倍的分辨率重新采样源文件:

gdalwarp -ts 24004 24004 -r cubicspline srtm_36_02.tif srtm_36_02_cubicspline_x4.tiff

海拔高度请求与以前使用新文件相同的位置:

gdallocationinfo -wgs84 srtm_36_02_cubicspline_x4.tiff 0 51.4779
43

gdallocationinfo -wgs84 srtm_36_02_cubicspline_x4.tiff 0 51.4780
41

这要好得多,因为那只是一个 2 米的跳跃。

这种方法的缺点是需要几分钟才能生成更高分辨率的文件,但主要问题是文件大小从 69MB 变为 1.1GB。

我很惊讶重新采样不是直接选择gdallocationinfo,或者我可以使用另一种方法?

标签: gdal

解决方案


您可以编写 Python 或 Node.js 脚本来执行此操作,这将是 4 或 5 行代码,因为 GDALRasterIO可以动态重新采样。

Node.js 会这样:

const cellSize = 4; // This is your resampling factor

const gdal = require('gdal-async');
const ds = gdal.open('srtm_36_02.tif');

// Transform from WGS84 to raster coordinates
const xform = new gdal.CoordinateTransformation(
  gdal.SpatialReference.fromEPSG(4326), ds);
const coords = xform.transformPoint({x, y}); 

ds.bands.get(1).pixels.read(
   coords.x - cellSize/2,
   coords.y - cellSize/2,
   cellSize,
   cellSize,
   undefined, // Let GDAL allocate an output buffer
   {  buffer_width: 1, buffer_height: 1 } // of this size
);
console.log(data);

为简洁起见,当您靠近边缘时,我省略了坐标的夹紧,在这种情况下您必须减小尺寸。

(免责声明:我是 GDAL 的 Node.js 绑定的作者)


推荐阅读