Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bilinear Interpolation #11

Open
anoldfellow opened this issue Jul 22, 2022 · 0 comments
Open

Bilinear Interpolation #11

anoldfellow opened this issue Jul 22, 2022 · 0 comments

Comments

@anoldfellow
Copy link

anoldfellow commented Jul 22, 2022

Hello,

as I understand, the getElevation code always takes the sample next up-left to the given coordinate. This is no problem for flat areas. But what along a steep coast or in regions like the european Alps? I did some simple experiments with this code. As a result, I calculate the elevation from all four points around the point of interest. The resulting height simply is taken from a bilinear interpolation along x and y path. My code is as follows (not tested well, needs optimyzation):

public float GetElevationAtLatLon(double latitude, double longitude)
{
float[] h = new float[4];
double h12, h34, xratio, yratio;
Utils.GetXYFromLatLon(metadata, latitude, longitude, out int x, out int y);

if (x < 0) throw new Exception($"Error longitude: {longitude} not valid to [{metadata.PhysicalStartLon}, {metadata.PhysicalEndLon}] offsetX: {x}");
if (y < 0) throw new Exception($"Error latitude: {latitude} not valid to [{metadata.PhysicalStartLat}, {metadata.PhysicalEndLat}] offsetY: {y}");

h[0] = GetElevationAtPoint(x, y);
h[1] = GetElevationAtPoint(x + 1, y);
h[2] = GetElevationAtPoint(x, y - 1);
h[3] = GetElevationAtPoint(x + 1, y - 1);
// now do bilinear interpolation
xratio = (longitude - metadata.PhysicalStartLon - x*metadata.pixelSizeX) / metadata.pixelSizeX;
yratio = (latitude - metadata.PhysicalStartLat + (metadata.Height-y-1) * metadata.pixelSizeY) / metadata.pixelSizeY;
h12 = (double)h[0] + ((double)(h[1] - h[0])) * xratio;
h34 = (double)h[2] + ((double)(h[3] - h[2])) * xratio;
return (float) (h12 + (h34 - h12) * yratio);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant