public static List<MapPoint> GetPointsFromStops(Route route) { var result = new List<MapPoint>(); foreach (var stop in route.Stops) { result.Add(new MapPoint((double)stop.StopGeography.Long, (double)stop.StopGeography.Lat)); } return result; }
/// <summary> /// Gets the route in points. /// </summary> /// <param name="route">The route.</param> /// <returns></returns> public static List<MapPoint> GetRouteInPoints(Route route) { var result = new List<MapPoint>(); for (int i = 1; i <= route.RouteGeography.STNumPoints(); i++) { var point = route.RouteGeography.STPointN(i); result.Add(new MapPoint((double)point.Long, (double)point.Lat)); } return result; }
public static Route GetCrossStops(Route route) { var crossStops = new List<Stop>(); var result = route; foreach (var stop in route.Stops) { if (stop.StopGeography.STBuffer(1).STIntersects(route.RouteGeography)) { crossStops.Add(stop); } } result.Stops = crossStops; return result; }
public void Can_Get_Route_Geography() { // Arrange var route = new Route(); route.RouteGeography = SqlGeography.STGeomFromText( new SqlChars("LINESTRING(35.01064 48.43012, 35.01436 48.42924, 35.01436 48.42924)"), 4326); var anotherRoute = new Route(); anotherRoute.RouteBin = SqlGeography.STLineFromText(route.RouteGeography.STAsText(), 4326).STAsBinary().Buffer; // Assert Assert.AreNotEqual(null, route.RouteGeography); Assert.AreNotEqual(null, anotherRoute.RouteGeography); }
/// <summary> /// Gets the time. /// </summary> /// <param name="route">The route.</param> /// <returns> /// Summary time of route /// </returns> private TimeSpan GetSumamryTime(Route route) { double walkLength = route.WalkingRoutes.Sum(walkingRoute => walkingRoute.Length); var walkTime = GetTimeInMinutes(GeneralSettings.WalkingSpeed, walkLength); return TimeSpan.FromMinutes( this.GetTimeInMinutes(route.Speed, route.Length, (int)route.WaitingTime.TotalMinutes) + walkTime); }
private static TimeSpan GetTime(Route route) { double lenght = 0; for (int i = 1; i < route.RouteGeography.STNumPoints(); i++) { lenght += (double) route.RouteGeography.STPointN(i).STDistance(route.RouteGeography.STPointN(i + 1)); } return TimeSpan.FromSeconds((lenght/(route.Speed*3600)/1000) + route.WaitingTime.Seconds); }
/// <summary> /// Gets the cross stops. /// </summary> /// <param name="route">The route.</param> /// <returns> /// Route with new cross stops /// </returns> private Route GetCrossStops(Route route) { var crossStops = new List<Stop>(); crossStops.AddRange( route.Stops.Where(stop => (bool)stop.StopGeography.STBuffer(1).STIntersects(route.CurrentPath))); route.Stops = crossStops; route.Stops = this.SortStops(route); return route; }
/// <summary> /// Adds the walking steps to route. /// </summary> /// <param name="route">The route.</param> /// <param name="startMarker">The start marker.</param> /// <param name="endMarker">The end marker.</param> /// <returns> /// Route with walking steps /// </returns> private Route BuildRouteProperties(Route route, SqlGeography startMarker, SqlGeography endMarker) { if (!route.Type.Type.Equals("Walking")) { route = this.GetCrossStops(route); route.Length = this.GetRouteLength(route); route.MapPoints = this.GetRouteInPoints(route); route.WalkingRoutes = new List<WalkingRoute> { this.GetWalkingStepsForRoute(endMarker, route.StartStop.StopGeography), this.GetWalkingStepsForRoute(route.EndStop.StopGeography, startMarker) }; route.Time = this.GetSumamryTime(route); route.RouteTime = TimeSpan.FromMinutes(GetTimeInMinutes(route.Speed, route.Length)); route.Stops.Select(this.GetPointsFromStops).ToList(); } return route; }
/// <summary> /// Finds the nearest stop. /// </summary> /// <param name="stops">The stops.</param> /// <param name="findRoute">The find route.</param> /// <param name="startMarker">The start marker.</param> /// <param name="endMarker">The end marker.</param> /// <returns>Neareres stop for startMarker</returns> private static SqlGeography FindNearestStop(Route route, SqlGeography marker) { SqlGeography result = route.Stops[0].StopGeography; for (int i = 0; i < route.Stops.Count - 1; i++) { if (route.Stops[i].StopGeography.STDistance(marker) < result.STDistance(marker)) { result = route.Stops[i].StopGeography; } } return result; }
/// <summary> /// Gets the walking routes in view model. /// </summary> /// <param name="route">The route.</param> /// <returns>Valid walking routes</returns> public static List<WalkingRoutesViewModel> GetWalkingRoutesInViewModel(Route route) { var walkingRoutes = route.WalkingRoutes.Select(walk => new WalkingRoutesViewModel() { Name = walk.Name, Length = GetRoundDistance(walk.Length), MapPoints = walk.MapPoints, Speed = walk.Speed, Steps = walk.Steps, Time = GetRoundTime(walk.Time), Type = walk.Type, }).ToList(); return walkingRoutes; }
/// <summary> /// Finds the nearest stop. /// </summary> /// <param name="stops">The stops.</param> /// <param name="findRoute">The find route.</param> /// <param name="startMarker">The start marker.</param> /// <param name="endMarker">The end marker.</param> /// <returns>Neareres stop for startMarker</returns> private static Stop FindNearestStop(List<Stop> stops, Route findRoute, SqlGeography startMarker, SqlGeography endMarker) { var buffer = StopBuffer; var result = new Stop(); var foundNearestStop = false; do { Parallel.For(0, stops.Count(), i => { if ( stops[i].StopGeography.STIntersects( endMarker.STBuffer(buffer)) && stops[i].StopGeography.STIntersects(findRoute.RouteGeography)) { result = new Stop { Name = stops[i].Name, StopGeography = stops[i].StopGeography.STIntersection(findRoute.RouteGeography).EnvelopeCenter() }; foundNearestStop = true; } }); /* foreach (Stop stop in stops) { if ( stop.StopGeography.STIntersects( endMarker.STBuffer(buffer)) && stop.StopGeography.STIntersects(findRoute.RouteGeography)) { result = new Stop { Name = stop.Name, StopGeography = stop.StopGeography.STIntersection(findRoute.RouteGeography).EnvelopeCenter() }; foundNearestStop = true; } }*/ buffer += StopBuffer; } while (foundNearestStop == false && buffer < MaxBuffer); return result; }
/// <summary> /// Gets the length of part route. /// </summary> /// <param name="route">The route.</param> /// <returns> /// Length of part route. /// </returns> private double GetLengthOfPartRoute(Route route) { double length = 0; for (var i = route.StartRouteIndex; i < route.EndRouteIndex; i++) { length += (double)route.RouteGeography.STPointN(i).STDistance(route.RouteGeography.STPointN(i + 1)); } return length; }
/// <summary> /// Gets the stops in view model. /// </summary> /// <param name="route">The route.</param> /// <returns>Valid stops </returns> public static List<StopViewModel> GetStopsInViewModel(Route route) { var stops = route.Stops.Select(stop => new StopViewModel() { Name = stop.Name, Points = stop.Points }).ToList(); return stops; }
/// <summary> /// Finds the start stop. /// </summary> /// <param name="route">The route.</param> /// <param name="marker">The marker.</param> /// <returns>Geography of stop</returns> private Stop GetStop(Route route, SqlGeography marker) { Stop result = route.Stops[0]; foreach (Stop stop in route.Stops) { if (stop.StopGeography.STDistance(marker) < result.StopGeography.STDistance(marker)) { result = stop; } } List<Stop> possibleStops = route.Stops.Where(stop => (bool)result.StopGeography.STBuffer(GeneralSettings.MaxStopRadiusSeach).STIntersects(stop.StopGeography)).ToList(); var urlForDistanceMatrix = GoogleMapHelper.CreateUrlForDisntanceMatrixRequest( GeneralSettings.GoogleApiKey, marker, possibleStops); var responce = GoogleMapHelper.GetResponceFromGoogleApi(urlForDistanceMatrix); var distances = GoogleMapHelper.GetMatrixOfDistanceForOneStop(responce); var index = distances.IndexOf(distances.Min(stop => stop)); return possibleStops[index]; }
/// <summary> /// Gets the length of the route. /// </summary> /// <param name="route">The route.</param> /// <returns>Length of route</returns> private double GetRouteLength(Route route) { double length = 0; for (var i = 1; i < route.CurrentPath.STNumPoints(); i++) { length += (double)route.CurrentPath.STPointN(i).STDistance(route.CurrentPath.STPointN(i + 1)); } return length; }
/// <summary> /// Gets the route in points. /// </summary> /// <param name="route">The route.</param> /// <returns>Points for paint route on map</returns> private List<MapPoint> GetRouteInPoints(Route route) { var result = new List<MapPoint>(); for (int i = 1; i <= route.CurrentPath.STNumPoints(); i++) { var point = route.CurrentPath.STPointN(i); result.Add(new MapPoint((double)point.Long, (double)point.Lat)); } return result; }
/// <summary> /// Gets the possible stop. /// </summary> /// <param name="route">The route.</param> /// <param name="marker">The marker.</param> /// <returns>Possible stop of route.</returns> private Stop GetPossibleStop(Route route, SqlGeography marker) { Stop result = route.Stops[0]; foreach (Stop stop in route.Stops) { if (stop.StopGeography.STDistance(marker) < result.StopGeography.STDistance(marker)) { result = stop; } } return result; }
/// <summary> /// Gets the index of the path. /// </summary> /// <param name="route">The route.</param> /// <param name="point">The point.</param> /// <returns>Index for rechenge route</returns> private int GetPathIndex(Route route, SqlGeography point) { int position = 1; for (int i = 1; i < route.RouteGeography.STNumPoints(); i++) { position++; if (route.RouteGeography.STPointN(i).STDistance(point) <= GeneralSettings.MaxFindIndexDeflection) { break; } } return position; }
/// <summary> /// Gets the walking route. /// </summary> /// <param name="startPoint">The start point.</param> /// <param name="endPoint">The end point.</param> /// <returns>Walking route</returns> private Route GetWalkingRoute(SqlGeography startPoint, SqlGeography endPoint) { var urlForDirection = GoogleMapHelper.CreateUrlForDirectionRequest( GeneralSettings.GoogleApiKey, startPoint, endPoint); var responce = GoogleMapHelper.GetResponceFromGoogleApi(urlForDirection); var polyline = (List<MapPoint>)GoogleMapHelper.GetSummaryPolyline(responce); var summaryDistance = GoogleMapHelper.GetDistanceOfDirection(responce); var summaryTime = GetTimeInMinutes(GeneralSettings.WalkingSpeed, summaryDistance); var walkingType = new TransportType { Type = "Walking" }; var walkingRoute = new Route { Name = string.Empty, MapPoints = polyline, RouteType = (int)Transport.Walking, Speed = GeneralSettings.WalkingSpeed, Time = TimeSpan.FromMinutes(summaryTime), Stops = new List<Stop>(), Type = walkingType, Steps = GoogleMapHelper.GetStepsOfDirection(responce), Length = GoogleMapHelper.GetDistanceOfDirection(responce) }; return walkingRoute; }
/// <summary> /// Intersections the specified route. /// </summary> /// <param name="route">The route.</param> /// <param name="startMarker">The start marker.</param> /// <param name="endMarker">The end marker.</param> /// <returns>return bool value of intersections for routes</returns> private bool Intersection(Route route, SqlGeography startMarker, SqlGeography endMarker) { return (bool)route.RouteGeography.STIntersects(startMarker.STBuffer(GeneralSettings.RouteRadiusSeach)) && (bool)route.RouteGeography.STIntersects(endMarker.STBuffer(GeneralSettings.RouteRadiusSeach)); }
/// <summary> /// Gets the summary walking distance. /// </summary> /// <param name="route">The route.</param> /// <returns>Summary walking distance in route</returns> public static double GetSummaryWalkingDistance(Route route) { var summaryDistance = route.WalkingRoutes.Sum(walk => walk.Length); return summaryDistance; }
/// <summary> /// Sorts the stops. /// </summary> /// <param name="route">The route.</param> /// <returns>Sorting list of stops</returns> private List<Stop> SortStops(Route route) { for (var i = route.Stops.Count - 1; i > 0; i--) { for (var j = 0; j < i; j++) { if (route.RouteGeography.STStartPoint().STDistance(route.Stops[j].StopGeography) >= route.RouteGeography.STStartPoint().STDistance(route.Stops[j + 1].StopGeography)) { var temp = route.Stops[j]; route.Stops[j] = route.Stops[j + 1]; route.Stops[j + 1] = temp; } } } return route.Stops as List<Stop>; }
/// <summary> /// Builds the route geography. /// </summary> /// <param name="startIndex">The start index.</param> /// <param name="endIndex">The end index.</param> /// <param name="nearestStop">The nearest stop.</param> /// <param name="furtherStop">The further stop.</param> /// <param name="route">The route.</param> /// <returns>Geography of appropriate route</returns> private static Route BuildRouteGeography(int startIndex,int endIndex,Stop nearestStop, Stop furtherStop,Route route) { var geographyBuilder = new SqlGeographyBuilder(); geographyBuilder.SetSrid(4326); geographyBuilder.BeginGeography(OpenGisGeographyType.LineString); SqlGeography beginGb = nearestStop.StopGeography; geographyBuilder.BeginFigure((double) beginGb.Lat, (double) beginGb.Long); for (var j = startIndex; j <= endIndex; j++) { var point = route.RouteGeography.STPointN(j); geographyBuilder.AddLine((double) point.Lat, (double) point.Long); } SqlGeography endFigure = furtherStop.StopGeography; geographyBuilder.AddLine((double) endFigure.Lat, (double) endFigure.Long); geographyBuilder.EndFigure(); geographyBuilder.EndGeography(); route.RouteGeography = geographyBuilder.ConstructedGeography; return route; }
/// <summary> /// Finds the start index. /// </summary> /// <param name="route">The route.</param> /// <param name="stop">The stop.</param> /// <returns>Start index</returns> private static int FindIndex(Route route, SqlGeography stop) { int result = 0; for (int j = 1; j <= route.RouteGeography.STNumPoints(); j++) { if (route.RouteGeography.STStartPoint().STDistance(stop) > route.RouteGeography.STStartPoint().STDistance(route.RouteGeography.STPointN(j))) { result = j; } } return result; }
/// <summary> /// Finds the start index. /// </summary> /// <param name="route">The route.</param> /// <param name="stop">The stop.</param> /// <returns>Start index</returns> private static int FindStartIndex(Route route, Stop stop) { var result = 0; /* Parallel.For(1, (int) route.RouteGeography.STNumPoints() + 1, i => { if (route.RouteGeography.STStartPoint().STDistance(stop.StopGeography) > route.RouteGeography.STStartPoint().STDistance(route.RouteGeography.STPointN(i))) { result = i; } });*/ for (int j = 1; j < route.RouteGeography.STNumPoints() ; j++) { if (route.RouteGeography.STStartPoint().STDistance(stop.StopGeography) > route.RouteGeography.STStartPoint().STDistance(route.RouteGeography.STPointN(j))) { result = j ; } } return result; }
/// <summary> /// Builds the route geography. /// </summary> /// <param name="route">The route.</param> /// <returns> /// New route geography /// </returns> private Route BuildRouteGeography(Route route) { var routeBuilder = new SqlGeographyBuilder(); routeBuilder.SetSrid(4326); routeBuilder.BeginGeography(OpenGisGeographyType.LineString); SqlGeography beginPoint = route.StartStop.StopGeography; routeBuilder.BeginFigure((double)beginPoint.Lat, (double)beginPoint.Long); for (var j = route.StartRouteIndex; j <= route.EndRouteIndex; j++) { var point = route.RouteGeography.STPointN(j); routeBuilder.AddLine((double)point.Lat, (double)point.Long); } SqlGeography endPoint = route.EndStop.StopGeography; routeBuilder.AddLine((double)endPoint.Lat, (double)endPoint.Long); routeBuilder.EndFigure(); routeBuilder.EndGeography(); route.CurrentPath = routeBuilder.ConstructedGeography; return route; }