public GeoCoordinateBox AddRoute(OsmSharpRoute route, Color? color, int width) { if (route != null) { Color route_color; if (color != null && color.HasValue) { route_color = color.Value; } else { route_color = this.RandomColor(); } List<GeoCoordinate> coordinates = route.GetPoints(); if (coordinates != null && coordinates.Count > 0) { double distance_arrow = 300; this.DoForRoute(coordinates, distance_arrow, route_color, width, 16, 18); this.DoForRoute(coordinates, distance_arrow * 4, route_color, width - 1, 14, 16); return this.DoForRoute(coordinates, distance_arrow * 8, route_color, width - 2, 0, 14); } } return null; }
/// <summary> /// Calculates metrics for the given route. /// </summary> /// <param name="route"></param> /// <returns></returns> public Dictionary<string, double> Calculate(OsmSharpRoute route) { OsmSharp.Routing.ArcAggregation.ArcAggregator aggregator = new OsmSharp.Routing.ArcAggregation.ArcAggregator(_interpreter); AggregatedPoint p = aggregator.Aggregate(route); return this.Calculate(route.Vehicle, p); }
/// <summary> /// Parses a route from a data stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public static OsmSharpRoute Load(Stream stream) { XmlSerializer ser = new XmlSerializer(typeof(OsmSharpRoute)); OsmSharpRoute route = ser.Deserialize(stream) as OsmSharpRoute; ser = null; return(route); }
/// <summary> /// Parses a route from a byte array. /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static OsmSharpRoute Load(byte[] bytes) { XmlSerializer serializer = new XmlSerializer(typeof(OsmSharpRoute)); MemoryStream mem_stream = new MemoryStream(bytes); //GZipStream stream = new GZipStream(mem_stream, CompressionMode.Decompress); Stream stream = mem_stream; OsmSharpRoute route = (serializer.Deserialize(stream) as OsmSharpRoute); return(route); }
/// <summary> /// Concatenates two routes. /// </summary> /// <param name="route1"></param> /// <param name="route2"></param> /// <param name="clone"></param> /// <returns></returns> public static OsmSharpRoute Concatenate(OsmSharpRoute route1, OsmSharpRoute route2, bool clone) { if (route1 == null) { return(route2); } if (route2 == null) { return(route1); } if (route1.Entries.Length == 0) { return(route2); } if (route2.Entries.Length == 0) { return(route1); } if (route1.Vehicle != route2.Vehicle) { throw new ArgumentException("Route vechicles do not match!"); } // get the end/start point. RoutePointEntry end = route1.Entries[route1.Entries.Length - 1]; RoutePointEntry start = route2.Entries[0]; // only do all this if the routes are 'concatenable'. if (end.Latitude == start.Latitude && end.Longitude == start.Longitude) { // construct the new route. OsmSharpRoute route = new OsmSharpRoute(); // concatenate points. List <RoutePointEntry> entries = new List <RoutePointEntry>(); // add points for the first route except the last point. for (int idx = 0; idx < route1.Entries.Length - 1; idx++) { if (clone) { entries.Add(route1.Entries[idx].Clone() as RoutePointEntry); } else { entries.Add(route1.Entries[idx]); } } // merge last and first entry. RoutePointEntry mergedEntry = route1.Entries[route1.Entries.Length - 1].Clone() as RoutePointEntry; mergedEntry.Type = RoutePointEntryType.Along; if (route2.Entries[0].Points != null && route2.Entries[0].Points.Length > 0) { // merge in important points from the second route too but do not keep duplicates. List <RoutePoint> points = new List <RoutePoint>(mergedEntry.Points); for (int otherIdx = 0; otherIdx < route2.Entries[0].Points.Length; otherIdx++) { bool found = false; for (int idx = 0; idx < points.Count; idx++) { if (points[idx].RepresentsSame( route2.Entries[0].Points[otherIdx])) { // the points represent the same info! found = true; break; } } if (!found) { // the point was not in there yet! points.Add(route2.Entries[0].Points[otherIdx]); } } mergedEntry.Points = points.ToArray(); } entries.Add(mergedEntry); // add points of the next route. for (int idx = 1; idx < route2.Entries.Length; idx++) { if (clone) { entries.Add(route2.Entries[idx].Clone() as RoutePointEntry); } else { entries.Add(route2.Entries[idx]); } } route.Entries = entries.ToArray(); // concatenate tags. List <RouteTags> tags = new List <RouteTags>(); if (route1.Tags != null) { tags.AddRange(route1.Tags); } if (route2.Tags != null) { tags.AddRange(route2.Tags); } route.Tags = tags.ToArray(); //// calculate metrics. //Routing.Core.Metrics.Time.TimeCalculator calculator = new OsmSharp.Routing.Metrics.Time.TimeCalculator(); //Dictionary<string, double> metrics = calculator.Calculate(route); //route.TotalDistance = metrics[Routing.Core.Metrics.Time.TimeCalculator.DISTANCE_KEY]; //route.TotalTime = metrics[Routing.Core.Metrics.Time.TimeCalculator.TIME_KEY]; // set the vehicle. route.Vehicle = route1.Vehicle; return(route); } else { throw new ArgumentOutOfRangeException("Contatenation routes can only be done when the end point of the first route equals the start of the second."); } }
/// <summary> /// Concatenates two routes. /// </summary> /// <param name="route1"></param> /// <param name="route2"></param> /// <returns></returns> public static OsmSharpRoute Concatenate(OsmSharpRoute route1, OsmSharpRoute route2) { return(OsmSharpRoute.Concatenate(route1, route2, true)); }
/// <summary> /// Loads a route from file. /// </summary> /// <param name="info"></param> /// <returns></returns> public static OsmSharpRoute Load(FileInfo info) { return(OsmSharpRoute.Load(info.OpenRead())); }
/// <summary> /// Concatenates two routes. /// </summary> /// <param name="route1"></param> /// <param name="route2"></param> /// <param name="clone"></param> /// <returns></returns> public static OsmSharpRoute Concatenate(OsmSharpRoute route1, OsmSharpRoute route2, bool clone) { if (route1 == null) return route2; if (route2 == null) return route1; if (route1.Entries.Length == 0) return route2; if (route2.Entries.Length == 0) return route1; // get the end/start point. RoutePointEntry end = route1.Entries[route1.Entries.Length - 1]; RoutePointEntry start = route2.Entries[0]; // only do all this if the routes are 'concatenable'. if (end.Latitude == start.Latitude && end.Longitude == start.Longitude) { // construct the new route. OsmSharpRoute route = new OsmSharpRoute(); // concatenate points. List<RoutePointEntry> entries = new List<RoutePointEntry>(); // add points for the first route except the last point. for (int idx = 0; idx < route1.Entries.Length - 1; idx++) { if (clone) { entries.Add(route1.Entries[idx].Clone() as RoutePointEntry); } else { entries.Add(route1.Entries[idx]); } } // add points of the next route. for (int idx = 0; idx < route2.Entries.Length; idx++) { if (clone) { entries.Add(route2.Entries[idx].Clone() as RoutePointEntry); } else { entries.Add(route2.Entries[idx]); } } route.Entries = entries.ToArray(); // concatenate tags. List<RouteTags> tags = new List<RouteTags>(); if (route1.Tags != null) { tags.AddRange(route1.Tags); } if (route2.Tags != null) { tags.AddRange(route2.Tags); } route.Tags = tags.ToArray(); //// calculate metrics. //Routing.Core.Metrics.Time.TimeCalculator calculator = new OsmSharp.Routing.Metrics.Time.TimeCalculator(); //Dictionary<string, double> metrics = calculator.Calculate(route); //route.TotalDistance = metrics[Routing.Core.Metrics.Time.TimeCalculator.DISTANCE_KEY]; //route.TotalTime = metrics[Routing.Core.Metrics.Time.TimeCalculator.TIME_KEY]; return route; } else { throw new ArgumentOutOfRangeException("Contatenation routes can only be done when the end point of the first route equals the start of the second."); } }
/// <summary> /// Concatenates two routes. /// </summary> /// <param name="route1"></param> /// <param name="route2"></param> /// <returns></returns> public static OsmSharpRoute Concatenate(OsmSharpRoute route1, OsmSharpRoute route2) { return OsmSharpRoute.Concatenate(route1, route2, true); }
public GeoCoordinateBox AddRoute(OsmSharpRoute route, Color? color) { return this.AddRoute(route, color, OsmSharpRouteLayer.DEFAULT_ROUTE_WIDTH); }
public void RouteConcatenateTagsTest() { OsmSharpRoute route1 = new OsmSharpRoute(); RoutePointEntry route1entry1 = new RoutePointEntry(); route1entry1.Distance = 10; route1entry1.Latitude = -1; route1entry1.Longitude = -1; route1entry1.Metrics = null; route1entry1.Points = new RoutePoint[1]; route1entry1.Points[0] = new RoutePoint(); route1entry1.Points[0].Name = "TestPoint1"; route1entry1.Points[0].Tags = new RouteTags[1]; route1entry1.Points[0].Tags[0] = new RouteTags(); route1entry1.Points[0].Tags[0].Value = "TestValue1"; route1entry1.Points[0].Tags[0].Key = "TestKey1"; route1entry1.SideStreets = null; route1entry1.Tags = new RouteTags[1]; route1entry1.Tags[0] = new RouteTags(); route1entry1.Tags[0].Key = "highway"; route1entry1.Tags[0].Value = "residential"; route1entry1.Time = 10; route1entry1.Type = RoutePointEntryType.Start; route1entry1.WayFromName = string.Empty; route1entry1.WayFromNames = null; RoutePointEntry route1entry2 = new RoutePointEntry(); route1entry2.Distance = 10; route1entry2.Latitude = -1; route1entry2.Longitude = -1; route1entry2.Metrics = null; route1entry2.Points = new RoutePoint[1]; route1entry2.Points[0] = new RoutePoint(); route1entry2.Points[0].Name = "TestPoint2"; route1entry2.Points[0].Tags = new RouteTags[1]; route1entry2.Points[0].Tags[0] = new RouteTags(); route1entry2.Points[0].Tags[0].Value = "TestValue2"; route1entry1.Points[0].Tags[0].Key = "TestKey2"; route1entry2.SideStreets = null; route1entry2.Tags = new RouteTags[1]; route1entry2.Tags[0] = new RouteTags(); route1entry2.Tags[0].Key = "highway"; route1entry2.Tags[0].Value = "residential"; route1entry2.Time = 10; route1entry2.Type = RoutePointEntryType.Start; route1entry2.WayFromName = string.Empty; route1entry2.WayFromNames = null; route1.Entries = new RoutePointEntry[2]; route1.Entries[0] = route1entry1; route1.Entries[1] = route1entry2; OsmSharpRoute route2 = new OsmSharpRoute(); RoutePointEntry route2entry1 = new RoutePointEntry(); route2entry1.Distance = 10; route2entry1.Latitude = -1; route2entry1.Longitude = -1; route2entry1.Metrics = null; route2entry1.Points = new RoutePoint[1]; route2entry1.Points[0] = new RoutePoint(); route2entry1.Points[0].Name = "TestPoint3"; route2entry1.Points[0].Tags = new RouteTags[1]; route2entry1.Points[0].Tags[0] = new RouteTags(); route2entry1.Points[0].Tags[0].Value = "TestValue3"; route2entry1.Points[0].Tags[0].Key = "TestKey3"; route2entry1.SideStreets = null; route2entry1.Tags = new RouteTags[1]; route2entry1.Tags[0] = new RouteTags(); route2entry1.Tags[0].Key = "highway"; route2entry1.Tags[0].Value = "residential"; route2entry1.Time = 10; route2entry1.Type = RoutePointEntryType.Start; route2entry1.WayFromName = string.Empty; route2entry1.WayFromNames = null; RoutePointEntry route2entry2 = new RoutePointEntry(); route2entry2.Distance = 10; route2entry2.Latitude = -1; route2entry2.Longitude = -1; route2entry2.Metrics = null; route2entry2.Points = new RoutePoint[1]; route2entry2.Points[0] = new RoutePoint(); route2entry2.Points[0].Name = "TestPoint4"; route2entry2.Points[0].Tags = new RouteTags[1]; route2entry2.Points[0].Tags[0] = new RouteTags(); route2entry2.Points[0].Tags[0].Value = "TestValue4"; route2entry1.Points[0].Tags[0].Key = "TestKey4"; route2entry2.SideStreets = null; route2entry2.Tags = new RouteTags[1]; route2entry2.Tags[0] = new RouteTags(); route2entry2.Tags[0].Key = "highway"; route2entry2.Tags[0].Value = "residential"; route2entry2.Time = 10; route2entry2.Type = RoutePointEntryType.Start; route2entry2.WayFromName = string.Empty; route2entry2.WayFromNames = null; route2.Entries = new RoutePointEntry[2]; route2.Entries[0] = route2entry1; route2.Entries[1] = route2entry2; OsmSharpRoute concatenated = OsmSharpRoute.Concatenate(route1, route2); // test the result. Assert.IsNotNull(concatenated); Assert.IsNotNull(concatenated.Entries); Assert.AreEqual(3, concatenated.Entries.Length); Assert.AreEqual("TestPoint1", concatenated.Entries[0].Points[0].Name); Assert.AreEqual("TestPoint3", concatenated.Entries[1].Points[0].Name); Assert.AreEqual("TestPoint4", concatenated.Entries[2].Points[0].Name); }