public override FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes) { var whitelist = new List <string>(); return(PedestrianProfile.FactorAndSpeed(attributes, new Whitelist())); }
/// <summary> /// Sets a single value. /// </summary> public static void SetSingle(this IAttributeCollection attributes, string key, float value) { attributes.AddOrReplace(key, value.ToInvariantString()); }
/// <summary> /// Returns true if the two edges with the given attributes are identical as far as this profile is concerned. /// </summary> /// <remarks> /// Default implementation compares attributes one-by-one. /// </remarks> public override sealed bool Equals(IAttributeCollection attributes1, IAttributeCollection attributes2) { return(attributes1.ContainsSame(attributes2)); }
/// <summary> /// Adds a tag as an attribute. /// </summary> public static void Add(this IAttributeCollection attributes, Tag tag) { attributes.AddOrReplace(tag.Key, tag.Value); }
/// <summary> /// Get a function to calculate properties for a set given edge attributes. /// </summary> /// <returns></returns> public sealed override EdgeFactor Factor(IAttributeCollection attributes) { lock (_script) { // build lua table. _attributesTable.Clear(); if (attributes == null || attributes.Count == 0) { return(EdgeFactor.NoFactor); } foreach (var attribute in attributes) { _attributesTable.Set(attribute.Key, DynValue.NewString(attribute.Value)); } // call factor_and_speed function. _resultsTable.Clear(); _script.Call(_script.Globals["factor"], _attributesTable, _resultsTable); // get the results. if (!_resultsTable.TryGetDouble("forward", out var forwardFactor)) { forwardFactor = 0; } if (!_resultsTable.TryGetDouble("backward", out var backwardFactor)) { backwardFactor = 0; } if (!_resultsTable.TryGetBool("canstop", out var canstop)) { canstop = backwardFactor != 0 || forwardFactor != 0; } // the speeds are supposed to be in m/s. if (!_resultsTable.TryGetDouble("forward_speed", out var speedForward)) { // when forward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s) speedForward = 0; if (forwardFactor > 0) { // convert to m/s. speedForward = 1.0 / forwardFactor; } } else { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s. speedForward /= 3.6; } if (!_resultsTable.TryGetDouble("backward_speed", out var speedBackward)) { // when backward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s) speedBackward = 0; if (backwardFactor > 0) { // convert to m/s. speedBackward = 1.0 / backwardFactor; } } else { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s. speedBackward /= 3.6; } return(new EdgeFactor((uint)(forwardFactor * 100), (uint)(backwardFactor * 100), (ushort)(speedForward * 100), (ushort)(speedBackward * 100), canstop)); } }
/// <summary> /// Tries to match the given attributes to the given fow/frc and returns a score of the match. /// </summary> public override float Match(IAttributeCollection attributes, FormOfWay fow, FunctionalRoadClass frc) { string highway; if (!attributes.TryGetValue("highway", out highway)) { // not even a highway tag! return(0); } // TODO: take into account form of way? Maybe not for OSM-data? switch (frc) { // check there reference values against OSM: http://wiki.openstreetmap.org/wiki/Highway case FunctionalRoadClass.Frc0: // main road. if (highway == "motorway" || highway == "motorway_link" || highway == "trunk" || highway == "trunk_link") { return(1); } break; case FunctionalRoadClass.Frc1: // first class road. if (highway == "primary" || highway == "primary_link") { return(1); } break; case FunctionalRoadClass.Frc2: // second class road. if (highway == "secondary" || highway == "secondary_link") { return(1); } break; case FunctionalRoadClass.Frc3: // third class road. if (highway == "tertiary" || highway == "tertiary_link") { return(1); } break; case FunctionalRoadClass.Frc4: if (highway == "road" || highway == "road_link" || highway == "unclassified" || highway == "residential") { return(1); } break; case FunctionalRoadClass.Frc5: if (highway == "road" || highway == "road_link" || highway == "unclassified" || highway == "residential" || highway == "living_street") { return(1); } break; case FunctionalRoadClass.Frc6: if (highway == "road" || highway == "track" || highway == "unclassified" || highway == "residential" || highway == "living_street") { return(1); } break; case FunctionalRoadClass.Frc7: // other class road. if (highway == "footway" || highway == "bridleway" || highway == "steps" || highway == "path" || highway == "living_street") { return(1); } break; } if (highway != null && highway.Length > 0) { // for any other highway return a low match. return(0.2f); } return(0); }
public static bool Contains(this IAttributeCollection self, AttributeKind kind) { return(self.Any(a => a.Kind == kind)); }
/// <summary> /// Returns true if the edge is one way forward, false if backward, null if bidirectional. /// </summary> public override bool?IsOneWay(IAttributeCollection tags) { return(null); }
/// <summary> /// Writes an attributes collection to the given stream and prefixed with it's size. /// </summary> public static long WriteWithSize(this IAttributeCollection attributes, System.IO.Stream stream) { return(attributes.SerializeWithSize(stream)); }
/// <summary> /// Gets a the factor for an edge with the given attributes. /// </summary> public static Factor Factor(this IProfileInstance profileInstance, IAttributeCollection attributes) { return(profileInstance.FactorAndSpeed(attributes).ToFactor()); }
/// <summary> /// Gets a the factor and speed for an edge with the given attributes. /// </summary> public static FactorAndSpeed FactorAndSpeed(this IProfileInstance profileInstance, IAttributeCollection attributes) { var factorAndSpeed = profileInstance.Profile.FactorAndSpeed(attributes); if (profileInstance.IsConstrained(factorAndSpeed.Constraints)) { return(Profiles.FactorAndSpeed.NoFactor); } return(factorAndSpeed); }
/// <summary> /// Tries to get cached whitelist. /// </summary> public bool TryGetCached(IAttributeCollection attributes, out Whitelist whitelist, bool filter = true) { bool[] canTraverse; return(this.TryGetCached(attributes, out whitelist, out canTraverse, filter)); }
public void AddComponent <T>(Action <T, XmlLayoutElement> componentConfig, IAttributeCollection <T> attrs = null) where T : Component { objectBuildActions.Add((prefabObject, binder) => { if (!prefabObject.gameObject.TryGetComponent <T>(out var component)) { component = prefabObject.gameObject.AddComponent <T>(); } componentConfig?.Invoke(component, prefabObject); if (attrs.IsEmpty()) { return; } foreach (var c in attrs.SerializableConstants) { c.Setter(prefabObject, component); } binder.AddAttributes(prefabObject, component, attrs.Variables, attrs.NonSerializableConstants); }); }
internal static FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes, Whitelist whitelist) { if (attributes == null || attributes.Count == 0) { return(Itinero.Profiles.FactorAndSpeed.NoFactor); } string highway = null; if (attributes.TryGetValue("highway", out highway)) { whitelist.Add("highway"); } string foot = null; if (attributes.TryGetValue("foot", out foot)) { if (foot == "no" || foot == "0") { return(Itinero.Profiles.FactorAndSpeed.NoFactor); } whitelist.Add("foot"); } string footway; if (attributes.TryGetValue("footway", out footway)) { whitelist.Add("footway"); } var result = new FactorAndSpeed(); var speed = 0.0f; short direction = 0; var canstop = true; if (String.IsNullOrEmpty(highway)) { if (!String.IsNullOrEmpty(foot)) { highway = "footway"; } else { return(Itinero.Profiles.FactorAndSpeed.NoFactor); } } //get default speed profiles var highway_speed = SpeedProfiles.ContainsKey(highway) ? (int?)SpeedProfiles[highway] : null; if (highway_speed != null) { speed = highway_speed.Value; direction = 0; canstop = true; } else { return(Itinero.Profiles.FactorAndSpeed.NoFactor); } if (CanAccess(attributes) == false || speed == 0) { return(Itinero.Profiles.FactorAndSpeed.NoFactor); } result.SpeedFactor = 1.0f / (speed / 3.6f); // 1/m/s result.Value = result.SpeedFactor; result.Direction = direction; if (!canstop) { result.Direction += 3; } return(result); }
/// <summary> /// Returns true if the two given edges are equals as far as this vehicle is concerned. /// </summary> public virtual bool Equals(IAttributeCollection attributes1, IAttributeCollection attributes2) { return(attributes1.ContainsSame(attributes2)); }
/// <summary> /// Adds new attributes. /// </summary> public uint Add(IAttributeCollection tags) { if (tags == null) { return(NULL_ATTRIBUTES); } else if (tags.Count == 0) { return(EMPTY_ATTRIBUTES); } if (_isReadonly) { // this index is readonly. // TODO: make writeable. // - set nextId. // - create reverse indexes if needed. if (_index != null) { // this should be an increase-one index. if ((_mode & AttributesIndexMode.IncreaseOne) != AttributesIndexMode.IncreaseOne) { throw new Exception("Invalid combination of data: There is an index but mode isn't increase one."); } _nextId = (uint)_index.Length; } // build reverse indexes if needed. if ((_mode & AttributesIndexMode.ReverseStringIndex) == AttributesIndexMode.ReverseStringIndex || (_mode & AttributesIndexMode.ReverseStringIndexKeysOnly) == AttributesIndexMode.ReverseStringIndexKeysOnly) { _stringReverseIndex = new Reminiscence.Collections.Dictionary <string, int>( new MemoryMapStream(), 1024 * 16); // add existing data. if ((_mode & AttributesIndexMode.ReverseStringIndex) == AttributesIndexMode.ReverseStringIndex) { // build reverse index for all data. foreach (var pair in _stringIndex) { _stringReverseIndex[pair.Value] = (int)pair.Key; } } else { // build reverse index for keys only. foreach (var collectionPair in _collectionIndex) { foreach (var stringId in collectionPair.Value) { _stringReverseIndex[_stringIndex.Get(stringId)] = stringId; } } } } if ((_mode & AttributesIndexMode.ReverseCollectionIndex) == AttributesIndexMode.ReverseCollectionIndex) { _collectionReverseIndex = new Reminiscence.Collections.Dictionary <int[], uint>(new MemoryMapStream(), 1024 * 16, new EqualityComparer()); if (_index != null) { for (uint col = 0; col < _nextId; col++) { var pointer = _index[col]; _collectionReverseIndex[_collectionIndex.Get(pointer)] = col; } } else { foreach (var pair in _collectionIndex) { _collectionReverseIndex[pair.Value] = (uint)pair.Key; } } } _isReadonly = false; } // add new collection. var sortedSet = new SortedSet <long>(); foreach (var tag in tags) { sortedSet.Add((long)this.AddString(tag.Key, true) + (long)int.MaxValue * (long)this.AddString(tag.Value, false)); } // sort keys. var sorted = new int[sortedSet.Count * 2]; var i = 0; foreach (var pair in sortedSet) { sorted[i] = (int)(pair % int.MaxValue); i++; sorted[i] = (int)(pair / int.MaxValue); i++; } // add sorted collection. return(this.AddCollection(sorted)); }
/// <summary> /// Tries to extract fow/frc from the given attributes. /// </summary> public override bool Extract(IAttributeCollection tags, out FunctionalRoadClass frc, out FormOfWay fow) { frc = FunctionalRoadClass.Frc7; fow = FormOfWay.Undefined; string highway; if (tags.TryGetValue("highway", out highway)) { switch (highway) { // check there reference values against OSM: http://wiki.openstreetmap.org/wiki/Highway case "motorway": case "trunk": frc = FunctionalRoadClass.Frc0; break; case "primary": case "primary_link": frc = FunctionalRoadClass.Frc1; break; case "secondary": case "secondary_link": frc = FunctionalRoadClass.Frc2; break; case "tertiary": case "tertiary_link": frc = FunctionalRoadClass.Frc3; break; case "road": case "road_link": case "unclassified": case "residential": frc = FunctionalRoadClass.Frc4; break; case "living_street": frc = FunctionalRoadClass.Frc5; break; default: frc = FunctionalRoadClass.Frc7; break; } switch (highway) { // check there reference values against OSM: http://wiki.openstreetmap.org/wiki/Highway case "motorway": case "trunk": fow = FormOfWay.Motorway; break; case "primary": case "primary_link": fow = FormOfWay.MultipleCarriageWay; break; case "secondary": case "secondary_link": case "tertiary": case "tertiary_link": fow = FormOfWay.SingleCarriageWay; break; default: fow = FormOfWay.SingleCarriageWay; break; } return(true); // should never fail on a highway tag. } return(false); }
/// <summary> /// Returns true if the given attribute collection represents a shortcut and returns the name. /// </summary> public static bool IsShortcut(this IAttributeCollection attributes, out string name) { return(attributes.TryGetValue(ShortcutExtensions.SHORTCUT_KEY, out name)); }
public static bool IsEmpty <T>(this IAttributeCollection <T> c) => c == null || c.SerializableConstants.Length == 0 && c.NonSerializableConstants.Length == 0 && c.Variables.Length == 0;
/// <summary> /// Calculates a factor and speed. /// </summary> public override FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes, Whitelist whitelist) { return(_getFactorAndSpeed(attributes)); }
/// <summary> /// Creates a new router database. /// </summary> private RouterDb(Guid guid, RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, IAttributeCollection dbMeta, Profiles.Vehicle[] supportedVehicles) { _guid = guid; _network = network; _edgeProfiles = profiles; _meta = meta; _dbMeta = dbMeta; _supportedVehicles = new Dictionary <string, Vehicle>(); _supportedProfiles = new Dictionary <string, Profile>(); foreach (var vehicle in supportedVehicles) { _supportedVehicles[vehicle.Name.ToLowerInvariant()] = vehicle; foreach (var profile in vehicle.GetProfiles()) { _supportedProfiles[profile.FullName.ToLowerInvariant()] = profile; } } _contracted = new Dictionary <string, ContractedDb>(); _restrictionDbs = new Dictionary <string, RestrictionsDb>(); _shortcutsDbs = new Dictionary <string, ShortcutsDb>(); }
public override bool AddToWhiteList(IAttributeCollection attributes, Whitelist whitelist) { return(PedestrianProfile.FactorAndSpeed(attributes, whitelist).SpeedFactor > 0); }
/// <summary> /// Pushes the attributes through this profiles and adds used keys in the given whitelist. /// </summary> public override FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes, Whitelist whiteList) { throw new NotImplementedException("Not used and unavailable with dynamic vehicles."); }
public override FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes, Whitelist whitelist) { throw new NotImplementedException(); }
/// <summary> /// Adds a new attribute. /// </summary> public static void AddOrReplace(this IAttributeCollection attributes, Attribute attribute) { attributes.AddOrReplace(attribute.Key, attribute.Value); }
/// <summary> /// Adds a number of keys to the given whitelist when they are relevant for this vehicle. /// </summary> /// <returns>True if the edge with the given attributes is usefull for this vehicle.</returns> public virtual bool AddToWhiteList(IAttributeCollection attributes, Whitelist whitelist) { return(this.FactorAndSpeed(attributes, whitelist).Value > 0); }
/// <summary> /// Extracts nwb/fow. /// </summary> public override bool Extract(IAttributeCollection attributes, out FunctionalRoadClass frc, out FormOfWay fow) { fow = FormOfWay.Undefined; frc = FunctionalRoadClass.Frc7; string baansubsrt = string.Empty, wegbeerder = string.Empty, wegnummer = string.Empty, rijrichting = string.Empty, dvkletter_ = string.Empty; if (!attributes.TryGetValue(BAANSUBSRT, out baansubsrt) & !attributes.TryGetValue(WEGBEHSRT, out wegbeerder) & !attributes.TryGetValue(WEGNUMMER, out wegnummer) & !attributes.TryGetValue(HECTOLTTR, out dvkletter_) & !attributes.TryGetValue(RIJRICHTNG, out rijrichting)) { // not even a BAANSUBSRT tag! // defaults: FRC5, OTHER. fow = FormOfWay.Other; frc = FunctionalRoadClass.Frc5; return(true); } // make sure everything is lowercase. char?dvkletter = null; // assume dkv letter is the suffix used for exits etc. see: http://www.wegenwiki.nl/Hectometerpaal#Suffix if (!string.IsNullOrWhiteSpace(wegbeerder)) { wegbeerder = wegbeerder.ToLowerInvariant(); } if (!string.IsNullOrWhiteSpace(baansubsrt)) { baansubsrt = baansubsrt.ToLowerInvariant(); } if (!string.IsNullOrWhiteSpace(wegnummer)) { wegnummer = wegnummer.ToLowerInvariant(); if (!string.IsNullOrEmpty(dvkletter_)) { dvkletter = dvkletter_[0]; } } if (!string.IsNullOrWhiteSpace(rijrichting)) { rijrichting = rijrichting.ToLowerInvariant(); } fow = FormOfWay.Other; frc = FunctionalRoadClass.Frc5; if (wegbeerder == "r") { if (baansubsrt == "hr") { fow = FormOfWay.Motorway; frc = FunctionalRoadClass.Frc0; } else if (baansubsrt == "nrb" || baansubsrt == "mrb") { fow = FormOfWay.Roundabout; frc = FunctionalRoadClass.Frc0; } else if (baansubsrt == "pst") { if (dvkletter.HasValue) { fow = FormOfWay.SlipRoad; if (dvkletter == 'a' || dvkletter == 'b' || dvkletter == 'c' || dvkletter == 'd') { // r pst (a|b|c|d) frc = FunctionalRoadClass.Frc3; } else { // r pst !(a|b|c|d) frc = FunctionalRoadClass.Frc0; } } else if (!string.IsNullOrWhiteSpace(rijrichting)) { // r pst !(a|b|c|d) fow = FormOfWay.SlipRoad; frc = FunctionalRoadClass.Frc0; } } else if (baansubsrt == "opr" || baansubsrt == "afr") { frc = FunctionalRoadClass.Frc3; fow = FormOfWay.SlipRoad; } else if (baansubsrt.StartsWith("vb")) { if (!string.IsNullOrWhiteSpace(rijrichting)) { fow = FormOfWay.SlipRoad; frc = FunctionalRoadClass.Frc0; } } } else if (wegbeerder == "p") { if (baansubsrt == "hr") { frc = FunctionalRoadClass.Frc3; fow = FormOfWay.MultipleCarriageWay; if (string.IsNullOrWhiteSpace(rijrichting)) { frc = FunctionalRoadClass.Frc2; fow = FormOfWay.SingleCarriageWay; } } else if (baansubsrt == "nrb" || baansubsrt == "mrb") { frc = FunctionalRoadClass.Frc3; fow = FormOfWay.Roundabout; } else if (baansubsrt == "opr" || baansubsrt == "afr") { frc = FunctionalRoadClass.Frc3; fow = FormOfWay.SlipRoad; } else if (baansubsrt == "pst" || baansubsrt.StartsWith("vb")) { frc = FunctionalRoadClass.Frc3; fow = FormOfWay.SlipRoad; } } return(true); }
/// <summary> /// Calculates a factor and speed and adds a keys to the given whitelist that are relevant. /// </summary> /// <returns>True if the edge with the given attributes is usefull for this vehicle.</returns> public abstract FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes, Whitelist whitelist);
/// <summary> /// Get a function to calculate properties for a set given edge attributes. /// </summary> /// <returns></returns> public sealed override FactorAndSpeed FactorAndSpeed(IAttributeCollection attributes) { lock (_script) { // build lua table. _attributesTable.Clear(); if (attributes == null || attributes.Count == 0) { return(Profiles.FactorAndSpeed.NoFactor); } foreach (var attribute in attributes) { _attributesTable.Set(attribute.Key, DynValue.NewString(attribute.Value)); } // call factor_and_speed function. _resultsTable.Clear(); _script.Call(_function, _attributesTable, _resultsTable); // get the results. var result = new FactorAndSpeed(); float val; if (!_resultsTable.TryGetFloat("speed", out val)) { val = 0; } if (val == 0) { return(Profiles.FactorAndSpeed.NoFactor); } result.SpeedFactor = 1.0f / (val / 3.6f); // 1/m/s if (_metric == ProfileMetric.TimeInSeconds) { // use 1/speed as factor. result.Value = result.SpeedFactor; } else if (_metric == ProfileMetric.DistanceInMeters) { // use 1 as factor. result.Value = 1; } else { // use a custom factor. if (!_resultsTable.TryGetFloat("factor", out val)) { val = 0; } result.Value = val; } if (!_resultsTable.TryGetFloat("direction", out val)) { val = 0; } result.Direction = (short)val; bool boolVal; if (!_resultsTable.TryGetBool("canstop", out boolVal)) { // default stopping everywhere. boolVal = true; } if (!boolVal) { result.Direction += 3; } return(result); } }
/// <summary> /// Splits the given tags into a normalized version, profile tags, and the rest in metatags. /// </summary> public static bool Normalize(IAttributeCollection tags, IAttributeCollection profileTags, VehicleCache vehicleCache) { if (tags == null || profileTags == null || vehicleCache == null) { return(false); } var normalizedTags = new HashSet <string>(new string[] { "highway", "maxspeed", "oneway", "oneway:bicycle", "cycleway", "junction", "access" }); foreach (var vehicle in vehicleCache.Vehicles) { foreach (var vehicleType in vehicle.VehicleTypes) { normalizedTags.Add(vehicleType); } } string highway; if (!tags.TryGetValue("highway", out highway)) { // there is no highway tag, don't continue the search. return(false); } // add the highway tag. profileTags.AddOrReplace("highway", highway); // normalize maxspeed tags. tags.NormalizeMaxspeed(profileTags); // normalize oneway tags. tags.NormalizeOneway(profileTags); tags.NormalizeOnewayBicycle(profileTags); // normalize cyclceway. tags.NormalizeCycleway(profileTags); // normalize junction=roundabout tag. tags.NormalizeJunction(profileTags); // normalize access tags. foreach (var vehicle in vehicleCache.Vehicles) { tags.NormalizeAccess(vehicleCache, vehicle, highway, profileTags); } // add whitelisted tags but only when they haven't been considered for normalization. foreach (var vehicle in vehicleCache.Vehicles) { foreach (var key in vehicle.ProfileWhiteList) { var value = string.Empty; if (tags.TryGetValue(key, out value)) { if (!normalizedTags.Contains(key)) { profileTags.AddOrReplace(key, value); } } } } return(true); }