/// <summary>
        /// Tests preprocessing data from a PBF file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pbfFile"></param>
        public static void TestSerialization(string name, string pbfFile)
        {
            FileInfo testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile));
            Stream stream = testFile.OpenRead();
            PBFOsmStreamSource source = new PBFOsmStreamSource(stream);

            FileInfo testOutputFile = new FileInfo(@"test.routing");
            testOutputFile.Delete();
            Stream writeStream = testOutputFile.OpenWrite();

            CHEdgeGraphFileStreamTarget target = new CHEdgeGraphFileStreamTarget(writeStream,
                Vehicle.Car);
            target.RegisterSource(source);

            PerformanceInfoConsumer performanceInfo = new PerformanceInfoConsumer("CHSerializer");
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var data = CHEdgeGraphOsmStreamTarget.Preprocess(
                source, new OsmRoutingInterpreter(), Vehicle.Car);

            TagsCollectionBase metaData = new TagsCollection();
            metaData.Add("some_key", "some_value");
            var routingSerializer = new CHEdgeDataDataSourceSerializer(true);
            routingSerializer.Serialize(writeStream, data, metaData);

            stream.Dispose();
            writeStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("CHSerializer", OsmSharp.Logging.TraceEventType.Information,
                string.Format("Serialized file: {0}KB", testOutputFile.Length / 1024));

            performanceInfo.Stop();
        }
        static void Main(string[] args)
        {
            // enable logging and use the console as output.
            OsmSharp.Logging.Log.Enable();
            OsmSharp.Logging.Log.RegisterListener(
                new OsmSharp.WinForms.UI.Logging.ConsoleTraceListener());

            // create router.
            using (var source = new FileInfo(@"kempen.osm.pbf").OpenRead())
            {
                var pbfSource = new PBFOsmStreamSource(source);
                var progress = new OsmStreamFilterProgress();
                progress.RegisterSource(pbfSource);
                var router = Router.CreateCHFrom(progress, new OsmRoutingInterpreter(), Vehicle.Car);

                OsmSharp.Service.Routing.ApiBootstrapper.Add("default", router);
            }

            var uri = new Uri("http://*****:*****@ http://localhost:1234");
                System.Diagnostics.Process.Start("http://localhost:1234/default");

                Console.ReadLine();
            }
        }
 /// <summary>
 /// Executes a test adding tags from a PBF file.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="index"></param>
 /// <param name="pbfFile"></param>
 public static void Test(string name, ITagsCollectionIndex index, string pbfFile)
 {
     FileInfo testFile = new FileInfo(string.Format(@".\TestFiles\{0}",pbfFile));
     Stream stream = testFile.OpenRead();
     PBFOsmStreamSource source = new PBFOsmStreamSource(stream);
     ITagCollectionIndexTests.TestAdd(name, index, source);
 }
Example #4
0
        /// <summary>
        /// Tests routing from a serialized routing file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="stream"></param>
        /// <param name="testCount"></param>
        public static void TestSerializedRouting(string name, Stream stream, int testCount)
        {
            var vehicle = Vehicle.Car;

            var tagsIndex = new TagsTableCollectionIndex(); // creates a tagged index.

            // read from the OSM-stream.
            var reader      = new OsmSharp.Osm.PBF.Streams.PBFOsmStreamSource(stream);
            var interpreter = new OsmRoutingInterpreter();
            var data        = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);

            data.DropVertexIndex();
            var targetData = new LiveGraphOsmStreamTarget(data, interpreter, tagsIndex);

            targetData.RegisterSource(reader);
            targetData.Pull();
            data.RebuildVertexIndex();

            // creates the live edge router.
            var router = new Dykstra();

            var performanceInfo = new PerformanceInfoConsumer("LiveRouting");

            performanceInfo.Start();
            performanceInfo.Report("Routing {0} routes...", testCount);

            var successCount   = 0;
            var totalCount     = testCount;
            var latestProgress = -1.0f;

            while (testCount > 0)
            {
                var from = (uint)OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(data.VertexCount - 1) + 1;
                var to   = (uint)OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(data.VertexCount - 1) + 1;

                var route = router.Calculate(data, interpreter, vehicle, from, to);

                if (route != null)
                {
                    successCount++;
                }
                testCount--;

                // report progress.
                var progress = (float)System.Math.Round(((double)(totalCount - testCount) / (double)totalCount) * 100);
                if (progress != latestProgress)
                {
                    OsmSharp.Logging.Log.TraceEvent("LiveRouting", TraceEventType.Information,
                                                    "Routing... {0}%", progress);
                    latestProgress = progress;
                }
            }
            performanceInfo.Stop();

            OsmSharp.Logging.Log.TraceEvent("LiveRouting", OsmSharp.Logging.TraceEventType.Information,
                                            string.Format("{0}/{1} routes successfull!", successCount, totalCount));
        }
        static void Main(string[] args)
        {
            // enable logging and use the console as output.
            OsmSharp.Logging.Log.Enable();
            OsmSharp.Logging.Log.RegisterListener(
                new OsmSharp.WinForms.UI.Logging.ConsoleTraceListener());

            // create router.
            using (var source = new FileInfo(@"kempen.osm.pbf").OpenRead())
            {
                var pbfSource = new PBFOsmStreamSource(source);
                var progress = new OsmStreamFilterProgress();
                progress.RegisterSource(pbfSource);
                var router = Router.CreateFrom(progress, new OsmRoutingInterpreter());

                OsmSharp.Service.Routing.ApiBootstrapper.Add("default", router);
            }

            // initialize mapcss interpreter.
            var mapCSSInterpreter = new MapCSSInterpreter(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Service.Routing.Sample.SelfHost.custom.mapcss"),
                new MapCSSDictionaryImageSource());

            using (var source = new FileInfo(@"kempen.osm.pbf").OpenRead())
            {
                var pbfSource = new PBFOsmStreamSource(source);
                var scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), new List<float>(new float[] {
                16, 14, 12, 10 }));
                var target = new StyleOsmStreamSceneTarget(
                    mapCSSInterpreter, scene, new WebMercator());
                var progress = new OsmStreamFilterProgress();
                progress.RegisterSource(pbfSource);
                target.RegisterSource(progress);
                target.Pull();

                // create a new instance (with a cache).
                var instance = new RenderingInstance();
                instance.Map.AddLayer(new LayerScene(scene));

                // add a default test instance.
                OsmSharp.Service.Tiles.ApiBootstrapper.AddInstance("default", instance);
            }

            var uri = new Uri("http://*****:*****@ http://localhost:1234");
                System.Diagnostics.Process.Start("http://localhost:1234/default");

                Console.ReadLine();
            }
        }
