Example #1
0
        public void TestDeserialize()
        {
            var db = new StopLinksDb(5, new RouterDb(), "pedestrian");

            db.Add(0, new RouterPoint(0, 1, 0, 0));
            db.Add(0, new RouterPoint(2, 3, 0, 16));
            db.Add(0, new RouterPoint(4, 5, 0, 64));
            db.Add(0, new RouterPoint(6, 7, 0, 256));
            db.Add(0, new RouterPoint(8, 9, 0, 1024));

            using (var stream = new MemoryStream())
            {
                db.Serialize(stream);

                stream.Seek(0, SeekOrigin.Begin);
                var db1 = StopLinksDb.Deserialize(stream);

                Assert.AreEqual(db.SizeInBytes, db1.SizeInBytes);

                var enumerator  = db.GetEnumerator();
                var enumerator1 = db1.GetEnumerator();

                enumerator.MoveTo(0);
                enumerator1.MoveTo(0);
                while (enumerator.MoveNext())
                {
                    Assert.IsTrue(enumerator1.MoveNext());

                    Assert.AreEqual(enumerator.EdgeId, enumerator1.EdgeId);
                    Assert.AreEqual(enumerator.Offset, enumerator1.Offset);
                }
            }
        }
Example #2
0
        public void TestAdd()
        {
            var db = new StopLinksDb(1, new RouterDb(), string.Empty);

            db.Add(0, new RouterPoint(0, 0, 0, 0));

            var enumerator = db.GetEnumerator();

            enumerator.MoveTo(0);
            Assert.AreEqual(1, enumerator.Count);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(0, enumerator.EdgeId);
            Assert.AreEqual(0, enumerator.Offset);

            db.Add(1, new RouterPoint(0, 0, 1, ushort.MaxValue));

            enumerator = db.GetEnumerator();

            enumerator.MoveTo(0);
            Assert.AreEqual(1, enumerator.Count);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(0, enumerator.EdgeId);
            Assert.AreEqual(0, enumerator.Offset);

            enumerator.MoveTo(1);
            Assert.AreEqual(1, enumerator.Count);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(1, enumerator.EdgeId);
            Assert.AreEqual(ushort.MaxValue, enumerator.Offset);

            db.Add(1, new RouterPoint(0, 0, 2, ushort.MaxValue / 2));

            enumerator = db.GetEnumerator();

            enumerator.MoveTo(0);
            Assert.AreEqual(1, enumerator.Count);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(0, enumerator.EdgeId);
            Assert.AreEqual(0, enumerator.Offset);

            enumerator.MoveTo(1);
            Assert.AreEqual(2, enumerator.Count);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(1, enumerator.EdgeId);
            Assert.AreEqual(ushort.MaxValue, enumerator.Offset);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(2, enumerator.EdgeId);
            Assert.AreEqual(ushort.MaxValue / 2, enumerator.Offset);
        }
Example #3
0
        public void TestSerialize()
        {
            var db = new StopLinksDb(5, new RouterDb(), "pedestrian");

            db.Add(0, new RouterPoint(0, 1, 0, 0));
            db.Add(0, new RouterPoint(2, 3, 0, 16));
            db.Add(0, new RouterPoint(4, 5, 0, 64));
            db.Add(0, new RouterPoint(6, 7, 0, 256));
            db.Add(0, new RouterPoint(8, 9, 0, 1024));

            var profileBytes = System.Text.Encoding.Unicode.GetByteCount("pedestrian");

            var size = 1 + 8 + 8 + 16 + profileBytes + 8 + 5 * 2 * 4 + 5 * 2 * 4;

            using (var stream = new MemoryStream())
            {
                Assert.AreEqual(size, db.SizeInBytes);
                Assert.AreEqual(size, db.Serialize(stream));
            }
        }
