예제 #1
0
        private List <long?> GetRouteMeetingConditionsAndPointIds(List <int> indexesOfDurationList, List <DbModel.LocalizationPointDto> pointsInsideIsochrone, double maxInnerDistance, double maxInnerTime, out RouteModel route)
        {
            var sortedPointsByDuration = new List <DbModel.LocalizationPointDto>();

            foreach (var index in indexesOfDurationList)
            {
                sortedPointsByDuration.Add(pointsInsideIsochrone.ElementAt(index));
            }

            bool doesRouteMeetParameters = false;

            route = null;
            while (!doesRouteMeetParameters)
            {
                var routeJson = OsrmAPIHelper.GetOptimalRoute(sortedPointsByDuration.Select(x => (Position)x.Point.Coordinates).ToArray());
                if (routeJson == null)
                {
                    route = null;
                    return(null);
                }
                route = routeJson.ToRouteModel();
                doesRouteMeetParameters = DoesInnerRouteMeetParameters(route, maxInnerDistance, maxInnerTime);
                if (!doesRouteMeetParameters)
                {
                    sortedPointsByDuration.RemoveAt(sortedPointsByDuration.Count() - 1);
                }
            }

            var pointIds = sortedPointsByDuration.Select(x => x.PointId).ToList();

            return(pointIds);
        }
예제 #2
0
        protected RouteModel GetRouteBetweenTwoPoints(List <Position> waypoints)
        {
            //var routeJson = MapboxAPIHelper.GetOptimalRoute(this.startingPosition, this.endingPositions, waypoints.ToArray());
            var routeJson = OsrmAPIHelper.GetOptimalRoute(this.startingPosition, this.endingPositions, waypoints.ToArray());

            return(routeJson.ToRouteModel());
        }
예제 #3
0
        protected RouteModel GetRouteBetweenTwoPoints()
        {
            //var routeJson = MapboxAPIHelper.GetSimpleRoute(startingPosition, endingPositions);
            var routeJson = OsrmAPIHelper.GetSimpleRoute(startingPosition, endingPositions);

            return(routeJson.ToRouteModel());
        }
예제 #4
0
        private List <ViewModel.Polyline> GetDifferencePolyline()
        {
            GeoJSON.Net.Geometry.Position warszawa = new GeoJSON.Net.Geometry.Position(52.229941, 21.011105);
            GeoJSON.Net.Geometry.Position mlawa    = new GeoJSON.Net.Geometry.Position(53.112154, 20.382377);
            GeoJSON.Net.Geometry.Position plock    = new GeoJSON.Net.Geometry.Position(52.546665, 19.687432);
            OsrmJsonRouteModel            parsed   = OsrmAPIHelper.GetOptimalRoute(warszawa, mlawa);
            var route = parsed.ToRouteModel();

            var jsonNew  = OsrmAPIHelper.GetOptimalRoute(warszawa, mlawa, plock);
            var newRoute = jsonNew.ToRouteModel();

            //var oldRouteGMultiLineString = route.MultiLineString.ToSqlGeography();
            //var newRouteGMultiLineString = newRoute.MultiLineString.ToSqlGeography();
            //var difference = newRouteGMultiLineString.STDifference(oldRouteGMultiLineString);


            //var oldRouteG = route.LineString.ToSqlGeography();
            //var newRouteG = newRoute.LineString.ToSqlGeography().MakeValidIfInvalid();
            //string isValid = newRouteG.IsValidDetailed();
            //var difference = newRouteG.STDifference(oldRouteG);


            Microsoft.SqlServer.Types.SqlGeography oldRouteGeography = route.MultiPointGeoJsonNet.ToSqlGeography();
            Microsoft.SqlServer.Types.SqlGeography newRouteGeography = newRoute.MultiPointGeoJsonNet.ToSqlGeography();
            Microsoft.SqlServer.Types.SqlGeography bufferOld         = oldRouteGeography.STBuffer(1000);
            Microsoft.SqlServer.Types.SqlGeography bufferNew         = newRouteGeography.STBuffer(1000);
            var difference = bufferNew.STDifference(bufferOld);


            double diffArea = (double)difference.STArea();
            double oldArea  = (double)bufferOld.STArea();
            double newArea  = (double)bufferNew.STArea();

            double howDifferent = diffArea / oldArea;



            List <ViewModel.Polyline> lines = new List <ViewModel.Polyline>();
            //lines.Add(GetFromSqlGeography(oldRouteGeography));
            //lines.Add(GetFromSqlGeography(newRouteGeography));
            //lines.Add(GetFromSqlGeography(difference));
            //lines.Add(GetFromSqlGeography(bufferNew));
            //lines.Add(GetFromSqlGeography(bufferOld));
            //lines.Add(GetFromSqlGeography(oldRouteG));
            //lines.Add(GetFromSqlGeography(newRouteG));
            //lines.Add(GetFromSqlGeography(difference));
            //lines.Add(GetFromSqlGeography(newRouteGMultiLineString));
            //lines.Add(GetFromSqlGeography(oldRouteGMultiLineString));

            var polylines = GetFromSqlGeographies(difference);

            foreach (var line in polylines)
            {
                lines.Add(line);
            }

            return(lines);
        }
