Exemplo n.º 1
0
        /// <summary>
        /// Parses this command into a processor given the arguments for this switch. Consumes the previous processors and returns how many it consumes.
        /// </summary>
        public override int Parse(List <Processor> previous, out Processor processor)
        {
            if (previous.Count < 1)
            {
                throw new ArgumentException("Expected at least one processors before this one.");
            }

            if (!(previous[previous.Count - 1] is Processors.RouterDb.IProcessorRouterDbSource))
            {
                throw new Exception("Expected a router db source.");
            }

            Profile profile   = null;
            var     augmented = false;

            if (this.Arguments.Length == 1 &&
                !this.Arguments[0].Contains("="))
            {
                var profileName = this.Arguments[0];
                profile = Itinero.Profiles.Profile.GetRegistered(profileName);
                if (profile == null)
                {
                    throw new Exception(string.Format("Cannot find profile {0}.", this.Arguments[0]));
                }
            }
            else
            {
                for (var i = 0; i < this.Arguments.Length; i++)
                {
                    string key, value;
                    if (SwitchParsers.SplitKeyValue(this.Arguments[i], out key, out value))
                    {
                        switch (key.ToLower())
                        {
                        case "profile":
                            var profileName = value;
                            profile = Itinero.Profiles.Profile.GetRegistered(profileName);
                            if (profile == null)
                            {
                                throw new Exception(string.Format("Cannot find profile {0}.", this.Arguments[0]));
                            }
                            break;

                        case "augmented":
                            if (SwitchParsers.IsTrue(value))
                            {
                                augmented = true;;
                            }
                            break;

                        default:
                            throw new SwitchParserException("--contract",
                                                            string.Format("Invalid parameter for command --contract: {0} not recognized.", key));
                        }
                    }
                }
            }

            var source = (previous[previous.Count - 1] as Processors.RouterDb.IProcessorRouterDbSource);
            Func <Itinero.RouterDb> getRouterDb = () =>
            {
                var routerDb = source.GetRouterDb();

                if (!augmented)
                {
                    routerDb.AddContracted(profile);
                }
                else
                {
                    routerDb.AddContracted(profile, profile.AugmentedWeightHandlerCached(routerDb));
                }

                return(routerDb);
            };

            processor = new Processors.RouterDb.ProcessorRouterDbSource(getRouterDb);

            return(1);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses this command into a processor given the arguments for this switch. Consumes the previous processors and returns how many it consumes.
        /// </summary>
        public override int Parse(List <Processor> previous, out Processor processor)
        {
            var vehicles = new List <Vehicle>(new Vehicle[]
            {
                Itinero.Osm.Vehicles.Vehicle.Car
            });
            var allCore    = false;
            var keepWayIds = false;

            for (var i = 0; i < this.Arguments.Length; i++)
            {
                string key, value;
                if (SwitchParsers.SplitKeyValue(this.Arguments[i], out key, out value))
                {
                    switch (key.ToLower())
                    {
                    case "vehicles":
                    case "vehicle":
                        string[] vehicleValues;
                        if (SwitchParsers.SplitValuesArray(value.ToLower(), out vehicleValues))
                        {     // split the values array.
                            vehicles = new List <Vehicle>(vehicleValues.Length);
                            for (int v = 0; v < vehicleValues.Length; v++)
                            {
                                Vehicle vehicle;
                                if (!Vehicle.TryGet(vehicleValues[v], out vehicle))
                                {
                                    if (vehicleValues[v] == "all")
                                    {     // all vehicles.
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Bicycle);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.BigTruck);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Bus);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Car);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Moped);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.MotorCycle);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Pedestrian);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.SmallTruck);
                                    }
                                    else if (vehicleValues[v] == "motorvehicle" ||
                                             vehicleValues[v] == "motorvehicles")
                                    {     // all motor vehicles.
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.BigTruck);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Bus);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Car);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.MotorCycle);
                                        vehicles.Add(Itinero.Osm.Vehicles.Vehicle.SmallTruck);
                                    }
                                    else
                                    {     // assume a filename.
                                        var vehicleFile = new FileInfo(vehicleValues[v]);
                                        if (!vehicleFile.Exists)
                                        {
                                            throw new SwitchParserException("--create-routerdb",
                                                                            string.Format("Invalid parameter value for command --create-routerdb: Vehicle profile '{0}' not found.",
                                                                                          vehicleValues[v]));
                                        }
                                        using (var stream = vehicleFile.OpenRead())
                                        {
                                            vehicle = DynamicVehicle.LoadFromStream(stream);
                                            vehicle.Register();
                                            vehicles.Add(vehicle);
                                        }
                                    }
                                }
                                else
                                {
                                    vehicles.Add(vehicle);
                                }
                            }
                        }
                        break;

                    case "allcore":
                        if (SwitchParsers.IsTrue(value))
                        {
                            allCore = true;;
                        }
                        break;

                    case "keepwayids":
                        if (SwitchParsers.IsTrue(value))
                        {
                            keepWayIds = true;;
                        }
                        break;

                    default:
                        throw new SwitchParserException("--create-routerdb",
                                                        string.Format("Invalid parameter for command --create-routerdb: {0} not recognized.", key));
                    }
                }
            }

            if (!(previous[previous.Count - 1] is Processors.Osm.IProcessorOsmStreamSource))
            {
                throw new Exception("Expected an OSM stream source.");
            }

            var source = (previous[previous.Count - 1] as Processors.Osm.IProcessorOsmStreamSource).Source;
            Func <Itinero.RouterDb> getRouterDb = () =>
            {
                var routerDb = new Itinero.RouterDb();

                // load the data.
                var target = new Itinero.IO.Osm.Streams.RouterDbStreamTarget(routerDb,
                                                                             vehicles.ToArray(), allCore, processRestrictions: true);
                if (keepWayIds)
                { // add way id's.
                    var eventsFilter = new OsmSharp.Streams.Filters.OsmStreamFilterDelegate();
                    eventsFilter.MoveToNextEvent += EventsFilter_AddWayId;
                    eventsFilter.RegisterSource(source);
                    target.RegisterSource(eventsFilter, false);
                }
                else
                { // use the source as-is.
                    target.RegisterSource(source);
                }
                target.Pull();

                // sort the network.
                routerDb.Sort();

                return(routerDb);
            };

            processor = new Processors.RouterDb.ProcessorRouterDbSource(getRouterDb);

            return(1);
        }