1

I'd like to read an elevation value from a GeoTIFF that I have. It's already working, but I'd like to interpolate the values for better results.

I currently do so like this:

Dataset dataset = gdal.Open(pathToGeoTiffFile, 0);
String projection = dataset.GetProjection();
double[] transform = dataset.GetGeoTransform();

SpatialReference src = new SpatialReference();
src.SetWellKnownGeogCS("WGS84");
SpatialReference dst = new SpatialReference(projection);
CoordinateTransformation coordinateTransformation = new CoordinateTransformation(src, dst);


double[] xy = coordinateTransformation.TransformPoint(lng, lat);

float[] result = new float[1];
int[] band = {1};

int x = (int)(((xy[0] - transform[0]) / transform[1]) - 0.5);
int y = (int)(((xy[1] - transform[3]) / transform[5]) - 0.5);

dataset.ReadRaster(x, y, 1, 1, 1, 1, 6, result, band);

double elevation = result[0];

This basically works, but there is no interpolation. So basically a coordinate gets mapped to a pixel (no matter if it's close to the next pixel or not) and then I take that value. But I'd prefer to interpolate the value. So if I am on. f.e the bottom of a pixel and the next pixel underneath has a different elevation, the value that I return should be interpolated.

I could of course calculate that on my own, but I was wondering if there is a function that I am missing?

1 Answer 1

0

There's Inverse Distance Weighting, which I tripped over recently:

https://pro.arcgis.com/en/pro-app/latest/tool-reference/3d-analyst/how-idw-works.htm

Basically, you take a set of points p with known elevations (say, the four pixel centers surrounding your position) and calculate:

A = Sum of p.elevation / distance(pos, p) for all p B = Sum of 1 / distance(pos, p) for all p

Interpolated elevation = A/B

You can change the weighting by raising distance(pos, p) to a power.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.