private static int GetIDWNeighborsBrute(InterpolationPoint InCellPoint, ref List <InterpolationPoint> InPointCache, out List <InterpolationPoint> OutNeighbors, IDWNeighborhoodType NeighborhoodType, int NeighborhoodCount, double NeighborhoodDistance, string ProjUnits)
        {
            SortInterpolationPointsByDistanceBrute(InCellPoint, ref InPointCache, ProjUnits);

            OutNeighbors = new List <InterpolationPoint>();
            int numPts = 0;

            if (NeighborhoodType == IDWNeighborhoodType.Variable) //Taking NeighborhoodCount number of nearest points
            {
                if (NeighborhoodDistance == -1)                   //Then take NeighborhoodCount number of nearest points
                {
                    foreach (InterpolationPoint npoint in InPointCache)
                    {
                        OutNeighbors.Add(new InterpolationPoint(npoint.X, npoint.Y, npoint.Value, npoint.Distance));
                        numPts++;
                        if (numPts >= NeighborhoodCount)
                        {
                            break;
                        }
                    }
                }
                else //Take NeighborhoodCount of nearest points that are a maximum of NeighborhoodDistance away
                {
                    foreach (InterpolationPoint npoint in InPointCache)
                    {
                        if (npoint.Distance <= NeighborhoodDistance)
                        {
                            OutNeighbors.Add(new InterpolationPoint(npoint.X, npoint.Y, npoint.Value, npoint.Distance));
                            numPts++;
                            if (numPts >= NeighborhoodCount)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else if (NeighborhoodType == IDWNeighborhoodType.Fixed) //Taking all points in fixed distance
            {
                foreach (InterpolationPoint npoint in InPointCache)
                {
                    if (npoint.Distance <= NeighborhoodDistance)
                    {
                        OutNeighbors.Add(new InterpolationPoint(npoint.X, npoint.Y, npoint.Value, npoint.Distance));
                    }
                }
                if (OutNeighbors.Count < NeighborhoodCount) //Test for minimum number of points found
                {
                    return(-1);
                    //Error
                }
            }
            else
            {
                return(-1);
                //Error
            }
            return(0);
        }
Exemple #2
0
        public InterpolationSet(int previousBusinessDays, double previousInterestRate,
                                int nextBusinessDays, double nextInterestRate,
                                int targetMaturityDays)
        {
            var previousPoint = new InterpolationPoint(previousBusinessDays, previousInterestRate);
            var nextPoint     = new InterpolationPoint(nextBusinessDays, nextInterestRate);

            Initialize(previousPoint, nextPoint, targetMaturityDays);
        }
        private static void SortInterpolationPointsByDistanceBrute(InterpolationPoint InCellPoint, ref List <InterpolationPoint> InPointCache, string ProjUnits)
        {
            foreach (InterpolationPoint cpoint in InPointCache)
            {
                cpoint.Distance = SpatialOperations.Distance(InCellPoint.X, InCellPoint.Y, cpoint.X, cpoint.Y, ProjUnits);
            }
            InterpolationPointSorter sorter = new InterpolationPointSorter();

            InPointCache.Sort(sorter);
        }
        private static void CachePointsBrute(string InPointsPath, int InValueFieldIndex, out List <InterpolationPoint> PointsCache, out string Projection, out string ProjectionUnits, out MapWinGIS.Extents PointsExtents, MapWinGIS.ICallback callback)
        {
            int newperc = 0, oldperc = 0;

            MapWinGIS.Shapefile points = new MapWinGIS.Shapefile();
            points.Open(InPointsPath, null);

            PointsExtents = points.Extents;
            Projection    = points.Projection;
            if (Projection != null)
            {
                ProjectionUnits = Projection.Substring(Projection.IndexOf("units=") + 6);
                ProjectionUnits = ProjectionUnits.Substring(0, ProjectionUnits.IndexOf("+")).Trim();
            }
            else
            {
                double tmpX   = points.Extents.xMax;
                string tmpstr = Math.Floor(tmpX).ToString();

                if (tmpstr.Length > 4)
                {
                    ProjectionUnits = "";
                }
                else
                {
                    ProjectionUnits = "lat/long";
                }
            }

            PointsCache = new List <InterpolationPoint>();
            InterpolationPoint pt;

            MapWinGIS.Point currpt;
            int             ns         = points.NumShapes;

            for (int i = 0; i < ns; i++)
            {
                newperc = Convert.ToInt32((Convert.ToDouble(i) / Convert.ToDouble(ns)) * 100);
                if ((newperc > oldperc))
                {
                    if (callback != null)
                    {
                        callback.Progress("Status", newperc, "IDW Caching " + i.ToString());
                    }
                    oldperc = newperc;
                }

                currpt = points.get_Shape(i).get_Point(0);

                pt = new InterpolationPoint(currpt.x, currpt.y, double.Parse(points.get_CellValue(InValueFieldIndex, i).ToString()), 0);
                PointsCache.Add(pt);
            }
            points.Close();
        }
        private InterpolationSet TransformEttjToInterpolationSet(Ettj ettj, int targetMaturityDays)
        {
            var rates = ettj.Rates.OrderBy(x => x.BusinessDays).ToList();

            var firstRate  = rates[0];
            var secondRate = rates[1];

            var firstInterpolationPoint  = new InterpolationPoint(firstRate.BusinessDays, firstRate.RateValue);
            var secondInterpolationPoint = new InterpolationPoint(secondRate.BusinessDays, secondRate.RateValue);

            return(new InterpolationSet(firstInterpolationPoint, secondInterpolationPoint, targetMaturityDays));
        }
        private static InterpolationPoint CreatePoint(IReadOnlyList <PathPoint> path, int index)
        {
            var pp1 = path[index];

            // interpolate between two path points
            var pp2  = path[(index + 1) % path.Count];
            var pp3  = path[(index + 2) % path.Count];
            var next = new InterpolationPoint(pp2, pp3);

            // line segment direction to the next point
            return(new InterpolationPoint(pp1, pp2, next));
        }
 private static double GetIDWeightBrute(InterpolationPoint CellPoint, InterpolationPoint TestPoint, String ProjUnits, double Power)
 {
     return(1 / Math.Pow(SpatialOperations.Distance(CellPoint.X, CellPoint.Y, TestPoint.X, TestPoint.Y, ProjUnits), Power));
 }
        /// <summary>
        /// Inverse Distance Weighting Interpolation
        /// </summary>
        /// <param name="InPointsPath">Input point shapefile path to interpolate</param>
        /// <param name="InValueFieldIndex">Input field index where interpolation value is stored</param>
        /// <param name="OutGridPath">Output grid path where interpolation is stored</param>
        /// <param name="CellSize">Cell Size for output grid. Default lower of points extent height or width divided by 250</param>
        /// <param name="Power">Power variable for IDW algorithm. 0 lt p lt 1 will give sharp changes. >1 will give smoother. Default 2.</param>
        /// <param name="NeighborhoodType">Variable is a variable number of nearest points. Fixed is all points in a fixed distance</param>
        /// <param name="NeighborhoodCount">Variable Count is either number of nearest points to use. Fixed Count is minimum points needed to find valid interpolation</param>
        /// <param name="NeighborhoodDistance">Variable Distance is a maximum distance of nearest points. Fixed Distance is distance to use to select points</param>
        /// <param name="callback">A callback for progress information</param>
        public static void IDWBrute(string InPointsPath, int InValueFieldIndex, string OutGridPath, double CellSize, double Power, IDWNeighborhoodType NeighborhoodType, int NeighborhoodCount, double NeighborhoodDistance, MapWinGIS.ICallback callback)
        {
            int newperc = 0, oldperc = 0;

            List <InterpolationPoint> pointsCache;
            string proj, projUnits;

            MapWinGIS.Extents pointsExtents;
            CachePointsBrute(InPointsPath, InValueFieldIndex, out pointsCache, out proj, out projUnits, out pointsExtents, callback);

            MapWinGIS.Grid outGrid;
            DataManagement.DeleteGrid(ref OutGridPath);
            double NoDataValue = -32768;

            CreateGridFromExtents(pointsExtents, CellSize, proj, NoDataValue, OutGridPath, out outGrid);

            //Cycle grid and find interpolated value for each cell
            List <InterpolationPoint> neighbors;
            InterpolationPoint        cellPoint;
            int    nr = outGrid.Header.NumberRows;
            int    nc = outGrid.Header.NumberCols;
            double sumWeight, sumWeightAndVal;

            newperc = 0;
            oldperc = 0;
            for (int row = 0; row < nr; row++)
            {
                newperc = Convert.ToInt32((Convert.ToDouble(row) / Convert.ToDouble(nr)) * 100);
                if (callback != null)
                {
                    callback.Progress("Status", newperc, "IDW Row " + row);
                }

                newperc = 0;
                oldperc = 0;
                for (int col = 0; col < nc; col++)
                {
                    newperc = Convert.ToInt32((Convert.ToDouble(col) / Convert.ToDouble(nc)) * 100);
                    if ((newperc > oldperc))
                    {
                        if (callback != null)
                        {
                            callback.Progress("Status", newperc, "IDW Row " + row.ToString() + "  Col " + col.ToString());
                        }
                        oldperc = newperc;
                    }

                    cellPoint = new InterpolationPoint();
                    outGrid.CellToProj(col, row, out cellPoint.X, out cellPoint.Y);
                    GetIDWNeighborsBrute(cellPoint, ref pointsCache, out neighbors, NeighborhoodType, NeighborhoodCount, NeighborhoodDistance, projUnits);

                    sumWeightAndVal = 0;
                    sumWeight       = 0;

                    foreach (InterpolationPoint npoint in neighbors)
                    {
                        sumWeightAndVal += GetIDWeightBrute(cellPoint, npoint, projUnits, Power) * npoint.Value;
                        sumWeight       += GetIDWeightBrute(cellPoint, npoint, projUnits, Power);
                    }

                    outGrid.set_Value(col, row, (sumWeightAndVal / sumWeight));
                }
            }

            outGrid.Save(OutGridPath, MapWinGIS.GridFileType.UseExtension, null);
            outGrid.Close();
        }
Exemple #9
0
 private void Initialize(InterpolationPoint previousPoint, InterpolationPoint nextPoint, int targetMaturityDays)
 {
     PreviousPoint      = previousPoint;
     NextPoint          = nextPoint;
     TargetMaturityDays = targetMaturityDays;
 }
Exemple #10
0
 public InterpolationSet(InterpolationPoint previousPoint, InterpolationPoint nextPoint, int targetMaturityDays)
 {
     Initialize(previousPoint, nextPoint, targetMaturityDays);
 }