Exemplo n.º 1
0
        /// <summary>
        /// Calculate the route for a set of coordinates
        /// </summary>
        public static OSMResponse CalculateRoute(String osrmAddress, IEnumerable <Coordinate> coords)
        {
            // Get initial response
            var response = NetworkUtilities.JsonRequest <OSMResponse>(
                new Uri(osrmAddress + MakeRouteAction(coords)));

            // process response
            // here i used a linq expression instead of foreach loop
            // this part is dependent on System.Linq namespace!
            response.Route_Instructions = (
                from objs in response.Raw_Route select new OSMInstruction(objs)).ToArray();

            var alt_instructions = new List <AlternativeInstructions>();

            foreach (var instSet in response.Raw_Alternatives)
            {
                var i = new OSMInstruction[instSet.Length];
                for (int j = 0; j < instSet.Length; j++)
                {
                    i[j] = new OSMInstruction(instSet[j]);
                }
                var alt = new AlternativeInstructions();
                alt.Instructions = i;
                alt_instructions.Add(alt);
            }
            response.Alternative_Instructions = alt_instructions.ToArray();

            // another linq shorthand
            response.Via_Points = (from raw_coord in response.Raw_Via
                                   select
                                   new Coordinate(Convert.ToDouble(raw_coord[0]), Convert.ToDouble(raw_coord[1]))).ToArray();

            return(response);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds nearest OSRM node.
        /// </summary>
        /// <param name="osrmAddress">Address of the OSRM server</param>
        /// <param name="c">Coordinate</param>
        /// <returns></returns>
        public static Coordinate FindNearestNode(String osrmAddress, Coordinate c)
        {
            String      actionString = "nearest?loc=" + c.lat + "," + c.lon;
            LocResponse response     = NetworkUtilities.JsonRequest <LocResponse>(
                new Uri(osrmAddress + actionString));

            if (response.Mapped_Coordinate.Length < 2)
            {
                GlobalLogger.SendLogMessage("OSRM_Error", MessageFlags.Warning, "Coordinate out of bounds ({0}, {1})", c.First, c.Second);
                return(new Coordinate(0, 0));
            }
            return(new Coordinate(response.Mapped_Coordinate[0], response.Mapped_Coordinate[1]));
        }