/// <summary> /// Returns true if the given attribute is found. /// </summary> public static bool Contains(this IReadonlyAttributeCollection attributes, string key, string value) { if (!attributes.TryGetValue(key, out var foundValue)) { return(false); } return(value == foundValue); }
/// <summary> /// Tries to get a single value for the given key. /// </summary> public static bool TryGetSingle(this IReadonlyAttributeCollection attributes, string key, out float value) { string stringValue; if (!attributes.TryGetValue(key, out stringValue)) { value = 0; return(false); } return(float.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out value)); }
/// <summary> /// Returns true if the given tags key has an associated value that can be interpreted as false. /// </summary> public static bool IsFalse(this IReadonlyAttributeCollection tags, string tagKey) { if (tags == null || string.IsNullOrWhiteSpace(tagKey)) { return(false); } string tagValue; return(tags.TryGetValue(tagKey, out tagValue) && BooleanFalseValues.Contains(tagValue.ToLowerInvariant())); }
/// <summary> /// Searches for a max axle load tag and returns the associated value. /// /// http://wiki.openstreetmap.org/wiki/Key:maxaxleload /// </summary> public static bool TryGetMaxAxleLoad(this IReadonlyAttributeCollection tags, out float kilogram) { kilogram = float.MaxValue; string tagValue; if (tags == null || !tags.TryGetValue("maxaxleload", out tagValue) || string.IsNullOrWhiteSpace(tagValue)) { return(false); } return(IAttributeCollectionExtension.TryParseWeight(tagValue, out kilogram)); }
/// <summary> /// Searches for a maxspeed tag and returns the associated value. /// /// http://wiki.openstreetmap.org/wiki/Key:maxspeed /// </summary> public static bool TryGetMaxSpeed(this IReadonlyAttributeCollection attributes, out float kmPerHour) { kmPerHour = float.MaxValue; string tagValue; if (attributes == null || !attributes.TryGetValue("maxspeed", out tagValue) || string.IsNullOrWhiteSpace(tagValue) || tagValue == "none" || tagValue == "signals" || tagValue == "signs" || tagValue == "no") { return(false); } return(IAttributeCollectionExtension.TryParseSpeed(tagValue, out kmPerHour)); }
/// <summary> /// Searches for a max width tag and returns the associated value. /// /// http://wiki.openstreetmap.org/wiki/Key:maxwidth /// </summary> /// <param name="attributes">The tags to search.</param> /// <param name="meter"></param> /// <returns></returns> public static bool TryGetMaxWidth(this IReadonlyAttributeCollection attributes, out float meter) { meter = float.MaxValue; string tagValue; if (attributes == null || !attributes.TryGetValue("maxwidth", out tagValue) || string.IsNullOrWhiteSpace(tagValue)) { return(false); } return(IAttributeCollectionExtension.TryParseLength(tagValue, out meter)); }
/// <summary> /// Returns true if the given tags key has an associated value that can be interpreted as true. /// </summary> public static bool IsTrue(this IReadonlyAttributeCollection tags, string tagKey) { if (tags == null || string.IsNullOrWhiteSpace(tagKey)) { return(false); } // TryGetValue tests if the 'tagKey' is present, returns true if the associated value can be interpreted as true. // returns false if the associated value can be interpreted as false. string tagValue; return(tags.TryGetValue(tagKey, out tagValue) && BooleanTrueValues.Contains(tagValue.ToLowerInvariant())); }
/// <summary> /// Searches the attributes collection for the access attributes and returns the associated values. /// /// http://wiki.openstreetmap.org/wiki/Key:access /// </summary> /// <param name="tags">The tags to search.</param> /// <param name="accessTagHierachy">The hierarchy of <c>Access</c>-Tags for different vehicle types.</param> /// <returns>The best fitting value is returned.</returns> public static string GetAccessTag(this IReadonlyAttributeCollection tags, IEnumerable <string> accessTagHierachy) { if (tags == null) { return(null); } foreach (string s in accessTagHierachy) { string access; if (tags.TryGetValue(s, out access)) { return(access); } } return(null); }
/// <summary> /// Returns true if the given attribute collection contains the same attributes than the given collection. /// </summary> public static bool ContainsSame(this IReadonlyAttributeCollection attributes, IReadonlyAttributeCollection other, params string[] exclude) { var attributesCount = 0; var otherCount = 0; if (attributes != null) { foreach (var a in attributes) { if (exclude.Contains(a.Key)) { continue; } attributesCount++; if (!other.Contains(a.Key, a.Value)) { return(false); } } } if (other != null) { foreach (var a in other) { if (exclude.Contains(a.Key)) { continue; } otherCount++; if (!attributes.Contains(a.Key, a.Value)) { return(false); } } } return(attributesCount == otherCount); }
/// <summary> /// Returns true if the given attribute collection contains the same attributes than the given collection. /// </summary> public static bool ContainsSame(this IReadonlyAttributeCollection attributes, IReadonlyAttributeCollection other) { if (attributes == null && other == null) { return(true); } else if (attributes == null) { return(other.Count == 0); } else if (other == null) { return(attributes.Count == 0); } if (attributes.Count != other.Count) { return(false); } foreach (var a in attributes) { if (!other.Contains(a.Key, a.Value)) { return(false); } } foreach (var a in other) { if (!attributes.Contains(a.Key, a.Value)) { return(false); } } return(true); }
/// <summary> /// Creates a new dynamic profile based on the given lua script. /// </summary> public DynamicVehicle(string script) { _profileFunctions = new Dictionary <string, object>(); _source = script; _script = new Script(); _script.DoString(script); var dynName = _script.Globals.Get("name"); if (dynName == null) { throw new Exception("Dynamic profile doesn't define a name."); } _name = dynName.String; var dynNormalize = _script.Globals.Get("normalize"); if (dynNormalize == null) { this.Normalize = true; } else { this.Normalize = dynNormalize.Boolean; } var dynVehicleTypes = _script.Globals.Get("vehicle_types"); if (dynVehicleTypes != null && dynVehicleTypes.Type == DataType.Table) { _vehicleTypes = dynVehicleTypes.Table.Values.Select(x => x.String).ToArray(); } else { _vehicleTypes = new string[0]; } var dynProfiles = _script.Globals.Get("profiles"); if (dynProfiles == null) { throw new ArgumentException("No profiles defined in lua script."); } foreach (var dynProfile in dynProfiles.Table.Pairs) { var profileDefinition = dynProfile.Value; var profileName = profileDefinition.Table.Get("name").String; var functionName = profileDefinition.Table.Get("function_name").String; var function = _script.Globals[functionName]; if (function == null) { throw new ArgumentException(string.Format("Function {0} not found in lua script.", functionName)); } var metric = ProfileMetric.Custom; var dynMetric = profileDefinition.Table.Get("metric"); if (dynMetric != null) { switch (dynMetric.String) { case "time": metric = ProfileMetric.TimeInSeconds; break; case "distance": metric = ProfileMetric.DistanceInMeters; break; } } if (!_profileFunctions.ContainsKey(functionName)) { _profileFunctions[functionName] = function; } var profile = new DynamicProfile(profileName, metric, _vehicleTypes, this, _script, function); this.Register(profile); } var dynAttributesWhitelist = this.Script.Globals.Get("meta_whitelist"); _metaWhiteList = new HashSet <string>(); if (dynAttributesWhitelist != null) { foreach (var attribute in dynAttributesWhitelist.Table.Values.Select(x => x.String)) { _metaWhiteList.Add(attribute); } } dynAttributesWhitelist = this.Script.Globals.Get("profile_whitelist"); _profileWhiteList = new HashSet <string>(); if (dynAttributesWhitelist != null) { foreach (var attribute in dynAttributesWhitelist.Table.Values.Select(x => x.String)) { _profileWhiteList.Add(attribute); } } var dynParameters = _script.Globals.Get("parameters"); var parameters = new AttributeCollection(); if (dynParameters != null && dynParameters.Table != null) { foreach (var dynParameter in dynParameters.Table.Pairs) { var parameterName = dynParameter.Key; var parameterValue = dynParameter.Value; parameters.AddOrReplace(parameterName.String, parameterValue.String); } } _parameters = parameters; }