public void TestHighway()
        {
            Itinero.Osm.Vehicles.Vehicle.RegisterVehicles();

            var tags        = new AttributeCollection();
            var profileTags = new AttributeCollection();
            var metaTags    = new AttributeCollection();

            Assert.IsFalse(tags.Normalize(profileTags, metaTags, Itinero.Osm.Vehicles.Vehicle.GetAllRegistered()));

            tags.AddOrReplace(new Attribute("highway", "residential"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, Itinero.Osm.Vehicles.Vehicle.GetAllRegistered()));
            Assert.IsTrue(profileTags.Contains("highway", "residential"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace(new Attribute("highway", "footway"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, Itinero.Osm.Vehicles.Vehicle.GetAllRegistered()));
            Assert.IsTrue(profileTags.Contains("highway", "footway"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace(new Attribute("highway", "motorway"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, Itinero.Osm.Vehicles.Vehicle.GetAllRegistered()));
            Assert.IsTrue(profileTags.Contains("highway", "motorway"));
            profileTags.Clear();
            tags.Clear();
        }
예제 #2
0
        public void TestNormalizeMaxspeed()
        {
            var maxspeed = string.Empty;

            var tags = new AttributeCollection(
                new Attribute("maxspeed", "100"));
            var profile = new AttributeCollection();

            tags.NormalizeMaxspeed(profile);
            Assert.IsTrue(profile.TryGetValue("maxspeed", out maxspeed));
            Assert.AreEqual("100", maxspeed);

            tags = new AttributeCollection(
                new Attribute("maxspeed", "100 mph"));
            profile.Clear();
            tags.NormalizeMaxspeed(profile);
            Assert.IsTrue(profile.TryGetValue("maxspeed", out maxspeed));
            Assert.AreEqual("100 mph", maxspeed);

            tags = new AttributeCollection(
                new Attribute("maxspeed", "mph"));
            profile.Clear();
            tags.NormalizeMaxspeed(profile);
            Assert.IsFalse(profile.TryGetValue("maxspeed", out maxspeed));
        }
        public void TestBicycleRestrictions()
        {
            Itinero.Osm.Vehicles.Vehicle.RegisterVehicles();
            var vehicles = new Itinero.Osm.Vehicles.Vehicle[] { Itinero.Osm.Vehicles.Vehicle.Bicycle };

            var tags        = new AttributeCollection();
            var profileTags = new AttributeCollection();
            var metaTags    = new AttributeCollection();

            tags.AddOrReplace(new Attribute("highway", "residential"));
            tags.AddOrReplace(new Attribute("bicycle", "yes"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.IsTrue(profileTags.Contains("highway", "residential"));
            Assert.IsFalse(profileTags.Contains("bicycle", "yes"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace(new Attribute("highway", "residential"));
            tags.AddOrReplace(new Attribute("bicycle", "no"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.IsTrue(profileTags.Contains("highway", "residential"));
            Assert.IsTrue(profileTags.Contains("bicycle", "no"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace(new Attribute("highway", "residential"));
            tags.AddOrReplace(new Attribute("bicycle", "mistake"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.IsTrue(profileTags.Contains("highway", "residential"));
            Assert.IsFalse(profileTags.Contains("bicycle", "mistake"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace(new Attribute("highway", "footway"));
            tags.AddOrReplace(new Attribute("bicycle", "no"));
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.IsTrue(profileTags.Contains("highway", "footway"));
            Assert.IsFalse(profileTags.Contains("bicycle", "no"));
            profileTags.Clear();
            tags.Clear();

            vehicles = new Itinero.Osm.Vehicles.Vehicle[] { Itinero.Osm.Vehicles.Vehicle.Car };

            tags.AddOrReplace("highway", "residential");
            tags.AddOrReplace("bicycle", "no");
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.IsTrue(profileTags.Contains("highway", "residential"));
            Assert.IsFalse(profileTags.Contains("bicycle", "no"));
            profileTags.Clear();
            tags.Clear();
        }
        public void TestMotorwayAccess()
        {
            Itinero.Osm.Vehicles.Vehicle.RegisterVehicles();
            var vehicles = new Itinero.Osm.Vehicles.Vehicle[] {
                Itinero.Osm.Vehicles.Vehicle.Pedestrian,
                Itinero.Osm.Vehicles.Vehicle.Bicycle,
                Itinero.Osm.Vehicles.Vehicle.Car
            };

            var tags        = new AttributeCollection();
            var profileTags = new AttributeCollection();
            var metaTags    = new AttributeCollection();

            tags.AddOrReplace("highway", "motorway");
            tags.AddOrReplace("access", "no");
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.AreEqual(2, profileTags.Count);
            Assert.IsTrue(profileTags.Contains("highway", "motorway"));
            Assert.IsTrue(profileTags.Contains("motorcar", "no"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace("highway", "motorway");
            tags.AddOrReplace("access", "yes");
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.AreEqual(3, profileTags.Count);
            Assert.IsTrue(profileTags.Contains("highway", "motorway"));
            Assert.IsTrue(profileTags.Contains("bicycle", "yes"));
            Assert.IsTrue(profileTags.Contains("foot", "yes"));
            profileTags.Clear();
            tags.Clear();

            tags.AddOrReplace("highway", "motorway");
            tags.AddOrReplace("access", "no");
            tags.AddOrReplace("vehicle", "yes");
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, vehicles));
            Assert.AreEqual(2, profileTags.Count);
            Assert.IsTrue(profileTags.Contains("highway", "motorway"));
            Assert.IsTrue(profileTags.Contains("bicycle", "yes"));
            profileTags.Clear();
            tags.Clear();
        }
예제 #5
0
        public void TestClearingCollection()
        {
            AttributeCollection attributes = new AttributeCollection();

            attributes.Add("Test1", new AttributeValue("Test Value1"));
            attributes.Add("Test2", new AttributeValue("Test Value2"));
            attributes.Add("Test3", new AttributeValue("Test Value3"));
            attributes.Add("Test4", new AttributeValue("Test Value4"));

            Assert.AreEqual <int>(4, attributes.Count);

            attributes.Clear();

            Assert.AreEqual <int>(0, attributes.Count);
        }
        public void TestRamp()
        {
            Itinero.Osm.Vehicles.Vehicle.RegisterVehicles();

            var tags        = new AttributeCollection();
            var profileTags = new AttributeCollection();
            var metaTags    = new AttributeCollection();

            tags.AddOrReplace("highway", "steps");
            tags.AddOrReplace("ramp", "yes");
            Assert.IsTrue(tags.Normalize(profileTags, metaTags, Itinero.Osm.Vehicles.Vehicle.GetAllRegistered()));
            Assert.IsTrue(profileTags.Contains("highway", "steps"));
            Assert.IsTrue(profileTags.Contains("ramp", "yes"));
            profileTags.Clear();
            tags.Clear();
        }
        public void Deny_Unrestricted()
        {
            // nothing else is required
            AttributeCollection ac = new AttributeCollection(bag);

            Assert.AreEqual(0, ac.Count, "Count");
            Assert.IsNotNull(ac.CssStyle, "CssStyle");
            ac["mono"] = "monkey";
            Assert.AreEqual("monkey", ac["mono"], "this");
            Assert.IsNotNull(ac.Keys, "Keys");

            ac.Add("monkey", "mono");
            ac.AddAttributes(writer);
            ac.Clear();
            ac.Remove("mono");
            ac.Render(writer);
        }
예제 #8
0
        /// <summary>
        /// Executes the actual algorithm.
        /// </summary>
        protected override void DoRun()
        {
            foreach (var vehicle in _vehicles)
            {
                _routerDb.AddSupportedVehicle(vehicle);
            }

            var nodeToVertex = new Dictionary <long, uint>();

            // read all vertices.
            var startTicks = DateTime.Now.Ticks;

            for (int readerIdx = 0; readerIdx < _shapefileReaders.Count; readerIdx++)
            {
                var reader = _shapefileReaders[readerIdx];
                var header = new Dictionary <string, int>();

                // make sure the header is loaded.
                if (header.Count == 0)
                { // build header.
                    for (int idx = 0; idx < reader.DbaseHeader.Fields.Length; idx++)
                    {
                        header.Add(reader.DbaseHeader.Fields[idx].Name, idx + 1);
                    }

                    // check if all columns are in the header.
                    if (!header.ContainsKey(_sourceVertexColumn))
                    { // no node from column.
                        throw new InvalidOperationException(string.Format("No column with name {0} found.", _sourceVertexColumn));
                    }
                    if (!header.ContainsKey(_targetVertexColumn))
                    { // no node to column.
                        throw new InvalidOperationException(string.Format("No column with name {0} found.", _targetVertexColumn));
                    }
                }

                // read all vertices.
                double latestProgress = 0;
                int    current        = 0;
                while (reader.Read())
                {
                    _points += 2;

                    // get the geometry.
                    var lineString = reader.Geometry as LineString;

                    // read nodes
                    long fromId = reader.GetInt64(header[_sourceVertexColumn]);
                    if (!nodeToVertex.ContainsKey(fromId))
                    { // the node has not been processed yet.
                        var vertexId = _routerDb.Network.VertexCount;
                        _routerDb.Network.AddVertex(vertexId,
                                                    (float)lineString.Coordinates[0].Y,
                                                    (float)lineString.Coordinates[0].X);
                        nodeToVertex.Add(fromId, vertexId);
                    }

                    long toId = reader.GetInt64(header[_targetVertexColumn]);
                    if (!nodeToVertex.ContainsKey(toId))
                    { // the node has not been processed yet.
                        var vertexId = _routerDb.Network.VertexCount;
                        _routerDb.Network.AddVertex(vertexId,
                                                    (float)lineString.Coordinates[lineString.Coordinates.Length - 1].Y,
                                                    (float)lineString.Coordinates[lineString.Coordinates.Length - 1].X);
                        nodeToVertex.Add(toId, vertexId);
                    }

                    // report progress.
                    float progress = (float)System.Math.Round((((double)current / (double)reader.RecordCount) * 100));
                    current++;
                    if (progress != latestProgress)
                    {
                        var pointSpan      = new TimeSpan(DateTime.Now.Ticks - startTicks);
                        var pointPerSecond = System.Math.Round((double)_points / pointSpan.TotalSeconds, 0);
                        Itinero.Logging.Logger.Log("ShapeFileReader", TraceEventType.Information,
                                                   "Reading vertices from file {1}/{2}... {0}% @ {3}/s", progress, readerIdx + 1, _shapefileReaders.Count, pointPerSecond);
                        latestProgress = progress;
                    }
                }
            }

            // read all edges.
            startTicks = DateTime.Now.Ticks;
            var attributes = new AttributeCollection();

            for (int readerIdx = 0; readerIdx < _shapefileReaders.Count; readerIdx++)
            {
                var reader = _shapefileReaders[readerIdx];
                var header = new Dictionary <string, int>();

                // make sure the header is loaded.
                if (header.Count == 0)
                { // build header.
                    for (int idx = 0; idx < reader.DbaseHeader.Fields.Length; idx++)
                    {
                        header.Add(reader.DbaseHeader.Fields[idx].Name, idx + 1);
                    }
                }

                // reset reader and read all edges/arcs.
                double latestProgress = 0;
                int    current        = 0;
                reader.Reset();
                while (reader.Read())
                {
                    _lineStrings++;

                    // get the geometry.
                    var lineString = reader.Geometry as LineString;

                    // read nodes
                    long vertex1Shape = reader.GetInt64(header[_sourceVertexColumn]);
                    long vertex2Shape = reader.GetInt64(header[_targetVertexColumn]);
                    uint vertex1, vertex2;
                    if (nodeToVertex.TryGetValue(vertex1Shape, out vertex1) &&
                        nodeToVertex.TryGetValue(vertex2Shape, out vertex2))
                    { // the node has not been processed yet.
                        // add intermediates.
                        var intermediates = new List <Coordinate>(lineString.Coordinates.Length);
                        for (int i = 1; i < lineString.Coordinates.Length - 1; i++)
                        {
                            intermediates.Add(new Coordinate()
                            {
                                Latitude  = (float)lineString.Coordinates[i].Y,
                                Longitude = (float)lineString.Coordinates[i].X
                            });
                        }

                        // calculate the distance.
                        float distance = 0;
                        float latitudeFrom, latitudeTo, longitudeFrom, longitudeTo;
                        if (_routerDb.Network.GetVertex(vertex1, out latitudeFrom, out longitudeFrom) &&
                            _routerDb.Network.GetVertex(vertex2, out latitudeTo, out longitudeTo))
                        { // calculate distance.
                            var fromLocation = new Coordinate(latitudeFrom, longitudeFrom);
                            for (int i = 0; i < intermediates.Count; i++)
                            {
                                var currentLocation = new Coordinate(intermediates[i].Latitude, intermediates[i].Longitude);
                                distance     = distance + Coordinate.DistanceEstimateInMeter(fromLocation, currentLocation);
                                fromLocation = currentLocation;
                            }
                            var toLocation = new Coordinate(latitudeTo, longitudeTo);
                            distance = distance + Coordinate.DistanceEstimateInMeter(fromLocation, toLocation);
                        }

                        // get profile and meta attributes.
                        var profile          = new AttributeCollection();
                        var meta             = new AttributeCollection();
                        var profileWhiteList = new Whitelist();
                        attributes.Clear();
                        reader.AddToAttributeCollection(attributes);
                        _vehicleCache.AddToWhiteList(attributes, profileWhiteList);
                        for (var i = 1; i < reader.FieldCount; i++)
                        {
                            var name        = reader.GetName(i);
                            var value       = reader.GetValue(i);
                            var valueString = string.Empty;
                            if (value != null)
                            {
                                valueString = value.ToInvariantString();
                            }

                            if (profileWhiteList.Contains(name) ||
                                _vehicles.IsOnProfileWhiteList(name))
                            {
                                profile.AddOrReplace(name, valueString);
                            }
                            else if (_vehicles.IsOnMetaWhiteList(name))
                            {
                                meta.AddOrReplace(name, valueString);
                            }
                        }

                        // add edge.
                        var profileId = _routerDb.EdgeProfiles.Add(profile);
                        if (profileId >= Data.Edges.EdgeDataSerializer.MAX_PROFILE_COUNT)
                        {
                            throw new Exception("Maximum supported profiles exeeded, make sure only routing attributes are included in the profiles.");
                        }
                        var metaId = _routerDb.EdgeMeta.Add(meta);
                        if (vertex1 != vertex2)
                        {
                            if (distance > _routerDb.Network.MaxEdgeDistance)
                            { // edge is too long to fit into the network, adding an itermediate vertex.
                                var shape = intermediates;
                                if (shape == null)
                                { // make sure there is a shape.
                                    shape = new List <Coordinate>();
                                }

                                shape = new List <Coordinate>(shape);
                                shape.Insert(0, _routerDb.Network.GetVertex(vertex1));
                                shape.Add(_routerDb.Network.GetVertex(vertex2));

                                var tooBig = true;
                                while (tooBig)
                                {
                                    tooBig = false;
                                    for (var s = 1; s < shape.Count; s++)
                                    {
                                        var localDistance = Coordinate.DistanceEstimateInMeter(shape[s - 1], shape[s]);
                                        if (localDistance >= _routerDb.Network.MaxEdgeDistance)
                                        { // insert a new intermediate.
                                            shape.Insert(s,
                                                         new Coordinate()
                                            {
                                                Latitude = (float)(((double)shape[s - 1].Latitude +
                                                                    (double)shape[s].Latitude) / 2.0),
                                                Longitude = (float)(((double)shape[s - 1].Longitude +
                                                                     (double)shape[s].Longitude) / 2.0),
                                            });
                                            tooBig = true;
                                            s--;
                                        }
                                    }
                                }

                                var        i             = 0;
                                var        shortShape    = new List <Coordinate>();
                                var        shortDistance = 0.0f;
                                uint       shortVertex   = Constants.NO_VERTEX;
                                Coordinate?shortPoint;
                                i++;
                                while (i < shape.Count)
                                {
                                    var localDistance = Coordinate.DistanceEstimateInMeter(shape[i - 1], shape[i]);
                                    if (localDistance + shortDistance > _routerDb.Network.MaxEdgeDistance)
                                    { // ok, previous shapepoint was the maximum one.
                                        shortPoint = shortShape[shortShape.Count - 1];
                                        shortShape.RemoveAt(shortShape.Count - 1);

                                        // add vertex.
                                        shortVertex = _routerDb.Network.VertexCount;
                                        _routerDb.Network.AddVertex(shortVertex, shortPoint.Value.Latitude,
                                                                    shortPoint.Value.Longitude);

                                        // add edge.
                                        _routerDb.Network.AddEdge(vertex1, shortVertex, new Data.Network.Edges.EdgeData()
                                        {
                                            Distance = (float)shortDistance,
                                            MetaId   = metaId,
                                            Profile  = (ushort)profileId
                                        }, shortShape);
                                        vertex1 = shortVertex;

                                        // set new short distance, empty shape.
                                        shortShape.Clear();
                                        shortShape.Add(shape[i]);
                                        shortDistance = localDistance;
                                        i++;
                                    }
                                    else
                                    { // just add short distance and move to the next shape point.
                                        shortShape.Add(shape[i]);
                                        shortDistance += localDistance;
                                        i++;
                                    }
                                }

                                // add final segment.
                                if (shortShape.Count > 0)
                                {
                                    shortShape.RemoveAt(shortShape.Count - 1);
                                }

                                // add edge.
                                _routerDb.Network.AddEdge(vertex1, vertex2, new Data.Network.Edges.EdgeData()
                                {
                                    Distance = (float)shortDistance,
                                    MetaId   = metaId,
                                    Profile  = (ushort)profileId
                                }, shortShape);
                            }
                            else
                            {
                                this.AddEdge(vertex1, vertex2, new Data.Network.Edges.EdgeData()
                                {
                                    Distance = distance,
                                    MetaId   = metaId,
                                    Profile  = (ushort)profileId
                                }, intermediates);
                            }
                        }
                    }

                    // report progress.
                    float progress = (float)System.Math.Round((((double)current / (double)reader.RecordCount) * 100));
                    current++;
                    if (progress != latestProgress)
                    {
                        var span      = new TimeSpan(DateTime.Now.Ticks - startTicks);
                        var perSecond = System.Math.Round((double)_lineStrings / span.TotalSeconds, 0);
                        Itinero.Logging.Logger.Log("ShapeFileReader", TraceEventType.Information,
                                                   "Reading edges {1}/{2}... {0}% @ {3}/s", progress, readerIdx + 1, _shapefileReaders.Count, perSecond);
                        latestProgress = progress;
                    }
                }
            }

            // sort the network.
            Itinero.Logging.Logger.Log("ShapeFileReader", TraceEventType.Information, "Sorting vertices...");
            _routerDb.Sort();

            this.HasSucceeded = true;
        }
예제 #9
0
        public void TestClearingCollection()
        {
            AttributeCollection attributes = new AttributeCollection();

            attributes.Add("Test1", new AttributeValue("Test Value1"));
            attributes.Add("Test2", new AttributeValue("Test Value2"));
            attributes.Add("Test3", new AttributeValue("Test Value3"));
            attributes.Add("Test4", new AttributeValue("Test Value4"));

            Assert.AreEqual<int>(4, attributes.Count);

            attributes.Clear();

            Assert.AreEqual<int>(0, attributes.Count);
        }