Пример #1
0
        public static TravelTimesMatrixModel GetTravelTimesMatrix(Position source, params Position[] desinations)
        {
            Position[] positions = new Position[desinations.Length + 1];
            positions[0] = source;
            Array.Copy(desinations, 0, positions, 1, desinations.Length);
            string positionString = StringUtils.GetStringFromPositions(positions);

            string uri  = $"https://api.mapbox.com/directions-matrix/v1/mapbox/driving/{positionString}?sources=0&annotations=duration,distance&access_token={API_KEY}";
            string json = HttpProxy.DownloadResource(uri);
            TravelTimesMatrixModel parsed = JsonConvert.DeserializeObject <TravelTimesMatrixModel>(json);

            return(parsed);
        }
Пример #2
0
        public static TravelTimesMatrixModel GetTravelTimesMatrix(Position source, params Position[] desinations)
        {
            Position[] positions = new Position[desinations.Length + 1];
            positions[0] = source;
            Array.Copy(desinations, 0, positions, 1, desinations.Length);
            string positionString = StringUtils.GetStringFromPositions(positions);

            string uri  = $"{usedApiUrl}table/v1/driving/{positionString}?sources=0";
            string json = HttpProxy.DownloadResource(uri);
            TravelTimesMatrixModel parsed = JsonConvert.DeserializeObject <TravelTimesMatrixModel>(json);

            return(parsed);
        }
Пример #3
0
        private static void InsertAggregatedPointsToDb()
        {
            var repo = new LocalizationPointRepository();
            List <LocalizationPointDto> points = repo.GetWithoutAggregated();
            var mappedPointsIds = new HashSet <long>();

            foreach (LocalizationPointDto dbPoint in points)
            {
                if (dbPoint.ParentPointId != null || dbPoint.StaticScore != 0 || mappedPointsIds.Contains(dbPoint.PointId.Value))
                {
                    continue;
                }

                Polygon      isochrone             = MapboxAPIHelper.GetIsochroneAsPolygon((Position)dbPoint.Point.Coordinates, 15);
                SqlGeography isochroneSqlGeography = isochrone.ToSqlGeography().MakeValid();
                isochroneSqlGeography = GetCorrectlyOrientedGeography(isochroneSqlGeography);

                // take points that are not parent or child points in aggregation
                var pointsWithoutParentPoint = points.Where(i => i.ParentPointId == null && i.StaticScore == 0 && !mappedPointsIds.Contains(i.PointId.Value)).ToList();
                var pointsInsideIsochrone    = pointsWithoutParentPoint.Where(i => (bool)isochroneSqlGeography.STContains(i.Point.ToSqlGeography())).ToList();
                if (pointsInsideIsochrone.Count > 1)
                {
                    Position[] positionsInsideIsochrone = pointsInsideIsochrone.Where(x => x.PointId != dbPoint.PointId).Select(x => (Position)x.Point.Coordinates).ToArray();

                    TravelTimesMatrixModel travelTimesMatrixModel = OsrmAPIHelper.GetTravelTimesMatrix((Position)dbPoint.Point.Coordinates, positionsInsideIsochrone);
                    var durationsList = travelTimesMatrixModel.durations != null ? travelTimesMatrixModel.durations[0].ToList() : new List <float>();
                    var durationsListSortedWithIndexes = durationsList.Select((x, index) => new KeyValuePair <int, float>(index, x)).OrderBy(x => x.Value).ToList();

                    // get route that innerdistance and time is no longer than 30min and x? km
                    double       maxInnerDistance = 30 * 1000;
                    double       maxInnerTime     = 15 * 60;
                    List <long?> pointIds         = GetRouteMeetingConditionsAndPointIds(durationsListSortedWithIndexes.Select(x => x.Key).ToList(), pointsInsideIsochrone, maxInnerDistance, maxInnerTime, out RouteModel resultRoute);

                    if (pointIds != null)
                    {
                        pointsInsideIsochrone.ForEach(i => mappedPointsIds.Add(i.PointId.Value));

                        // Create aggregated point
                        LocalizationPointDto aggregatedPoint = GetAggregatedPoint(pointIds, pointsWithoutParentPoint, resultRoute.Distance, resultRoute.Time);
                        LocalizationPointDto addedPoint      = repo.Add(aggregatedPoint);

                        // Update parentId for child points
                        List <LocalizationPointDto> updatedPoints = GetUpdatedChildPointsWithParentId(pointIds, points, addedPoint.PointId);
                        foreach (LocalizationPointDto point in updatedPoints)
                        {
                            repo.Update(point);
                        }
                    }
                }
            }
        }