/// <summary> /// Registers the source. /// </summary> public virtual void RegisterSource(OsmStreamSource source, bool filterNonRoutingTags) { if (filterNonRoutingTags) { // add filtering. var eventsFilter = new OsmSharp.Streams.Filters.OsmStreamFilterDelegate(); eventsFilter.MoveToNextEvent += (osmGeo, param) => { if (osmGeo.Type == OsmGeoType.Way) { // normalize tags, reduce the combination of tags meaning the same thing. var tags = osmGeo.Tags.ToAttributes(); var normalizedTags = new AttributeCollection(); if (!DefaultTagNormalizer.Normalize(tags, normalizedTags, _vehicleCache)) { // invalid data, no access, or tags make no sense at all. return(osmGeo); } // rewrite tags and keep whitelisted meta-tags. osmGeo.Tags.Clear(); foreach (var tag in normalizedTags) { osmGeo.Tags.Add(tag.Key, tag.Value); } foreach (var tag in tags) { if (_vehicleCache.Vehicles.IsOnMetaWhiteList(tag.Key)) { osmGeo.Tags.Add(tag.Key, tag.Value); } } } return(osmGeo); }; eventsFilter.RegisterSource(source); base.RegisterSource(eventsFilter); } else { // no filtering. base.RegisterSource(source); } }
/// <summary> /// Registers the source. /// </summary> public virtual void RegisterSource(OsmStreamSource source, bool filterNonRoutingTags) { if (filterNonRoutingTags) { // add filtering. var eventsFilter = new OsmSharp.Streams.Filters.OsmStreamFilterDelegate(); eventsFilter.MoveToNextEvent += (osmGeo, param) => { if (osmGeo.Type == OsmGeoType.Way) { var tags = new TagsCollection(osmGeo.Tags); foreach (var tag in tags) { var relevant = false; for (var i = 0; i < _vehicles.Length; i++) { if (_vehicles[i].IsRelevant(tag.Key, tag.Value)) { relevant = true; break; } } if (!relevant) { osmGeo.Tags.RemoveKeyValue(tag); } } } return(osmGeo); }; eventsFilter.RegisterSource(source); base.RegisterSource(eventsFilter); } else { // no filtering. base.RegisterSource(source); } }
/// <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); }