ThinkGeo.com    |     Documentation    |     Premium Support

Shortest distance

 


 

hello,


 


is there is any method to find a shortest distance between points. i.e there is a point A and  collection of feature(points) now i want to find shortest distance (not route) between the A and the collection's point. and it return the point (from collection) which is at the shortest distance to point A



 Hello Sarah,


 
You are use GetShortestLineTo Method, compare the distance from A to any of the point in the collection B, then return the shortest one. like:

            PointShape pointA = new PointShape();
            PointShape pointB = new PointShape();
            Collection<PointShape> collectionB = new Collection<PointShape>();
            collectionB.Add(pointB);
            PointShape shortestPoint;
            foreach (PointShape point in collectionB)
            {
                point.GetShortestLineTo(pointA, GeographyUnit.DecimalDegree);
                //compare and assign to shortestPoint
            }
            return shortestPoint

Or if the points in the collection can make a lineshape. you can use GetShortestLineTo with the lineshape directly.
 
Regards,
 
Gary
 

Thank you very much for you kind help gray.


 


vertex = poilayer.QueryTools.GetFeaturesIntersecting(check2.GetShape(), new string[0]);

            PointShape shortestpoint ;



            Collection<PointShape> collectionP = new Collection<PointShape>();



            foreach (Feature feature in vertex)

            {

                collectionP.Add(new PointShape(feature.WellKnownBinary));

            }



            foreach (PointShape p in collectionP)

            {

               double distance = p.GetShortestLineTo(goal, GeographyUnit.DecimalDegree);

            }


it gives error cannot convert multilineshape into double.


secondly you mentioned compare i donot get it. i know how to comapre in array to find smallestnumber  but how to compare geometry(point)



kindly help me to correct my code it gives error


 Operator '==' cannot be applied to operands of type 'double' and 'ThinkGeo.MapSuite.Core.PointShape'   

 


vertex = poilayer.QueryTools.GetFeaturesIntersecting(check2.GetShape(), new string[0]);

            PointShape spoint = null;

            List<double> distances = new List<double>();

            double distance = 0;

            Dictionary<double, PointShape> final = new Dictionary<double, PointShape>();

            Collection<PointShape> collectionP = new Collection<PointShape>();



            foreach (Feature feature in vertex)

            {

                collectionP.Add(new PointShape(feature.WellKnownBinary));

            }



            

            foreach (PointShape p in collectionP)

            {

               distance = p.GetDistanceTo(goal,GeographyUnit.DecimalDegree, DistanceUnit.Meter);



                final.Add(distance, p);

                distances.Add(distance);

            }

                

                distances.Sort();

                for (int i = 0; i < collectionP.Count; i++)

                {

                    if (distances == final)

                    {

                        spoint = final;

                    }

                }

 



Hi Sarah,


I think the following code could solve your original problem,



            double distance = double.MaxValue;

            foreach (var item in points)
            {
                double dis = a.GetDistanceTo(item, GeographyUnit.DecimalDegree, DistanceUnit.Meter);
                if (dis < distance)
                {
                    distance = dis;
                }
            }

    // a is the point A from your first post, and the points is the collection of points.


Hope it helps,


Edgar