Esempio n. 1
0
        public void TestBollardUncontractedSimpleRestrictions()
        {
            var routerDb = new RouterDb();

            using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                       "Itinero.Test.test_data.networks.network10.osm"))
            {
                var source = new OsmSharp.Streams.XmlOsmStreamSource(stream);
                routerDb.LoadOsmData(source, Itinero.Osm.Vehicles.Vehicle.Car);
            }

            var profile = routerDb.GetSupportedProfile("car");

            var location1 = new Coordinate(51.22735657780183f, 4.834375977516174f);
            var location2 = new Coordinate(51.22702399914085f, 4.833759069442749f);
            var location3 = new Coordinate(51.22759509233135f, 4.833576679229736f);
            var router    = new Router(routerDb);

            var resolved1 = router.Resolve(profile, location1);
            var resolved2 = router.Resolve(profile, location2);
            var resolved3 = router.Resolve(profile, location3);

            var route1 = router.TryCalculate(profile, resolved1, resolved2);

            Assert.IsTrue(route1.IsError);
            var route2 = router.TryCalculate(profile, resolved1, resolved3);

            Assert.IsFalse(route2.IsError);
        }
Esempio n. 2
0
        /// <summary>
        /// Runs a very simple test with a fictional network with negative id's.
        /// </summary>
        public static void RunFictional()
        {
            var stream   = new OsmSharp.Streams.XmlOsmStreamSource(File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tests/fictional.osm")));
            var routerDb = new RouterDb();

            routerDb.LoadOsmData(stream, Itinero.Osm.Vehicles.Vehicle.Car);

            var router = new Router(routerDb);

            Assert.IsNotNull(router.Calculate(Itinero.Osm.Vehicles.Vehicle.Car.Fastest(),
                                              new Coordinate(51.25758522843f, 4.43582452680f),
                                              new Coordinate(51.25669892931f, 4.44101728345f)));
        }
Esempio n. 3
0
        /// <summary>
        /// Builds a routerdb.
        /// </summary>
        /// <returns></returns>
        public static RouterDb Build(string queryName, Vehicle vehicle)
        {
            Download.ToFile(queryName);

            var fileName = queryName + ".osm";

            RouterDb routerDb = null;

            Itinero.Logging.Logger.Log("RouterDbBuilder", Itinero.Logging.TraceEventType.Information, "No existing RouterDb file found, creating now.");
            using (var stream = File.OpenRead(fileName))
            {
                var xmlStream  = new OsmSharp.Streams.XmlOsmStreamSource(stream);
                var sortedData = xmlStream.ToList();
                sortedData.Sort((x, y) =>
                {
                    if (x.Type == y.Type)
                    {
                        return(x.Id.Value.CompareTo(y.Id.Value));
                    }
                    if (x.Type == OsmSharp.OsmGeoType.Node)
                    {
                        return(-1);
                    }
                    else if (x.Type == OsmSharp.OsmGeoType.Way)
                    {
                        if (y.Type == OsmSharp.OsmGeoType.Node)
                        {
                            return(1);
                        }
                        return(-1);
                    }
                    return(1);
                });

                routerDb = new RouterDb();
                routerDb.LoadOsmData(sortedData, vehicle);
            }

            Itinero.Logging.Logger.Log("RouterDbBuilder", Itinero.Logging.TraceEventType.Information, "RouterDb file created.");

            if (!routerDb.HasContractedFor(vehicle.Fastest()))
            {
                Itinero.Logging.Logger.Log("RouterDbBuilder", Itinero.Logging.TraceEventType.Information, "No contracted graph found for the 'car' profile, building now...");
                routerDb.AddContracted(vehicle.Fastest(), true);
            }
            return(routerDb);
        }
Esempio n. 4
0
        /// <summary>
        /// Builds a routerdb from an overpass query.
        /// </summary>
        public static RouterDb BuildRouterDbOverpass(string query, Vehicle vehicle)
        {
            var fileName = DownloadOverpass(query, "temp");

            RouterDb routerDb;

            using (var stream = File.OpenRead(fileName))
            {
                var xmlStream  = new OsmSharp.Streams.XmlOsmStreamSource(stream);
                var sortedData = xmlStream.ToList();
                sortedData.Sort((x, y) =>
                {
                    if (x.Type == y.Type)
                    {
                        return(x.Id.Value.CompareTo(y.Id.Value));
                    }
                    if (x.Type == OsmSharp.OsmGeoType.Node)
                    {
                        return(-1);
                    }
                    else if (x.Type == OsmSharp.OsmGeoType.Way)
                    {
                        if (y.Type == OsmSharp.OsmGeoType.Node)
                        {
                            return(1);
                        }
                        return(-1);
                    }
                    return(1);
                });

                routerDb = new RouterDb();
                routerDb.LoadOsmData(sortedData, vehicle);
            }
            return(routerDb);
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            // enable logging.
            OsmSharp.Logging.Logger.LogAction = (origin, level, message, parameters) =>
            {
                var formattedMessage = $"{origin} - {message}";
                switch (level)
                {
                case "critical":
                    Log.Fatal(formattedMessage);
                    break;

                case "error":
                    Log.Error(formattedMessage);
                    break;

                case "warning":
                    Log.Warning(formattedMessage);
                    break;

                case "verbose":
                    Log.Verbose(formattedMessage);
                    break;

                case "information":
                    Log.Information(formattedMessage);
                    break;

                default:
                    Log.Debug(formattedMessage);
                    break;
                }
            };

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .WriteTo.Console()
                         .WriteTo.File(Path.Combine("logs", "log-.txt"), rollingInterval: RollingInterval.Day)
                         .CreateLogger();

            // download test data.
            Download.ToFile("http://files.itinero.tech/data/OSM/planet/europe/luxembourg-latest.osm.pbf", "test.osm.pbf").Wait();

            // test read/writing an existing OSM file.
            Log.Information("Testing reading/writing via OSM binary format...");
            using (var sourceStream = File.OpenRead("test.osm.pbf"))
                using (var targetStream = File.Open("test1.osm.bin", FileMode.Create))
                {
                    var source = new OsmSharp.Streams.PBFOsmStreamSource(sourceStream);

                    var target = new OsmSharp.Streams.BinaryOsmStreamTarget(targetStream);
                    target.RegisterSource(source);
                    target.Pull();
                }
            using (var sourceStream = File.OpenRead("test1.osm.bin"))
                using (var targetStream = File.Open("test2.osm.pbf", FileMode.Create))
                {
                    var source = new OsmSharp.Streams.BinaryOsmStreamSource(sourceStream);

                    var target = new OsmSharp.Streams.PBFOsmStreamTarget(targetStream);
                    target.RegisterSource(source);
                    target.Pull();
                }

            // test read/writing an existing OSM file via a compressed stream.
            Log.Information("Testing reading/writing via OSM binary format using a compressed stream...");
            using (var sourceStream = File.OpenRead("test.osm.pbf"))
                using (var targetStream = File.Open("test2.osm.bin.zip", FileMode.Create))
                    using (var targetStreamCompressed = new System.IO.Compression.DeflateStream(targetStream, CompressionLevel.Fastest))
                    {
                        var source = new OsmSharp.Streams.PBFOsmStreamSource(sourceStream);

                        var target = new OsmSharp.Streams.BinaryOsmStreamTarget(targetStreamCompressed);
                        target.RegisterSource(source);
                        target.Pull();
                    }

            using (var sourceStream = File.OpenRead("test2.osm.bin.zip"))
                using (var sourceStreamCompressed = new System.IO.Compression.DeflateStream(sourceStream, CompressionMode.Decompress))
                    using (var targetStream = File.Open("test2.osm.pbf", FileMode.Create))
                    {
                        var source = new OsmSharp.Streams.BinaryOsmStreamSource(sourceStreamCompressed);

                        var target = new OsmSharp.Streams.PBFOsmStreamTarget(targetStream);
                        target.RegisterSource(source);
                        target.Pull();
                    }

            // test reading/writing edited OSM-data.
            Log.Information("Testing reading/writing via OSM binary format of edited incomplete OSM data...");
            using (var sourceStream = File.OpenRead("./test-data/data.osm"))
                using (var targetStream = File.Open("test3.osm.bin", FileMode.Create))
                {
                    var source = new OsmSharp.Streams.XmlOsmStreamSource(sourceStream);

                    var target = new OsmSharp.Streams.BinaryOsmStreamTarget(targetStream);
                    target.RegisterSource(source);
                    target.Pull();
                }

            using (var sourceStream = File.OpenRead("test3.osm.bin"))
                using (var targetStream = File.Open("test2.osm.pbf", FileMode.Create))
                {
                    var source = new OsmSharp.Streams.BinaryOsmStreamSource(sourceStream);

                    var target = new OsmSharp.Streams.PBFOsmStreamTarget(targetStream);
                    target.RegisterSource(source);
                    target.Pull();
                }
        }