Example #6
0
        /// <summary>
        /// Tests routing from a serialized routing file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="stream"></param>
        /// <param name="testCount"></param>
        public static void TestSerializedRouting(string name, Stream stream, int testCount)
        {
            var vehicle = Vehicle.Car;

            var tagsIndex = new TagsTableCollectionIndex(); // creates a tagged index.

            // read from the OSM-stream.
            var reader = new OsmSharp.Osm.PBF.Streams.PBFOsmStreamSource(stream);
            var interpreter = new OsmRoutingInterpreter();
            var data = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex);
            data.DropVertexIndex();
            var targetData = new LiveGraphOsmStreamTarget(data, interpreter, tagsIndex);
            targetData.RegisterSource(reader);
            targetData.Pull();
            data.RebuildVertexIndex();

            // creates the live edge router.
            var router = new Dykstra();

            var performanceInfo = new PerformanceInfoConsumer("LiveRouting");
            performanceInfo.Start();
            performanceInfo.Report("Routing {0} routes...", testCount);

            var successCount = 0;
            var totalCount = testCount;
            var latestProgress = -1.0f;
            while (testCount > 0)
            {
                var from = (uint)OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(data.VertexCount - 1) + 1;
                var to = (uint)OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(data.VertexCount - 1) + 1;

                var route = router.Calculate(data, interpreter, vehicle, from, to);

                if (route != null)
                {
                    successCount++;
                }
                testCount--;

                // report progress.
                var progress = (float)System.Math.Round(((double)(totalCount - testCount) / (double)totalCount) * 100);
                if (progress != latestProgress)
                {
                    OsmSharp.Logging.Log.TraceEvent("LiveRouting", TraceEventType.Information,
                        "Routing... {0}%", progress);
                    latestProgress = progress;
                }
            }
            performanceInfo.Stop();

            OsmSharp.Logging.Log.TraceEvent("LiveRouting", OsmSharp.Logging.TraceEventType.Information,
                string.Format("{0}/{1} routes successfull!", successCount, totalCount));
        }
Example #7
0
        /// <summary>
        /// Tests preprocessing data from a PBF file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pbfFile"></param>
        public static void TestPreprocessing(string name, string pbfFile)
        {
            FileInfo testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile));
            Stream stream = testFile.OpenRead();
            PBFOsmStreamSource source = new PBFOsmStreamSource(stream);

            PerformanceInfoConsumer performanceInfo = new PerformanceInfoConsumer("CHPreProcessor.Pre");
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            Router.CreateCHFrom(source, new OsmRoutingInterpreter(), Vehicle.Car);

            stream.Dispose();

            performanceInfo.Stop();
        }
