문제

I am building a windows form application that works with PolyLineZ (ESRI Shapefile) data and rewrites outlying Z values. The minimum and maximum Z-values are defined by the user through the interface

Let's take the following as an example, let's say the minimum is 0 and the maximum is 10:

XY      Z
1,1     0
1,3     1
1,5     7
1,7     11*
1,10    10

The value with the 11 would need to be interpolated as it does not fall into the range defined by the user. This is a very simplified example obviously. Some PolyLines can be missing more values.

What I've done:

I've researched linear interpolation. Looked at example youtube videos, having a hard time wrapping my head around it.

What I need:

Code examples from either any language or an "English" explanation of the theory behind linear/bilinear/trilinear interpolation so that I can implement it into my program. My math skills aren't the greatest, so I have a hard time understanding wikipedias definition of it.

I'm also assuming that linear interpolation is what I need to research,

EDIT: Currently implementing the following, stop me if I'm wrong

I'm using what I think is Pythagorean Theory type approach. I haven't made it catch exceptions yet (ie, making sure the left point is actually left, make sure the list doesn't run out of bounds, etc), that can come later

internal static double calculateDistance(XYPoints a, XYPoints b)
{
    double xd = b.X - a.X;
    double yd = b.Y - a.Y;
    return Math.Sqrt(xd * xd + yd * yd);
}


for (var i = 0; i < polylinez.ZPoints.Count;i++)
{
    if (polylinez.ZPoints[i] > maxValue || (polylinez.ZPoints[i] < minValue))
    {
        //polylinez.ZPoints[i] = (((1 - polylinez.XYpoints[i].X) * polylinez.ZPoints[i - 1]) + (polylinez.XYpoints[i].X * polylinez.ZPoints[i + 1]));
        double prevdistance = calculateDistance(polylinez.XYpoints[i - 1], polylinez.XYpoints[i]);
        double nextdistance = calculateDistance(polylinez.XYpoints[i], polylinez.XYpoints[i + 1]);
        double fraction = prevdistance / nextdistance;
        double diffsBetweensZ = polylinez.ZPoints[i + 1] - polylinez.ZPoints[i - 1];
        Console.WriteLine(polylinez.ZPoints[i - 1] + (diffsBetweensZ * fraction));
    }
}

return polylinez;

It returns 9.12 as an answer for the above example table. This sounds about right to me. I checked my distance calculator method with sample data on the internet, and it seems to be doing the trick.

도움이 되었습니까?

해결책

First step, create a routine for calculating distances:

internal static double calculateDistance(XYPoints a, XYPoints b)
{
    double xd = b.X - a.X;
    double yd = b.Y - a.Y;
    return Math.Sqrt(xd * xd + yd * yd);
} 

I changed the variable names to something more logical (my variable names were different)

//get distance frpm previous point to point in question
double prevdistance = calculateDistance(prevXYpoint, currentXYPoint);
//get distance frpm point in question to the next point
double nextdistance = calculateDistance(currentXYPoint, nextXYPoint);
//generate a ratio
double fraction = prevdistance / (nextdistance + prevdistance);
//find out the difference between the two known points
double diffsBetweensZ = nextZpointValue - prevZpointValue;
//interpolate!
double newZvalue = (prevZpointValue + (diffsBetweensZ * fraction));

I checked this on several sets of data and it's the most accurate thing I can find... what blows my mind is that I couldn't find any existing code anywhere to do this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top