finding a location on a line based using a point on it and a relative distance

758
6
07-13-2012 12:36 AM
Egan_LouisFajardo
New Contributor III
The problem I'm trying to solve is to find a point on a line given another point on the same line and a distance from the point. I've been looking on the various RouteTask examples but they mostly tackle the problem of finding a route given two points. Can anyone advise if RouteTask is the right approach for this kind of problem, and how do I do it?

Thanks!
0 Kudos
6 Replies
Egan_LouisFajardo
New Contributor III
Any help, please?
0 Kudos
JohnnyPenet
New Contributor
I did already somthing simular in a ArcMap extension. I show you the code below in C#. I think it can be translated into Javascript with the use of the help of the ArcGis javascript API. You will need the help of esri.geometry.getLength(point1, point2) to calculate the lengths. In the code I use also the angle of a line segment, this you will need to be written in javascript. All coordinates has to be in projected coordinates (not degrees).
The code below does a lot more than what you requires, so some simplicity is needed.
The most interesting function is CalcDxDy.
Sorry for the dutch comments.

Johnny


        /// <summary>
        /// Verleng een lijn (segment) met een eindpunt als start en een gegeven lengte.
        /// </summary>
        /// <param name="segmentFrom">Lijn segment</param>
        /// <param name="endPoint">eindpunt van het segment</param>
        /// <param name="length">lengte, negatief betekent inkorten</param>
        /// <returns></returns>
        private IPoint MovePoint(ISegment segmentFrom,
            IPoint endPoint, double length)
        {
            double angle=0;
            ILine line = new LineClass();
            line.SpatialReference = segmentFrom.SpatialReference;
            // Verlenging moet steeds gebeuren vanaf het tot punt
            if (segmentFrom.FromPoint.Compare(endPoint) == 0)
            {
                // Beweeg dit eind punt, verwissel de van en tot punt.
                line.ToPoint = segmentFrom.FromPoint;
                line.FromPoint = segmentFrom.ToPoint;
                angle = line.Angle;
                IPoint tmpPoint = line.ToPoint;
                CalcDxDy(ref tmpPoint, angle, length);
                return tmpPoint;
            }
            else if (segmentFrom.ToPoint.Compare(endPoint) == 0)
            {
                // Beweeg dit punt, geen omwisseling van punten
                line.ToPoint = segmentFrom.ToPoint;
                line.FromPoint = segmentFrom.FromPoint;
                angle = line.Angle;
                IPoint tmpPoint = line.ToPoint;
                CalcDxDy(ref tmpPoint, angle, length);
                return tmpPoint;
            }
            return null;
        }

        private void CalcDxDy(ref IPoint point, double angle,double length)
        {
            double degree = (angle * 180) / Math.PI;
            if (degree < 0)
            {
                degree += 180;
            }
            if (degree < 0 || degree >= 360)
            {
                // ongeldige situatie, doe niks
                return;
            }
            if (degree < 90)
            {
                point.X += length * Math.Cos(angle);
                point.Y += length * Math.Sin(angle);
            }
            else if (degree < 180)
            {
                point.X -= length * Math.Cos((Math.PI) -angle);
                point.Y += length * Math.Sin((Math.PI) - angle);
            }
            else if (degree < 270)
            {
                point.X -= length * Math.Cos(angle - (Math.PI));
                point.Y -= length * Math.Sin(angle - (Math.PI));
            }
            else
            {
                point.X = length * Math.Cos(2*Math.PI - angle);
                point.Y -= length * Math.Sin(2*Math.PI - angle);
            }
            return;

        }
0 Kudos
JohnnyPenet
New Contributor
I forgot the code that use the function to cut or extent a polyline.

Johnny


                                double lengte = lengteDialog.Lengte;
                                ISegmentCollection segmentCol =
                                    (ISegmentCollection)polyline;
                                while (Math.Abs(lengte) > 0.01)
                                {

                                    ISegment segment =
                                        segmentCol.get_Segment(segmentIndex);
                                    double segmentLengte = segment.Length;
                                    double verlenging = lengte;
                                    if (lengte > 0)
                                    {
                                        verlenging = lengte;
                                        lengte = 0;
                                    }
                                    else
                                    {
                                        if (Math.Abs(lengte) < segmentLengte)
                                        {
                                            verlenging = lengte;
                                            lengte = 0;
                                        }
                                        else
                                        {
                                            // Verwijder een segment uit de polyline
                                            segmentCol.RemoveSegments(segmentIndex, 1, false);
                                            if (segmentIndex > 0)
                                            {
                                                // Van hoger naar lager segmenten
                                                segmentIndex -= 1;
                                                endPoint =
                                                    segmentCol.get_Segment(segmentIndex).ToPoint;
                                            }
                                            else
                                            {
                                                // Van 0 naar hogere segmenten
                                                endPoint =
                                                    segmentCol.get_Segment(0).FromPoint;

                                            }
                                            lengte += segmentLengte;
                                            verlenging = 0;
                                        }
                                    }
                                    if (Math.Abs(verlenging) > 0.01)
                                    {
                                        IPoint newEndPoint =
                                            MovePoint(segment, endPoint, verlenging);
                                        MoveOnePoint(ref polyline, newEndPoint, endPoint);
                                    }
                                }
0 Kudos
nicogis
MVP Frequent Contributor
I don't know if in javascript is exposed this functionality but in arcobject you can use:

QueryPointAndDistance (you can get position of point from DistanceAlongCurve)
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/002m/002m00000116000000.htm

and QueryPoint (point from specified distance from the beginning of the curve)
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/002m/002m00000115000000.htm

then you expose it with soe rest and call from js
0 Kudos
Egan_LouisFajardo
New Contributor III
Thank you very much. I'll try these suggestions.
0 Kudos
LisaRuf
New Contributor
Hi there,

I believe I have similar requirements for my task and could probably use your thread to solve my issues except that I am new to ArcGIs and not familar at all with any type of coding so even with your explanations I am not sure of what to do.

Would you be able to detail the steps to follow in order to use your coding?

Many thanks in advance!
Lisa
0 Kudos