Ejemplo n.º 1
0
        /// <param name="pulseProgress">делегат вызываемый для отображения прогресс, первый параметр текущее значение. Второй сколько всего.</param>
        public List <PointLatLng> GetGeometryOfRoute(long[] route, Action <uint, uint> pulseProgress)
        {
            //Запрашиваем кешь одним запросом для всего маршрута.
            LoadDBCacheIfNeed(GenerateWaysOfRoute(route));

            List <PointLatLng> resultRoute = new List <PointLatLng>();

            for (int ix = 1; ix < route.Length; ix++)
            {
                CachedDistance way = GetCachedGeometry(route[ix - 1], route[ix], false);

                if (way?.PolylineGeometry != null)
                {
                    var decodedPoints = Polyline.DecodePolyline(way.PolylineGeometry);
                    resultRoute.AddRange(decodedPoints.Select(p => new PointLatLng(p.Latitude, p.Longitude)));
                }
                else
                {
                    double lat, lon;
                    CachedDistance.GetLatLon(route[ix - 1], out lat, out lon);
                    resultRoute.Add(new PointLatLng(lat, lon));
                    CachedDistance.GetLatLon(route[ix], out lat, out lon);
                    resultRoute.Add(new PointLatLng(lat, lon));
                }

                if (pulseProgress != null)
                {
                    pulseProgress((uint)ix, (uint)route.Length);
                }
            }

            return(resultRoute);
        }
Ejemplo n.º 2
0
        bool UpdateFromProvider(CachedDistance distance)
        {
            if (ErrorWays.Any(x => x.FromHash == distance.FromGeoHash && x.ToHash == distance.ToGeoHash))
            {
                //logger.Warn("Повторный запрос дистанции с ошибкой расчета. Пропускаем...");
                return(false);
            }

            //logger.Info("Запрашиваем путь {0}->{1} у сервиса {0}.", distance.FromGeoHash, distance.ToGeoHash, Provider);
            List <PointOnEarth> points = new List <PointOnEarth>();
            double latitude, longitude;

            CachedDistance.GetLatLon(distance.FromGeoHash, out latitude, out longitude);
            points.Add(new PointOnEarth(latitude, longitude));
            CachedDistance.GetLatLon(distance.ToGeoHash, out latitude, out longitude);
            points.Add(new PointOnEarth(latitude, longitude));
            bool ok = false;

            if (Provider == DistanceProvider.Osrm)
            {
                var result = OsrmMain.GetRoute(points, false, GeometryOverview.Full);
                ok = result?.Code == "Ok";
                if (ok && result.Routes.Any())
                {
                    distance.DistanceMeters   = result.Routes.First().TotalDistance;
                    distance.TravelTimeSec    = result.Routes.First().TotalTimeSeconds;
                    distance.PolylineGeometry = result.Routes.First().RouteGeometry;
                }
            }
            else
            {
                var result = SputnikMain.GetRoute(points, false, true);
                ok = result.Status == 0;
                if (ok)
                {
                    distance.DistanceMeters   = result.RouteSummary.TotalDistance;
                    distance.TravelTimeSec    = result.RouteSummary.TotalTimeSeconds;
                    distance.PolylineGeometry = result.RouteGeometry;
                }
            }

            if (ok)
            {
                lock (uow) {
                    AddNewCacheDistance(distance);
                    uow.TrySave(distance);
                    uow.Commit();
                }
                addedCached++;
                return(true);
            }

            ErrorWays.Add(new WayHash(distance.FromGeoHash, distance.ToGeoHash));
            totalErrors++;
            return(false);
        }