Ejemplo n.º 1
0
        private void RecalculateLength(RouteDTO route)
        {
            var length = 0d;

            foreach (var s in DistanceHelpers.SplitFeatureIntoLineSegments(route.Data.Coordinates))
            {
                length += DistanceHelpers.DistanceBetweenCoordinates(s.Item1, s.Item2);
            }

            route.Info.Length = Math.Round(length, 2);
        }
Ejemplo n.º 2
0
        public static RouteDTO PathToRoute(List <RouteFeature> path, PointPosition startPosition, PointPosition endPosition)
        {
            var sortedFeatures = SortFeatures(path, startPosition, endPosition);

            var routeDto = new RouteDTO
            {
                Data = new RouteDataDTO
                {
                    Type        = "LineString",
                    Coordinates = sortedFeatures.SelectMany(x =>
                                                            x.Data.Coordinates.Select(y => new[] { y.Longitude, y.Latitude })
                                                            .ToArray()).ToArray()
                },
                Info = new RouteInfoDTO
                {
                    Length = 0
                             //Length = Math.Round(path.Sum(x => (double) x.Data.Properties["lenght"]), 2)
                             //TODO fix
                }
            };

            var routeJson = routeDto.ToGeoJson();

            var firstFeature = sortedFeatures.First();
            var lastFeature  = sortedFeatures.Last();


            var firstFeatureJson = FeaturesToGeojsonHelper.ToGeojson(new [] { firstFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray() });
            var lastFeatureJson  = FeaturesToGeojsonHelper.ToGeojson(new [] { lastFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray() });

            //var distanceToStartFeature = DistanceHelpers.CalcualteDistanceToFeature(
            //    firstFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray(), startPosition.ToDoubleArray());


            var projectionOnStartFeature = DistanceHelpers.GetProjectionOnFeature(
                firstFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray(),
                startPosition.ToDoubleArray());

            var distanceToStartProjection =
                DistanceHelpers.DistanceBetweenCoordinates(projectionOnStartFeature.Item2,
                                                           startPosition.ToDoubleArray());

            if ((
                    distanceToStartProjection != DistanceHelpers.DistanceBetweenCoordinates(startPosition.ToDoubleArray(),
                                                                                            firstFeature.Data.Coordinates.First().ToDoubleArray()) &&
                    distanceToStartProjection != DistanceHelpers.DistanceBetweenCoordinates(startPosition.ToDoubleArray(),
                                                                                            firstFeature.Data.Coordinates.Last().ToDoubleArray())
                    ) ||
                (distanceToStartProjection == DistanceHelpers.DistanceBetweenCoordinates(
                     startPosition.ToDoubleArray(),
                     firstFeature.Data.Coordinates.Last().ToDoubleArray()))
                )
            {
                //var projectionResult = DistanceHelpers.GetProjectionOnFeature(
                //    firstFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray(),
                //    startPosition.ToDoubleArray());

                var result = new List <double[]> {
                    projectionOnStartFeature.Item2
                };

                var index = Array.FindIndex(routeDto.Data.Coordinates,
                                            x => x.SequenceEqual(projectionOnStartFeature.Item1.Item1));

                result.AddRange(routeDto.Data.Coordinates.Skip(index + 1));
                routeDto.Data.Coordinates = result.ToArray();
            }

            var projectionOnEndFeature = DistanceHelpers.GetProjectionOnFeature(
                lastFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray(),
                endPosition.ToDoubleArray());

            var distanceToEndProjection =
                DistanceHelpers.DistanceBetweenCoordinates(projectionOnEndFeature.Item2,
                                                           endPosition.ToDoubleArray());

            if ((distanceToEndProjection != DistanceHelpers.DistanceBetweenCoordinates(endPosition.ToDoubleArray(),
                                                                                       lastFeature.Data.Coordinates.First().ToDoubleArray()) &&
                 distanceToEndProjection != DistanceHelpers.DistanceBetweenCoordinates(endPosition.ToDoubleArray(),
                                                                                       lastFeature.Data.Coordinates.Last().ToDoubleArray()))
                ||
                (distanceToEndProjection == DistanceHelpers.DistanceBetweenCoordinates(
                     endPosition.ToDoubleArray(),
                     lastFeature.Data.Coordinates.First().ToDoubleArray()))
                )
            {
                //var projectionResult = DistanceHelpers.GetProjectionOnFeature(
                //    lastFeature.Data.Coordinates.Select(x => x.ToDoubleArray()).ToArray(), endPosition.ToDoubleArray());

                var index = Array.FindIndex(routeDto.Data.Coordinates,
                                            x => x.SequenceEqual(projectionOnEndFeature.Item1.Item2));

                var result = routeDto.Data.Coordinates.Take(index).ToList();
                result.Add(projectionOnEndFeature.Item2);

                routeDto.Data.Coordinates = result.ToArray();
            }

            return(routeDto);
        }