Exemplo n.º 1
0
        /// <summary>
        /// Formats and structures the response.
        /// </summary>
        /// <param name="response"></param>
        /// <returns>Response reprensented in custom class.</returns>
        private RootRouteObject ParseJson(DriveReportTransportType transportType, string response)
        {
            JToken jRouteObject = JToken.Parse(response);

            if (jRouteObject == null)
            {
                return(null);
            }

            RootRouteObject route = new RootRouteObject(jRouteObject)
            {
                route_summary = new RouteSummary(jRouteObject["route_summary"])
            };

            if (jRouteObject["alternative_geometries"] != null)
            {
                if (jRouteObject["alternative_summaries"] != null)
                {
                    var altSums = jRouteObject["alternative_summaries"].ToList();

                    var altGeos = jRouteObject["alternative_geometries"].ToList();

                    for (int i = 0; i < altGeos.Count; i++)
                    {
                        route.alternative_geometries.Add((string)altGeos[i]);
                        route.alternative_summaries.Add(new AlternativeSummary(altSums[i]));
                    }
                }
            }

            if (jRouteObject["route_instructions"] != null)
            {
                var ferryDistance = 0;
                var list          = jRouteObject["route_instructions"].ToList();

                // Iterate all route instructions except for the last one.
                // The last one is different from the others somehow.
                // It does not have a mode. It only has 7 properties where the others have 8.
                // It never seems to have any distance though, so it shouldnt matter.
                for (var i = 0; i < list.Count - 1; i++)
                {
                    var currentInstruction = list.ElementAt(i).ToList();
                    var mode = currentInstruction.ElementAt(8).ToString();

                    // If the transportType is car mode 2 means travelling on a ferry.
                    // If the transportType is bicycle mode 3 means travelling on a ferry. Annoying that it's not mode 2 for both of them.
                    if ((transportType.Equals(DriveReportTransportType.Car) && mode == CarFerryMode) ||
                        (transportType.Equals(DriveReportTransportType.Bike) && mode == BicycleFerryMode))
                    {
                        // Replace "m" with nothing in the string, because the distance is given as "123m"
                        ferryDistance += int.Parse(currentInstruction.ElementAt(5).ToString().Replace("m", ""));
                    }
                }
                route.route_summary.distance_not_including_ferry = route.route_summary.total_distance - ferryDistance;
            }

            return(route);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get routes and alternative routes for the given set of coordinates.
        /// </summary>
        /// <param name="transportType">Type of transport. Car or Bike.</param>
        /// <param name="routeCoordinates"></param>
        /// <exception cref="RouteInformationException">Thrown if no route was returned or no rute was found.</exception>
        /// <returns></returns>
        public IEnumerable <RouteInformation> GetRoute(DriveReportTransportType transportType, IEnumerable <Coordinates> routeCoordinates)
        {
            List <RouteInformation> routes  = new List <RouteInformation>();
            HttpWebRequest          request = CreateRequest(transportType, routeCoordinates);
            RootRouteObject         result  = ExecuteAndRead(transportType, request);

            if (result == null)
            {
                var e = new RouteInformationException("No route returned.");
                //Logger.Error("Exception when getting route information", e);
                throw e;
            }


            //From septima 01/09 2016:
            //Routingservices kan forventes senere at returnere status = 200,
            //idet OSRM er gået fra at bruge "C style" exit-koder, til "HTTP Style" status-koder.
            if (result.status != 0 && result.status / 100 == 2)
            {
                var e = new RouteInformationException("No route found.");;
                //Logger.Error("Exception when getting route information", e);
                throw e;
            }

            var route = new RouteInformation();

            route.Length      = result.route_summary.distance_not_including_ferry;
            route.Duration    = result.route_summary.total_time;
            route.EndStreet   = result.route_summary.end_point;
            route.StartStreet = result.route_summary.start_point;
            route.GeoPoints   = result.route_geometry;

            routes.Add(route);
            for (int i = 0; i < result.alternative_summaries.Count; i++)
            {
                route = new RouteInformation
                {
                    Length      = result.alternative_summaries[i].total_distance,
                    Duration    = result.alternative_summaries[i].total_time,
                    EndStreet   = result.alternative_summaries[i].end_point,
                    StartStreet = result.alternative_summaries[i].start_point,
                    GeoPoints   = result.alternative_geometries[i]
                };

                routes.Add(route);
            }
            return(routes);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get routes and alternative routes for the given set of coordinates.
        /// </summary>
        /// <param name="transportType">Type of transport. Car or Bike.</param>
        /// <param name="routeCoordinates"></param>
        /// <exception cref="RouteInformationException">Thrown if no route was returned or no rute was found.</exception>
        /// <returns></returns>
        public IEnumerable <RouteInformation> GetRoute(DriveReportTransportType transportType, IEnumerable <Coordinates> routeCoordinates)
        {
            List <RouteInformation> routes  = new List <RouteInformation>();
            HttpWebRequest          request = CreateRequest(transportType, routeCoordinates);
            RootRouteObject         result  = ExecuteAndRead(transportType, request);

            if (result == null)
            {
                var e = new RouteInformationException("No route returned.");
                //Logger.Error("Exception when getting route information", e);
                throw e;
            }

            if (result.status != 0)
            {
                var e = new RouteInformationException("No route found.");;
                //Logger.Error("Exception when getting route information", e);
                throw e;
            }

            var route = new RouteInformation();

            route.Length      = result.route_summary.distance_not_including_ferry;
            route.Duration    = result.route_summary.total_time;
            route.EndStreet   = result.route_summary.end_point;
            route.StartStreet = result.route_summary.start_point;
            route.GeoPoints   = result.route_geometry;

            routes.Add(route);
            for (int i = 0; i < result.alternative_summaries.Count; i++)
            {
                route = new RouteInformation
                {
                    Length      = result.alternative_summaries[i].total_distance,
                    Duration    = result.alternative_summaries[i].total_time,
                    EndStreet   = result.alternative_summaries[i].end_point,
                    StartStreet = result.alternative_summaries[i].start_point,
                    GeoPoints   = result.alternative_geometries[i]
                };

                routes.Add(route);
            }
            return(routes);
        }