コード例 #1
0
        /// <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(), false);
                            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);
            }
        }
コード例 #2
0
        /// <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;
            }
        }