Esempio n. 1
0
        /// <summary>
        /// Normalizes access for the given hierarchy of access tags.
        /// </summary>
        public static void NormalizeAccess(this IAttributeCollection tags, IAttributeCollection profileTags, bool defaultAccess, params string[] accessTags)
        {
            bool?access = Itinero.Osm.Vehicles.Vehicle.InterpretAccessValue(tags, "access");

            for (var i = 0; i < accessTags.Length; i++)
            {
                var currentAccess = Itinero.Osm.Vehicles.Vehicle.InterpretAccessValue(tags, accessTags[i]);
                if (currentAccess != null)
                {
                    access = currentAccess;
                }
            }

            if (access != null && access.Value != defaultAccess)
            {
                if (access.Value)
                {
                    profileTags.AddOrReplace(accessTags[accessTags.Length - 1], "yes");
                }
                else
                {
                    profileTags.AddOrReplace(accessTags[accessTags.Length - 1], "no");
                }
            }
        }
Esempio n. 2
0
        /// <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)
        {
            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);
        }
Esempio n. 3
0
        /// <summary>
        /// Normalize the cycleway tag.
        /// </summary>
        public static void NormalizeCycleway(this IAttributeCollection tags, IAttributeCollection profileTags)
        {
            string cycleway;

            if (!tags.TryGetValue("cycleway", out cycleway))
            { // nothing to normalize.
                return;
            }
            if (cycleway == "cyclestreet")
            {
                profileTags.AddOrReplace("cycleway", "cyclestreet");
            }
            else if (cycleway == "lane")
            {
                profileTags.AddOrReplace("cycleway", "lane");
            }

            // TODO: add the unidirectional cycleway stuff. WARNING: direction of 'left' and 'right' depends on country.
        }
Esempio n. 4
0
        /// <summary>
        /// Adds or replaces the existing attributes to/in the given collection of attributes.
        /// </summary>
        public static void AddOrReplace(this IAttributeCollection attributes, IEnumerable <Attribute> other)
        {
            if (other == null)
            {
                return;
            }

            foreach (var attribute in other)
            {
                attributes.AddOrReplace(attribute.Key, attribute.Value);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Normalizes the junction tag.
        /// </summary>
        /// <returns></returns>
        public static void NormalizeJunction(this IAttributeCollection tags, IAttributeCollection profileTags)
        {
            string junction;

            if (!tags.TryGetValue("junction", out junction))
            { // nothing to normalize.
                return;
            }
            if (junction == "roundabout")
            {
                profileTags.AddOrReplace("junction", "roundabout");
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Normalizes the oneway bicycle tag.
        /// </summary>
        public static void NormalizeOnewayBicycle(this IAttributeCollection tags, IAttributeCollection profileTags)
        {
            string oneway;

            if (!tags.TryGetValue("oneway:bicycle", out oneway))
            { // nothing to normalize.
                return;
            }
            if (oneway == "no")
            {
                profileTags.AddOrReplace("oneway:bicycle", "no");
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Normalizes the oneway tag.
        /// </summary>
        public static void NormalizeOneway(this IAttributeCollection tags, IAttributeCollection profileTags)
        {
            string oneway;

            if (!tags.TryGetValue("oneway", out oneway))
            { // nothing to normalize.
                return;
            }
            bool defaultOnewayFound;

            if (!OnewayValues.TryGetValue(oneway, out defaultOnewayFound))
            { // invalid value.
                return;
            }

            if (defaultOnewayFound)
            {
                profileTags.AddOrReplace("oneway", "yes");
            }
            else
            {
                profileTags.AddOrReplace("oneway", "-1");
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Normalizes maxspeed.
        /// </summary>
        public static void NormalizeMaxspeed(this IAttributeCollection tags, IAttributeCollection profileTags)
        {
            string maxspeed;

            if (!tags.TryGetValue("maxspeed", out maxspeed))
            { // nothing to normalize.
                return;
            }
            int maxSpeedValue;

            if (int.TryParse(maxspeed, out maxSpeedValue) &&
                maxSpeedValue > 0 && maxSpeedValue <= 200)
            {
                profileTags.AddOrReplace("maxspeed", maxspeed);
            }
            else if (maxspeed.EndsWith("mph"))
            {
                if (int.TryParse(maxspeed.Substring(0, maxspeed.Length - 4), out maxSpeedValue) &&
                    maxSpeedValue > 0 && maxSpeedValue <= 150)
                {
                    profileTags.AddOrReplace("maxspeed", maxspeed);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Adds the attributes in the current record in the shapefile reader to the given attribute collection.
        /// </summary>
        public static void AddToAttributeCollection(this ShapefileDataReader reader, IAttributeCollection collection)
        {
            var valueString = string.Empty;

            for (var i = 1; i < reader.FieldCount; i++)
            {
                var name  = reader.GetName(i);
                var value = reader.GetValue(i - 1);
                valueString = string.Empty;
                if (value != null)
                {
                    valueString = value.ToInvariantString();
                }
                collection.AddOrReplace(name, valueString);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Adds attributes to this attribute collection and adds a prefix to all keys.
        /// </summary>
        public static void AddOrReplaceWithPrefix(this IAttributeCollection attributes, string prefix, IEnumerable <Attributes.Attribute> toPrefix)
        {
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException("Not a valid prefix.");
            }

            if (toPrefix == null)
            {
                return;
            }

            foreach (var attribute in toPrefix)
            {
                attributes.AddOrReplace(prefix + attribute.Key, attribute.Value);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Normalizes the ramp tag.
        /// </summary>
        public static void NormalizeRamp(this IAttributeCollection tags, IAttributeCollection profileTags, bool defaultAccess)
        {
            string ramp;

            if (!tags.TryGetValue("ramp", out ramp))
            { // nothing to normalize.
                return;
            }
            bool?defaultAccessFound;

            if (!RampValues.TryGetValue(ramp, out defaultAccessFound))
            { // invalid value.
                return;
            }

            if (defaultAccess != defaultAccessFound)
            {
                profileTags.AddOrReplace("ramp", ramp);
            }
        }
Esempio n. 12
0
 /// <summary>
 /// Adds a tag as an attribute.
 /// </summary>
 public static void Add(this IAttributeCollection attributes, Tag tag)
 {
     attributes.AddOrReplace(tag.Key, tag.Value);
 }
Esempio n. 13
0
 /// <summary>
 /// Sets a single value.
 /// </summary>
 public static void SetSingle(this IAttributeCollection attributes, string key, float value)
 {
     attributes.AddOrReplace(key, value.ToInvariantString());
 }
Esempio n. 14
0
 /// <summary>
 /// Adds a new attribute.
 /// </summary>
 public static void AddOrReplace(this IAttributeCollection attributes, Attribute attribute)
 {
     attributes.AddOrReplace(attribute.Key, attribute.Value);
 }