8

This:

gdallocationinfo -valonly -wgs84 file longitude latitude 

provides the value from the file at the resolved pixel.

Is there a gdal function that can provide an interpolated value from the neighbouring pixels?

For example these calls read elevation from Greenwich Park in London:

gdallocationinfo -wgs84 srtm_36_02.tif 0 51.4779
47

gdallocationinfo -wgs84 srtm_36_02.tif 0 51.4780
37

That's a 10 metre drop in elevation for a movement of 0.0001°, about 11 metres north.

The pixels in the file are quite coarse - corresponding to about 80 metres on the ground. I want to get smoother values out rather than sudden big jumps.

The workaround I'm currently using is to resample the source file at four times the resolution using this transformation:

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

The elevations requests for the same locations as previously using the new file:

gdallocationinfo -wgs84 srtm_36_02_cubicspline_x4.tiff 0 51.4779
43

gdallocationinfo -wgs84 srtm_36_02_cubicspline_x4.tiff 0 51.4780
41

which is much better as that is only a 2 metre jump.

The downside of this approach is that it takes a few minutes to generate the higher resolution file, but the main problem is that the file size goes from 69MB to 1.1GB.

I'm surprised that resampling is not a direct option to gdallocationinfo, or maybe there is another approach I can use?

Simon Nuttall
  • 394
  • 1
  • 3
  • 12

2 Answers2

0

You can write a Python or a Node.js script to do this, it would be 4 or 5 lines of code as GDAL's RasterIO can resample on the fly.

Node.js would go like this:

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);

For brevity I have omitted the clamping of the coordinates when you are near the edges, you have to reduce the size in this case.

(disclaimer: I am the author of the Node.js bindings for GDAL)

mmomtchev
  • 2,497
  • 1
  • 8
  • 23
-1

You may try to get a 1-pixel raster from gdalwarp. This would use all the warp resample machinery with minimal impact on ram/cpu/disk. I am using this (inside a Python program, since the calculations may be a bit too complex for a shell script). It does work.

rapto
  • 405
  • 3
  • 15