Example #8
0
        /// <summary>
        /// Executes reading tests.
        /// </summary>
        public static void Test()
        {
            FileInfo testFile = new FileInfo(@".\TestFiles\kempen.osm.pbf");
            Stream stream = testFile.OpenRead();
            PBFOsmStreamSource source = new PBFOsmStreamSource(stream);
            OsmStreamTargetEmpty emptyTarget = new OsmStreamTargetEmpty();
            emptyTarget.RegisterSource(source);

            PerformanceInfoConsumer performanceInfo = new PerformanceInfoConsumer("PBFOsmStreamSource.Pull");
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            emptyTarget.Pull();
            stream.Dispose();

            performanceInfo.Stop();
        }
Example #9
0
 public Task<List<CompleteWay>> GetAllHighways(string osmFilePath)
 {
     return Task.Run(() =>
     {
         using (var stream = _fileSystemHelper.FileOpenRead(osmFilePath))
         {
             _logger.Info($"Reading {osmFilePath} to memory - extracting only highways.");
             var source = new PBFOsmStreamSource(stream);
             var completeSource = new OsmSimpleCompleteStreamSource(source);
             var higways = completeSource
                 .OfType<CompleteWay>()
                 .Where(o => o.Tags.ContainsKey("highway"))
                 .ToList();
             _logger.Info("Finished getting highways. " + higways.Count);
             return higways;
         }
     });
 }
Example #10
0
 public Task<Dictionary<string, List<ICompleteOsmGeo>>> GetElementsWithName(string osmFilePath)
 {
     return Task.Run(() =>
     {
         using (var stream = _fileSystemHelper.FileOpenRead(osmFilePath))
         {
             _logger.Info($"Reading {osmFilePath} to memory - extracting only elements with name.");
             var source = new PBFOsmStreamSource(stream);
             var completeSource = new OsmSimpleCompleteStreamSource(source);
             var namesDictionary = completeSource
                 .Where(o => string.IsNullOrWhiteSpace(GetName(o)) == false)
                 .GroupBy(GetName)
                 .ToDictionary(g => g.Key, g => g.ToList());
             _logger.Info("Finished grouping data by name.");
             return namesDictionary;
         }
     });
 }
        /// <summary>
        /// Tests a few boundingbox queries.
        /// </summary>
        protected void TestBoundingBoxQueries()
        {
            this.NotifyEmptyExpected(); // empty test database.

            // create the target and pull the data from the test-file into the sqlite database.
            OsmStreamTarget target = this.CreateDataStreamTarget();
            PBFOsmStreamSource source = new PBFOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Data.Test.Unittests.Data.Osm.test.osm.pbf"));
            target.RegisterSource(source);
            target.Pull();

            IDataSourceReadOnly dataSource = this.CreateDataSource();
            MemoryDataSource memorySource = MemoryDataSource.CreateFrom(source);

            // get reference data and compare to the real thing!
            GeoCoordinateBox box = memorySource.BoundingBox;
            IList<OsmGeo> referenceBoxData = memorySource.Get(box, null);
            IList<OsmGeo> boxData = dataSource.Get(box, null);
            this.CompareResults(referenceBoxData, boxData);

            // increase box size and compare again.
            box = memorySource.BoundingBox.Resize(0.1);
            referenceBoxData = memorySource.Get(box, null);
            boxData = dataSource.Get(box, null);
            this.CompareResults(referenceBoxData, boxData);

            // descrese box size and compare again.
            box = memorySource.BoundingBox.Scale(0.5);
            referenceBoxData = memorySource.Get(box, null);
            boxData = dataSource.Get(box, null);
            this.CompareResults(referenceBoxData, boxData);

            // descrese box size and compare again.
            box = memorySource.BoundingBox.Scale(0.25);
            referenceBoxData = memorySource.Get(box, null);
            boxData = dataSource.Get(box, null);
            this.CompareResults(referenceBoxData, boxData);

            // descrese box size and compare again.
            box = memorySource.BoundingBox.Scale(0.1);
            referenceBoxData = memorySource.Get(box, null);
            boxData = dataSource.Get(box, null);
            this.CompareResults(referenceBoxData, boxData);
        }
        public void MoveNextWayRegression1()
        {
            using (var fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "OsmSharp.Test.data.api.osm.pbf"))
            {
                using (var reader = new PBFOsmStreamSource(fileStream))
                {
                    reader.Initialize();
                    var counter = 0;
                    while (reader.MoveNextWay())
                    {
                        if (counter++ % 10000 == 0)
                        {

                        }
                    }
                }
            }
        }
        /// <summary>
        /// Executes reading tests.
        /// </summary>
        public static void Test()
        {
            var testFile = new FileInfo(@".\TestFiles\kempen-big.osm.pbf");
            var stream = testFile.OpenRead();
            var source = new PBFOsmStreamSource(stream);
            var progress = new OsmStreamFilterProgress();
            progress.RegisterSource(source);
            var completeSource = new OsmSharp.Osm.Streams.Complete.OsmSimpleCompleteStreamSource(progress);

            PerformanceInfoConsumer performanceInfo = new PerformanceInfoConsumer("OsmSimpleCompleteStreamSourceTests.Pull");
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var completeObjects = new List<ICompleteOsmGeo>(completeSource);

            stream.Dispose();

            performanceInfo.Stop();
        }