Example #4
0
        public void TestSourceEdgeHasStop()
        {
            var routerDb = new RouterDb();

            routerDb.Network.AddVertex(0, 51.27018537520318f, 4.799609184265137f);
            routerDb.Network.AddVertex(1, 51.2682252886248f, 4.793150424957275f);
            routerDb.Network.AddEdge(0, 1,
                                     new Itinero.Data.Network.Edges.EdgeData()
            {
                Profile  = 0,
                MetaId   = 0,
                Distance = 500
            }, null);
            routerDb.EdgeProfiles.Add(new AttributeCollection());
            routerDb.EdgeMeta.Add(new AttributeCollection());

            var profile = VehicleMock.Car(x => new FactorAndSpeed()
            {
                Direction   = 0,
                Value       = .1f,
                SpeedFactor = .1f
            }).Fastest();

            var stopLinksDb = new StopLinksDb(1, routerDb, profile);

            stopLinksDb.Add(0, new RouterPoint(51.269138216062984f, 4.796175956726074f, 0, ushort.MaxValue / 2));
            var transitDb    = new TransitDb();
            var multimodalDb = new MultimodalDb(routerDb, transitDb);

            multimodalDb.AddStopLinksDb(stopLinksDb);
            multimodalDb.TransitDb.AddStop(51.269138216062984f, 4.796175956726074f, 0);

            var stopFound         = false;
            var closestStopSearch = new ClosestStopsSearch(multimodalDb,
                                                           profile, new RouterPoint(51.269138216062984f, 4.796175956726074f, 0, ushort.MaxValue / 2), 1800, false);

            closestStopSearch.StopFound = (uint stopId, float seconds) =>
            {
                if (!stopFound)
                {
                    Assert.AreEqual(0, stopId);
                    Assert.AreEqual(0, seconds);
                    stopFound = true;
                }
                return(false);
            };
            closestStopSearch.Run();

            Assert.IsTrue(closestStopSearch.HasRun);
            Assert.IsTrue(closestStopSearch.HasSucceeded);
            Assert.IsTrue(stopFound);

            var path = closestStopSearch.GetPath(0);

            Assert.IsNotNull(path);
            Assert.AreEqual(Itinero.Constants.NO_VERTEX, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);
            var point = closestStopSearch.GetTargetPoint(0);

            Assert.IsNotNull(point);
            Assert.AreEqual(0, point.EdgeId);
            Assert.AreEqual(ushort.MaxValue / 2, point.Offset);

            stopFound         = false;
            closestStopSearch = new ClosestStopsSearch(multimodalDb,
                                                       profile, new RouterPoint(51.27018537520318f, 4.799609184265137f, 0, 0), 1800, false);
            closestStopSearch.StopFound = (uint stopId, float seconds) =>
            {
                Assert.AreEqual(0, stopId);
                Assert.AreEqual(profile.Factor(null).Value * 250, seconds, .1f);
                stopFound = true;
                return(false);
            };
            closestStopSearch.Run();

            Assert.IsTrue(stopFound);

            path = closestStopSearch.GetPath(0);
            Assert.IsNotNull(path);
            Assert.AreEqual(Itinero.Constants.NO_VERTEX, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(0, path.From.Vertex);
            Assert.AreEqual(profile.Factor(null).Value * 250, path.Weight, .1f);
            point = closestStopSearch.GetTargetPoint(0);
            Assert.IsNotNull(point);
            Assert.AreEqual(0, point.EdgeId);
            Assert.AreEqual(ushort.MaxValue / 2, point.Offset);
        }
Example #5
0
        public void TestNetwork2()
        {
            var routerDb = new RouterDb();

            routerDb.LoadTestNetwork(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "Itinero.Transit.Test.test_data.networks.network2.geojson"));

            var profile = VehicleMock.Car(x => new FactorAndSpeed()
            {
                Direction   = 0,
                Value       = 10f,
                SpeedFactor = 10f
            }).Fastest();

            var stopLocation = new Coordinate(51.229621576122774f, 4.464208334684372f);
            var stopLinksDb  = new StopLinksDb(1, routerDb, profile);

            stopLinksDb.Add(0, new RouterPoint((float)stopLocation.Latitude, (float)stopLocation.Longitude, 1,
                                               ushort.MaxValue / 2));
            var transitDb    = new TransitDb();
            var multimodalDb = new MultimodalDb(routerDb, transitDb);

            multimodalDb.AddStopLinksDb(stopLinksDb);
            multimodalDb.TransitDb.AddStop(51.229621576122774f, 4.464208334684372f, 0);

            var stopFound      = false;
            var distanceToStop = Coordinate.DistanceEstimateInMeter(routerDb.Network.GetVertex(0),
                                                                    routerDb.Network.GetVertex(1)) + Coordinate.DistanceEstimateInMeter(routerDb.Network.GetVertex(1),
                                                                                                                                        stopLocation);
            var closestStopSearch = new ClosestStopsSearch(multimodalDb,
                                                           profile, routerDb.Network.CreateRouterPointForVertex(0, 1), 3600, false);

            closestStopSearch.StopFound = (uint stopId, float seconds) =>
            {
                Assert.AreEqual(0, stopId);
                Assert.AreEqual(distanceToStop * 10, seconds, 1);
                stopFound = true;
                return(false);
            };
            closestStopSearch.Run();

            Assert.IsTrue(stopFound);

            var path = closestStopSearch.GetPath(0);

            Assert.IsNotNull(path);
            Assert.AreEqual(distanceToStop * 10, path.Weight, 1);
            Assert.AreEqual(Itinero.Constants.NO_VERTEX, path.Vertex);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(routerDb.Network.GetVertex(0),
                                                               routerDb.Network.GetVertex(1)) * 10, path.Weight, 1);
            Assert.AreEqual(1, path.Vertex);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.01);
            Assert.AreEqual(0, path.Vertex);
            path = path.From;
            Assert.IsNull(path);
        }