Ejemplo n.º 1
0
        /// <summary>
        /// Clones this object.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            RoutePointEntry clone = new RoutePointEntry();

            clone.Distance  = this.Distance;
            clone.Latitude  = this.Latitude;
            clone.Longitude = this.Longitude;
            if (this.Metrics != null)
            {
                clone.Metrics = new RouteMetric[this.Metrics.Length];
                for (int idx = 0; idx < this.Metrics.Length; idx++)
                {
                    clone.Metrics[idx] = this.Metrics[idx].Clone() as RouteMetric;
                }
            }
            if (this.Points != null)
            {
                clone.Points = new RoutePoint[this.Points.Length];
                for (int idx = 0; idx < this.Points.Length; idx++)
                {
                    clone.Points[idx] = this.Points[idx].Clone() as RoutePoint;
                }
            }
            if (this.SideStreets != null)
            {
                clone.SideStreets = new RoutePointEntrySideStreet[this.SideStreets.Length];
                for (int idx = 0; idx < this.SideStreets.Length; idx++)
                {
                    clone.SideStreets[idx] = this.SideStreets[idx].Clone() as RoutePointEntrySideStreet;
                }
            }
            if (this.Tags != null)
            {
                clone.Tags = new RouteTags[this.Tags.Length];
                for (int idx = 0; idx < this.Tags.Length; idx++)
                {
                    clone.Tags[idx] = this.Tags[idx].Clone() as RouteTags;
                }
            }
            clone.Time        = this.Time;
            clone.Type        = this.Type;
            clone.WayFromName = this.WayFromName;
            if (this.WayFromNames != null)
            {
                clone.WayFromNames = new RouteTags[this.WayFromNames.Length];
                for (int idx = 0; idx < this.WayFromNames.Length; idx++)
                {
                    clone.WayFromNames[idx] = this.WayFromNames[idx].Clone() as RouteTags;
                }
            }
            return(clone);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Clones this object.
 /// </summary>
 /// <returns></returns>
 public object Clone()
 {
     RoutePointEntry clone = new RoutePointEntry();
     clone.Distance = this.Distance;
     clone.Latitude = this.Latitude;
     clone.Longitude = this.Longitude;
     if (this.Metrics != null)
     {
         clone.Metrics = new RouteMetric[this.Metrics.Length];
         for (int idx = 0; idx < this.Metrics.Length; idx++)
         {
             clone.Metrics[idx] = this.Metrics[idx].Clone() as RouteMetric;
         }
     }
     if (this.Points != null)
     {
         clone.Points = new RoutePoint[this.Points.Length];
         for (int idx = 0; idx < this.Points.Length; idx++)
         {
             clone.Points[idx] = this.Points[idx].Clone() as RoutePoint;
         }
     }
     if (this.SideStreets != null)
     {
         clone.SideStreets = new RoutePointEntrySideStreet[this.SideStreets.Length];
         for (int idx = 0; idx < this.SideStreets.Length; idx++)
         {
             clone.SideStreets[idx] = this.SideStreets[idx].Clone() as RoutePointEntrySideStreet;
         }
     }
     if (this.Tags != null)
     {
         clone.Tags = new RouteTags[this.Tags.Length];
         for (int idx = 0; idx < this.Tags.Length; idx++)
         {
             clone.Tags[idx] = this.Tags[idx].Clone() as RouteTags;
         }
     }
     clone.Time = this.Time;
     clone.Type = this.Type;
     clone.WayFromName = this.WayFromName;
     if (this.WayFromNames != null)
     {
         clone.WayFromNames = new RouteTags[this.WayFromNames.Length];
         for (int idx = 0; idx < this.WayFromNames.Length; idx++)
         {
             clone.WayFromNames[idx] = this.WayFromNames[idx].Clone() as RouteTags;
         }
     }
     return clone;
 }
Ejemplo n.º 3
0
        /// <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.");
            }
        }
Ejemplo n.º 4
0
        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);
        }