예제 #5
0
        private static void OsrmTravelTimeTableUsage()
        {
            GeoJSON.Net.Geometry.Position mlawa           = new GeoJSON.Net.Geometry.Position(53.112128, 20.383661);
            GeoJSON.Net.Geometry.Position positionInside  = new GeoJSON.Net.Geometry.Position(53.125982, 20.358108);
            GeoJSON.Net.Geometry.Position positionOutside = new GeoJSON.Net.Geometry.Position(53.155378, 20.363038);

            OsrmAPIHelper.GetTravelTimesMatrix(mlawa, positionInside, positionOutside);
            MapboxAPIHelper.GetTravelTimesMatrix(mlawa, positionInside, positionOutside);
        }
예제 #6
0
        private void InsertAggregatedPointsToDb()
        {
            var repo       = new MockLocalizationPointRepositoryMazowieckie();
            var pointItems = GetPointsItemsFromRepo(repo);

            foreach (var pointItem in pointItems)
            {
                (this.DataContext as MapViewModel).Pushpins.Add(pointItem);
            }

            var points = repo.GetWithoutAggregated();

            for (int i = 0; i < points.Count(); i++)
            {
                if (points.ElementAt(i).ParentPointId != null || points.ElementAt(i).StaticScore != 0)
                {
                    continue;
                }

                var          isochrone             = MapboxAPIHelper.GetIsochroneAsPolygon((Position)points.ElementAt(i).Point.Coordinates, 15);
                SqlGeography isochroneSqlGeography = isochrone.ToSqlGeography().MakeValid();
                isochroneSqlGeography = GetCorrectlyOrientedGeography(isochroneSqlGeography);
                this.AddPolylineToMap(GetFromSqlGeography(isochroneSqlGeography));

                // take points that are not parent or child points in aggregation
                var pointsWithoutParentPoint = points.Where(i => i.ParentPointId == null && i.StaticScore == 0).ToList();
                var pointsInsideIsochrone    = pointsWithoutParentPoint.Where(i => (bool)isochroneSqlGeography.STContains(i.Point.ToSqlGeography())).ToList();
                if (pointsInsideIsochrone.Count() > 1)
                {
                    var positionsInsideIsochrone       = pointsInsideIsochrone.Where(x => x.PointId != points.ElementAt(i).PointId).Select(x => (Position)x.Point.Coordinates).ToArray();
                    var travelTimesMatrixModel         = OsrmAPIHelper.GetTravelTimesMatrix((Position)points.ElementAt(i).Point.Coordinates, positionsInsideIsochrone);
                    var durationsList                  = travelTimesMatrixModel.durations[0].ToList();
                    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
                    RouteModel resultRoute;
                    double     maxInnerDistance = 30 * 1000;
                    double     maxInnerTime     = 15 * 60;
                    var        pointIds         = GetRouteMeetingConditionsAndPointIds(durationsListSortedWithIndexes.Select(x => x.Key).ToList(), pointsInsideIsochrone, maxInnerDistance, maxInnerTime, out resultRoute);

                    if (pointIds != null)
                    {
                        // Create aggregated point
                        var aggregatedPoint = GetAggregatedPoint(pointIds, pointsWithoutParentPoint, resultRoute.Distance, resultRoute.Time);
                        points.Add(aggregatedPoint);
                        // Update parentId for child points
                        var updatedPoints = GetUpdatedChildPointsWithParentId(pointIds, ref points, aggregatedPoint.PointId);
                    }
                }
            }
        }
예제 #7
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);
                        }
                    }
                }
            }
        }
예제 #8
0
        private List <ViewModel.Polyline> GetBufferPolyline()
        {
            GeoJSON.Net.Geometry.Position pos1   = new GeoJSON.Net.Geometry.Position(52.218703, 21.005044);
            GeoJSON.Net.Geometry.Position pos2   = new GeoJSON.Net.Geometry.Position(54.182346, 16.189145);
            OsrmJsonRouteModel            parsed = OsrmAPIHelper.GetOptimalRoute(pos1, pos2);
            var route = parsed.ToRouteModel();

            Microsoft.SqlServer.Types.SqlGeography sqlGeography = route.MultiPointGeoJsonNet.ToSqlGeography();


            Microsoft.SqlServer.Types.SqlGeography buffer = sqlGeography.STBuffer(100000);


            List <ViewModel.Polyline> lines = new List <ViewModel.Polyline>();

            lines.Add(GetFromSqlGeography(sqlGeography));
            lines.Add(GetFromSqlGeography(buffer));

            return(lines);
        }
예제 #9
0
        private static ViewModel.Polyline GetMapControlPolyLineFromOsrmApi()
        {
            //OsrmJsonRouteModel parsed = APIHelpers.OsrmAPIHelper.GetRouteBetweenPoints(GetManyCoordinatesForNormalRoute());

            GeoJSON.Net.Geometry.Position warszawa = new GeoJSON.Net.Geometry.Position(52.230320, 21.011132);
            GeoJSON.Net.Geometry.Position szczecin = new GeoJSON.Net.Geometry.Position(53.421684, 14.561405);
            OsrmJsonRouteModel            parsed   = OsrmAPIHelper.GetOptimalRoute(warszawa, szczecin, GetManyCoordinatesForOptimalRoute());

            var polyline = new ViewModel.Polyline();

            polyline.Locations = new MapControl.LocationCollection();
            foreach (var coordinate in parsed.routes[0].geometry.coordinates)
            {
                polyline.Locations.Add(new MapControl.Location((float)coordinate[1], (float)coordinate[0]));

                //if (feature.geometry.coordinates[0] is double longitude && feature.geometry.coordinates[1] is double latitude)
                //{
                //	polyline.Locations.Add(new MapControl.Location(latitude, longitude));
                //}
            }

            return(polyline);
        }