ThinkGeo.com    |     Documentation    |     Premium Support

Find percent along the line?

I know that there is a function (GetPointOnALine) that I can pass in a percentage to say how far along the line I want to get a point from. Is there something that is the reverse of that? I have a point, but I want to know what percentage down the line it is. Any ideas?

Hi Clay, 
  
 We don’t have a exsiting API for that. 
  
 You can try modifing this code as below for your requirement. 
  
 
PointShape yourPoint = new PointShape();
            LineShape yourLine = new LineShape();

            PointShape yourPointInYourLine = yourLine.GetClosestPointTo(yourPoint, GeographyUnit.DecimalDegree);
            double totalLength = yourLine.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter);
            double currentLength = 0;

            for (int i = 1; i < yourLine.Vertices.Count; i++)
            {
                LineShape tmpLine = new LineShape(new Collection<Vertex>() { yourLine.Vertices[i - 1], yourLine.Vertices[i] });

                PointShape tmpPoint = tmpLine.GetClosestPointTo(yourPoint, GeographyUnit.DecimalDegree);

                if (Math.Round(tmpPoint.X, 6) == Math.Round(yourPointInYourLine.X, 6) && Math.Round(tmpPoint.Y, 6) == Math.Round(yourPointInYourLine.Y, 6))
                {
                    tmpLine = new LineShape(new Collection<Vertex>() { yourLine.Vertices[i - 1], new Vertex(tmpPoint) });
                    currentLength += tmpLine.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter);
                    break;
                }
                else
                {
                    currentLength += tmpLine.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter);
                }
            }

            double ratio = currentLength / totalLength;
 
 
  
 Wish that’s helpful. 
  
 Regards, 
  
 Don