Example #14
0
        /// <summary>
        /// Tests routing from a serialized routing file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="stream"></param>
        /// <param name="testCount"></param>
        public static void Test(string name, Stream stream, int testCount)
        {
            var vehicle = Vehicle.Pedestrian;

            var tagsIndex = new TagsIndex(); // creates a tagged index.

            // read from the OSM-stream.
            var reader      = new OsmSharp.Osm.PBF.Streams.PBFOsmStreamSource(stream);
            var interpreter = new OsmRoutingInterpreter();
            var data        = new RouterDataSource <Edge>(new Graph <Edge>(), tagsIndex);

            data.DropVertexIndex();
            var targetData = new GraphOsmStreamTarget(data, interpreter, tagsIndex);

            targetData.RegisterSource(reader);
            targetData.Pull();
            data.RebuildVertexIndex();

            RoutingTest.Test(data, vehicle, testCount);
        }
        public void PBFOsmStreamReaderReset()
        {
            // generate the source.
            var source = new PBFOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "OsmSharp.Test.data.api.osm.pbf"));

            // pull the data out.
            var target = new OsmStreamTargetEmpty();
            target.RegisterSource(source);
            target.Pull();

            // reset the source.
            if (source.CanReset)
            {
                source.Reset();

                // pull the data again.
                target.Pull();
            }
        }
        /// <summary>
        /// Loads a new instance.
        /// </summary>
        /// <param name="apiConfiguration"></param>
        /// <param name="instanceConfiguration"></param>
        /// <returns></returns>
        private static bool LoadInstance(ApiConfiguration apiConfiguration, InstanceConfiguration instanceConfiguration)
        {
            try
            {
                // get graph configuration.
                var graph = instanceConfiguration.Graph;
                var type = instanceConfiguration.Type;
                var format = instanceConfiguration.Format;
                var vehicleName = instanceConfiguration.Vehicle;

                try
                {
                    // create routing instance.
                    OsmSharp.Logging.Log.TraceEvent("Bootstrapper", OsmSharp.Logging.TraceEventType.Information,
                        string.Format("Creating {0} instance...", instanceConfiguration.Name));
                    Router router = null;
                    RouterDataSource<Edge> data = null;

                    var graphFile = new FileInfo(graph);
                    switch (type)
                    {
                        case "raw":
                            switch (format)
                            {
                                case "osm-xml":
                                    using(var graphStream = graphFile.OpenRead())
                                    {
                                        data = OsmSharp.Routing.Osm.Streams.GraphOsmStreamTarget.Preprocess(
                                            new XmlOsmStreamSource(graphStream), new OsmRoutingInterpreter());
                                        router = Router.CreateFrom(data, new OsmRoutingInterpreter());
                                    }
                                    break;
                                case "osm-pbf":
                                    using (var graphStream = graphFile.OpenRead())
                                    {
                                        data = OsmSharp.Routing.Osm.Streams.GraphOsmStreamTarget.Preprocess(
                                            new PBFOsmStreamSource(graphStream), new OsmRoutingInterpreter());
                                        router = Router.CreateFrom(data, new OsmRoutingInterpreter());
                                    }
                                    break;
                                default:
                                    throw new Exception(string.Format("Invalid format {0} for type {1}.",
                                        format, type));
                            }
                            break;
                        case "contracted":
                            // check for GTFS-feeds and give warning if they are there.
                            if(instanceConfiguration.Feeds != null && instanceConfiguration.Feeds.Count > 0)
                            { // ... oeps a feed and a contracted network are not supported for now.
                                OsmSharp.Logging.Log.TraceEvent("ApiBootstrapper", OsmSharp.Logging.TraceEventType.Warning,
                                    "NotSupported: GTFS and contracted graphs cannot (yet) be combined.");
                            }

                            switch(format)
                            {
                                case "osm-xml":
                                    if (string.IsNullOrWhiteSpace(vehicleName))
                                    { // invalid configuration.
                                        throw new Exception("Invalid configuration, a vehicle type is required when building contracted graphs on-the-fly.");
                                    }
                                    using (var graphStream = graphFile.OpenRead())
                                    {
                                        var graphSource = new XmlOsmStreamSource(graphStream);
                                        router = Router.CreateCHFrom(graphSource, new OsmRoutingInterpreter(), Vehicle.GetByUniqueName(vehicleName));
                                    }
                                    break;
                                case "osm-pbf":
                                    if (string.IsNullOrWhiteSpace(vehicleName))
                                    { // invalid configuration.
                                        throw new Exception("Invalid configuration, a vehicle type is required when building contracted graphs on-the-fly.");
                                    }
                                    using (var graphStream = graphFile.OpenRead())
                                    {
                                        var graphSource = new PBFOsmStreamSource(graphStream);
                                        router = Router.CreateCHFrom(graphSource, new OsmRoutingInterpreter(), Vehicle.GetByUniqueName(vehicleName));
                                    }
                                    break;
                                case "flat":
                                    var mobileRoutingSerializer = new CHEdgeSerializer();
                                    // keep this stream open, it is used while routing!
                                    var mobileGraphInstance = mobileRoutingSerializer.Deserialize(graphFile.OpenRead());
                                    router = Router.CreateCHFrom(mobileGraphInstance, new CHRouter(), new OsmRoutingInterpreter());
                                    break;
                                default:
                                    throw new Exception(string.Format("Invalid format {0} for type {1}.",
                                        format, type));
                            }
                            break;
                        case "simple":
                            switch (format)
                            {
                                case "flat":
                                    using (var graphStream = graphFile.OpenRead())
                                    {
                                        var routingSerializer = new RoutingDataSourceSerializer();
                                        data = routingSerializer.Deserialize(graphStream);
                                        router = Router.CreateFrom(data, new Dykstra(), new OsmRoutingInterpreter());
                                    }
                                    break;
                                default:
                                    throw new Exception(string.Format("Invalid format {0} for type {1}.",
                                        format, type));
                            }
                            break;
                    }

                    if(instanceConfiguration.Feeds != null && instanceConfiguration.Feeds.Count > 0)
                    { // transit-data use the transit-api.
                        if(instanceConfiguration.Feeds.Count > 1)
                        { // for now only one feed at a time is supported.
                            OsmSharp.Logging.Log.TraceEvent("ApiBootstrapper", OsmSharp.Logging.TraceEventType.Warning,
                                "NotSupported: Only one GTFS-feed at a time is supported.");
                        }

                        var feed = instanceConfiguration.Feeds[0];
                        var reader = new GTFSReader<GTFSFeed>(false);
                        var gtfsFeed = reader.Read<GTFSFeed>(new GTFSDirectorySource(feed.Path));
                        var connectionsDb = new GTFSConnectionsDb(gtfsFeed);
                        var multimodalConnectionsDb = new MultimodalConnectionsDb(data, connectionsDb,
                            new OsmRoutingInterpreter(), Vehicle.Pedestrian);

                        lock (_sync)
                        {
                            OsmSharp.Service.Routing.ApiBootstrapper.AddOrUpdate(instanceConfiguration.Name,
                                new MultimodalApi(multimodalConnectionsDb));
                        }
                    }
                    else
                    { // no transit-data just use the regular routing api implementation.
                        lock (_sync)
                        {
                            OsmSharp.Service.Routing.ApiBootstrapper.AddOrUpdate(instanceConfiguration.Name, router);
                        }
                    }

                    OsmSharp.Logging.Log.TraceEvent("Bootstrapper", OsmSharp.Logging.TraceEventType.Information,
                        string.Format("Instance {0} created successfully!", instanceConfiguration.Name));
                }
                catch(Exception ex)
                {
                    OsmSharp.Logging.Log.TraceEvent("Bootstrapper", OsmSharp.Logging.TraceEventType.Error,
                        string.Format("Exception occured while creating instance {0}:{1}", 
                        instanceConfiguration.Name, ex.ToInvariantString()));
                    throw;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Tests writing data and getting relations using it's members.
        /// </summary>
        protected void TestGetRelationsForMember()
        {
            this.NotifyEmptyExpected(); // empty test database.

            // create the target and pull the data from the test-file into the sqlite database.
            OsmStreamTarget target = this.CreateDataStreamTarget();
            target.Initialize();
            PBFOsmStreamSource source = new PBFOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Data.Test.Unittests.Data.Osm.test.osm.pbf"));
            IDataSourceReadOnly dataSource = this.CreateDataSource();
            source.Initialize();
            while (source.MoveNext())
            {
                switch (source.Current().Type)
                {
                    case OsmGeoType.Relation:
                        Relation relation = (source.Current() as Relation);
                        target.AddRelation(relation);
                        target.Flush();

                        if (relation.Members != null)
                        {
                            foreach (var member in relation.Members)
                            {
                                OsmGeoType type = OsmGeoType.Node;
                                switch (member.MemberType.Value)
                                {
                                    case OsmGeoType.Node:
                                        type = OsmGeoType.Node;
                                        break;
                                    case OsmGeoType.Way:
                                        type = OsmGeoType.Way;
                                        break;
                                    case OsmGeoType.Relation:
                                        type = OsmGeoType.Relation;
                                        break;
                                }
                                IList<Relation> relations = dataSource.GetRelationsFor(type, member.MemberId.Value);
                                Assert.IsNotNull(relations);
                                Assert.IsTrue(relations.Count > 0);
                                List<Relation> foundRelations = new List<Relation>(relations.Where<Relation>(x => x.Id == relation.Id));
                                Assert.AreEqual(1, foundRelations.Count);
                            }
                        }
                        break;
                }
            }

            MemoryDataSource memorySource = MemoryDataSource.CreateFrom(source);
            foreach (Relation relation in memorySource.GetRelations())
            {
                if (relation.Members != null)
                {
                    foreach (var member in relation.Members)
                    {
                        OsmGeoType type = OsmGeoType.Node;
                        switch (member.MemberType.Value)
                        {
                            case OsmGeoType.Node:
                                type = OsmGeoType.Node;
                                break;
                            case OsmGeoType.Way:
                                type = OsmGeoType.Way;
                                break;
                            case OsmGeoType.Relation:
                                type = OsmGeoType.Relation;
                                break;
                        }
                        IList<Relation> relations = dataSource.GetRelationsFor(type, member.MemberId.Value);
                        Assert.IsNotNull(relations);
                        Assert.IsTrue(relations.Count > 0);
                        List<Relation> foundRelations = new List<Relation>(relations.Where<Relation>(x => x.Id == relation.Id));
                        Assert.AreEqual(1, foundRelations.Count);
                    }
                }
                break;
            }
        }
Example #18
0
        /// <summary>
        /// Tests routing from a serialized routing file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="stream"></param>
        /// <param name="testCount"></param>
        public static void Test(string name, Stream stream, int testCount)
        {
            var vehicle = Vehicle.Pedestrian;

            var tagsIndex = new TagsIndex(); // creates a tagged index.

            // read from the OSM-stream.
            var reader = new OsmSharp.Osm.PBF.Streams.PBFOsmStreamSource(stream);
            var interpreter = new OsmRoutingInterpreter();
            var data = new RouterDataSource<Edge>(new Graph<Edge>(), tagsIndex);
            data.DropVertexIndex();
            var targetData = new GraphOsmStreamTarget(data, interpreter, tagsIndex);
            targetData.RegisterSource(reader);
            targetData.Pull();
            data.RebuildVertexIndex();

            RoutingTest.Test(data, vehicle, testCount);
        }
        /// <summary>
        /// Loads a new instance.
        /// </summary>
        /// <param name="apiConfiguration"></param>
        /// <param name="instanceConfiguration"></param>
        /// <returns></returns>
        private static bool LoadInstance(TileApiConfiguration apiConfiguration, InstanceConfiguration instanceConfiguration)
        {
            // get data file configuration.
            var data = instanceConfiguration.Data;

            // get mapcss configuration.
            var mapCSS = instanceConfiguration.MapCSS;
            if (string.IsNullOrWhiteSpace(mapCSS))
            {

            }

            // get the format.
            var format = instanceConfiguration.Format;

            // get the include file.
            try
            {
                // create routing instance.
                OsmSharp.Logging.Log.TraceEvent("Bootstrapper", OsmSharp.Logging.TraceEventType.Information,
                    string.Format("Creating {0} instance...", instanceConfiguration.Name));
                switch (format.ToLowerInvariant())
                {
                    case "osm-xml":
                        using (var stream = new FileInfo(data).OpenRead())
                        {
                            var streamSource = new XmlOsmStreamSource(stream);
                            using (var cssStream = new FileInfo(mapCSS).OpenRead())
                            {
                                ApiBootstrapper.BuildTileServer("tiles_" +
                                    instanceConfiguration.Name, streamSource, cssStream, apiConfiguration.Cache);
                            }
                        }
                        break;
                    case "osm-pbf":
                        using (var stream = new FileInfo(data).OpenRead())
                        {
                            var streamSource = new PBFOsmStreamSource(stream);
                            using (var cssStream = new FileInfo(mapCSS).OpenRead())
                            {
                                ApiBootstrapper.BuildTileServer("tiles_" +
                                    instanceConfiguration.Name, streamSource, cssStream, apiConfiguration.Cache);
                            }
                        }
                        break;
                    default:
                        throw new Exception(string.Format("Unrecognised raw osm format: {0}", format));
                }
                OsmSharp.Logging.Log.TraceEvent("Bootstrapper", OsmSharp.Logging.TraceEventType.Information,
                    string.Format("Instance {0} created successfully!", instanceConfiguration.Name));
            }
            catch (Exception ex)
            {
                OsmSharp.Logging.Log.TraceEvent("Bootstrapper", OsmSharp.Logging.TraceEventType.Error,
                    string.Format("Exception occured while creating instance {0}:{1}",
                    instanceConfiguration.Name, ex.ToInvariantString()));
                return false;
            }
            return true;
        }
        /// <summary>
        /// Tests interpreting all data from a given pbf source.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="scene"></param>
        /// <param name="interpreter"></param>
        /// <param name="pbfSource"></param>
        public static Stream TestInterpret(string name, MapCSSInterpreter interpreter, Scene2D scene, string pbfSource)
        {
            StyleOsmStreamSceneTarget target = new StyleOsmStreamSceneTarget(
                interpreter, scene, new WebMercator());
            FileInfo testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfSource));
            Stream stream = testFile.OpenRead();
            OsmStreamSource source = new PBFOsmStreamSource(stream);
            OsmStreamFilterProgress progress = new OsmStreamFilterProgress();
            progress.RegisterSource(source);
            target.RegisterSource(progress);

            PerformanceInfoConsumer performanceInfo = new PerformanceInfoConsumer(string.Format("{0}.Add", name));
            performanceInfo.Start();
            performanceInfo.Report("Interpreting style with objects from {0}...", pbfSource.ToString());

            target.Pull();

            performanceInfo.Stop();

            Console.Write("", scene.BackColor);
            stream.Dispose();

            return testFile.OpenRead();
        }
        /// <summary>
        /// Tests read/write and actual data file.
        /// </summary>
        protected void TestReadWriteData()
        {
            this.NotifyEmptyExpected(); // empty test database.

            // create the target and pull the data from the test-file into the sqlite database.
            OsmStreamTarget target = this.CreateDataStreamTarget();
            PBFOsmStreamSource source = new PBFOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Data.Test.Unittests.Data.Osm.test.osm.pbf"));
            target.RegisterSource(source);
            target.Pull();

            IDataSourceReadOnly dataSource = this.CreateDataSource();
            MemoryDataSource memorySource = MemoryDataSource.CreateFrom(source);
            foreach (Node node in memorySource.GetNodes())
            {
                Node dbNode = dataSource.GetNode(node.Id.Value);
                ComparisonHelpers.CompareSimple(node, dbNode);
            }
            foreach (Way way in memorySource.GetWays())
            {
                Way dbWay = dataSource.GetWay(way.Id.Value);
                ComparisonHelpers.CompareSimple(way, dbWay);
            }
            foreach (Relation relation in memorySource.GetRelations())
            {
                Relation dbRelation = dataSource.GetRelation(relation.Id.Value);
                ComparisonHelpers.CompareSimple(relation, dbRelation);
            }
        }
        /// <summary>
        /// Tests preprocessing data from a PBF file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pbfFile"></param>
        public static void TestSerialization(string name, string pbfFile)
        {
            var testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile));

            var performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var stream = testFile.OpenRead();
            var source = new PBFOsmStreamSource(stream);
            var progress = new OsmStreamFilterProgress();
            progress.RegisterSource(source);

            var testOutputFile = new FileInfo(@"test.routing");
            testOutputFile.Delete();
            Stream writeStream = testOutputFile.OpenWrite();

            var tagsIndex = new TagsTableCollectionIndex();
            var interpreter = new OsmRoutingInterpreter();
            var graph = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex);
            var routingSerializer = new LiveEdgeFlatfileSerializer();

            // read from the OSM-stream.
            using (var fileFactory = new MemoryMappedFileFactory(@"d:\temp\"))
            {
                using (var memoryMappedGraph = new MemoryMappedGraph<LiveEdge>(10000, fileFactory))
                {
                    using (var coordinates = new HugeCoordinateIndex(fileFactory, 10000))
                    {
                        var memoryData = new DynamicGraphRouterDataSource<LiveEdge>(memoryMappedGraph, tagsIndex);
                        var targetData = new LiveGraphOsmStreamTarget(memoryData, new OsmRoutingInterpreter(), tagsIndex, coordinates);
                        targetData.RegisterSource(progress);
                        targetData.Pull();

                        performanceInfo.Stop();

                        performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
                        performanceInfo.Start();
                        performanceInfo.Report("Writing file for {0}...", testFile.Name);

                        var metaData = new TagsCollection();
                        metaData.Add("some_key", "some_value");
                        routingSerializer.Serialize(writeStream, memoryData, metaData);
                    }
                }
            }
            stream.Dispose();
            writeStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                string.Format("Serialized file: {0}KB", testOutputFile.Length / 1024));
            performanceInfo.Stop();

            performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
            performanceInfo.Start();
            performanceInfo.Report("Reading file for {0}...", testFile.Name);

            var testInputFile = new FileInfo(@"europe-latest.osm.pbf.routing");
            Stream readStream = testInputFile.OpenRead();

            var deserializedGraph = routingSerializer.Deserialize(readStream, false);

            readStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                string.Format("Read: {0}KB", testInputFile.Length / 1024));

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", Logging.TraceEventType.Information, deserializedGraph.ToInvariantString());

            performanceInfo.Stop();
        }
        /// <summary>
        /// Tests writing data and getting ways using it's nodes.
        /// </summary>
        protected void TestGetWaysForNode()
        {
            this.NotifyEmptyExpected(); // empty test database.

            // create the target and pull the data from the test-file into the sqlite database.
            OsmStreamTarget target = this.CreateDataStreamTarget();
            target.Initialize();
            PBFOsmStreamSource source = new PBFOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Data.Test.Unittests.Data.Osm.test.osm.pbf"));
            IDataSourceReadOnly dataSource = this.CreateDataSource();
            source.Initialize();
            while (source.MoveNext())
            {
                switch (source.Current().Type)
                {
                    case OsmGeoType.Way:
                        Way way = (source.Current() as Way);
                        target.AddWay(way);
                        target.Flush();

                        if (way.Nodes != null)
                        {
                            foreach (long nodeId in way.Nodes)
                            {
                                IList<Way> ways = dataSource.GetWaysFor(nodeId);
                                Assert.IsNotNull(ways);
                                Assert.IsTrue(ways.Count > 0);
                                List<Way> foundWays = new List<Way>(ways.Where<Way>(x => x.Id == way.Id));
                                Assert.AreEqual(1, foundWays.Count);
                            }
                        }
                        break;
                }
            }

            MemoryDataSource memorySource = MemoryDataSource.CreateFrom(source);
            foreach (Way way in memorySource.GetWays())
            {
                if (way.Nodes != null)
                {
                    foreach (long nodeId in way.Nodes)
                    {
                        IList<Way> ways = dataSource.GetWaysFor(nodeId);
                        Assert.IsNotNull(ways);
                        Assert.IsTrue(ways.Count > 0);
                        List<Way> foundWays = new List<Way>(ways.Where<Way>(x => x.Id == way.Id));
                        Assert.AreEqual(1, foundWays.Count);
                    }
                }
            }
        }
        /// <summary>
        /// Tests preprocessing data from a PBF file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pbfFile"></param>
        public static void TestSerialization(string name, string pbfFile)
        {
            var testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile));

            var performanceInfo = new PerformanceInfoConsumer("SerializerFlatFile.Serialize", 2000);
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var stream = testFile.OpenRead();
            var source = new PBFOsmStreamSource(stream);
            var progress = new OsmStreamFilterProgress();
            progress.RegisterSource(source);

            var testOutputFile = new FileInfo(@"test.routing");
            testOutputFile.Delete();
            var writeStream = testOutputFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);

            var tagsIndex = new TagsIndex();
            var interpreter = new OsmRoutingInterpreter();
            var graph = new RouterDataSource<Edge>(new Graph<Edge>(), tagsIndex);
            var routingSerializer = new RoutingDataSourceSerializer();

            var memoryMappedGraph = new Graph<Edge>(1024);
            var coordinates = new HugeCoordinateIndex(1024);
            var memoryData = new RouterDataSource<Edge>(memoryMappedGraph, tagsIndex);
            var targetData = new GraphOsmStreamTarget(memoryData, new OsmRoutingInterpreter(), tagsIndex, coordinates);
            targetData.RegisterSource(progress);
            targetData.Pull();

            performanceInfo.Stop();

            memoryData.Compress();

            performanceInfo = new PerformanceInfoConsumer("SerializerFlatFile.Serialize", 100000);
            performanceInfo.Start();
            performanceInfo.Report("Writing file for {0}...", testFile.Name);

            var metaData = new TagsCollection();
            metaData.Add("some_key", "some_value");
            routingSerializer.Serialize(writeStream, memoryData, metaData);
            stream.Dispose();
            writeStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("SerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                string.Format("Serialized file: {0}KB", testOutputFile.Length / 1024));
            performanceInfo.Stop();

            ////performanceInfo = new PerformanceInfoConsumer("SerializerFlatFile.Serialize", 100000);
            ////performanceInfo.Start();
            ////performanceInfo.Report("Reading file for {0}...", testFile.Name);

            //var testInputFile = new FileInfo(@"test.routing");
            //var readStream = testInputFile.OpenRead();

            //RoutingTest.TestSerialized(readStream);

            ////var deserializedGraph = routingSerializer.Deserialize(readStream, false);

            ////readStream.Dispose();

            ////OsmSharp.Logging.Log.TraceEvent("SerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
            ////    string.Format("Read: {0}KB", testInputFile.Length / 1024));

            ////OsmSharp.Logging.Log.TraceEvent("SerializerFlatFile", Logging.TraceEventType.Information, deserializedGraph.ToInvariantString());

            ////performanceInfo.Stop();
        }