Example #1
0
        public void TestDistanceTo()
        {
            // build router db.
            var routerDb = new RouterDb();

            routerDb.Network.AddVertex(0, 0, 0);
            routerDb.Network.AddVertex(1, .1f, .1f);
            routerDb.Network.AddEdge(0, 1, new EdgeData()
            {
                Distance = 1000,
                MetaId   = routerDb.EdgeProfiles.Add(new AttributeCollection(
                                                         new Attribute("name", "Abelshausen Blvd."))),
                Profile = (ushort)routerDb.EdgeProfiles.Add(new AttributeCollection(
                                                                new Attribute("highway", "residential")))
            }, new Coordinate(0.025f, 0.025f),
                                     new Coordinate(0.050f, 0.050f),
                                     new Coordinate(0.075f, 0.075f));

            // mock profile.
            var profile = MockProfile.CarMock();

            var point = new RouterPoint(0.04f, 0.04f, 0, (ushort)(0.4 * ushort.MaxValue));

            var distance = point.DistanceTo(routerDb, 0);

            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(new Coordinate(0, 0),
                                                               new Coordinate(0.04f, 0.04f)), distance, 0.001);

            distance = point.DistanceTo(routerDb, 1);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(new Coordinate(.1f, .1f),
                                                               new Coordinate(0.04f, 0.04f)), distance, 0.001);
        }
Example #2
0
        /// <summary>
        /// Adds the shape point between from and to and the target location itself.
        /// </summary>
        private void Add(uint from, uint to)
        {
            if (from == Constants.NO_VERTEX &&
                _source.IsVertex())
            { // replace from with the vertex.
                from = _source.VertexId(_routerDb);
            }
            if (to == Constants.NO_VERTEX &&
                _target.IsVertex())
            { // replace to with the vertex.
                to = _target.VertexId(_routerDb);
            }

            if (to == from && to != Constants.NO_VERTEX)
            { // nothing to be done.
                return;
            }

            // get shapepoints and edge.
            var         shape          = new List <Coordinate>(0);
            RoutingEdge edge           = null;
            Coordinate? targetLocation = null;
            var         distance       = 0f;
            var         direction      = true;

            if (from == Constants.NO_VERTEX &&
                to == Constants.NO_VERTEX)
            {     // from is the source and to is the target.
                if (_source.EdgeId != _target.EdgeId)
                { // a route inside one edge but source and target do not match.
                    this.ErrorMessage = "Target and source have to be on the same vertex with a route with only virtual vertices.";
                    return;
                }
                shape          = _source.ShapePointsTo(_routerDb, _target);
                distance       = _source.DistanceTo(_routerDb, _target);
                edge           = _routerDb.Network.GetEdge(_source.EdgeId);
                targetLocation = _target.Location();
                shape.Add(targetLocation.Value);
            }
            else if (from == Constants.NO_VERTEX)
            { // from is the source and to is a regular vertex.
                edge = _routerDb.Network.GetEdge(_source.EdgeId);
                var toOnEdge = _routerDb.Network.CreateRouterPointForVertex(to,
                                                                            edge.GetOther(to));
                shape          = _source.ShapePointsTo(_routerDb, toOnEdge);
                distance       = _source.DistanceTo(_routerDb, toOnEdge);
                targetLocation = _routerDb.Network.GetVertex(to);
                shape.Add(targetLocation.Value);
            }
            else if (to == Constants.NO_VERTEX)
            { // from is a regular vertex and to is the target.
                edge = _routerDb.Network.GetEdge(_target.EdgeId);
                var fromOnEdge = _routerDb.Network.CreateRouterPointForVertex(from,
                                                                              edge.GetOther(from));
                shape          = fromOnEdge.ShapePointsTo(_routerDb, _target);
                distance       = fromOnEdge.DistanceTo(_routerDb, _target);
                targetLocation = _target.Location();
                shape.Add(targetLocation.Value);
            }
            else
            { // both are just regular vertices.
                edge      = _routerDb.Network.GetEdgeEnumerator(from).First(x => x.To == to);
                direction = !edge.DataInverted;
                distance  = edge.Data.Distance;
                var shapeEnumerable = edge.Shape;
                if (shapeEnumerable != null)
                {
                    if (edge.DataInverted)
                    {
                        shapeEnumerable = shapeEnumerable.Reverse();
                    }
                    shape.AddRange(shapeEnumerable);
                }
                targetLocation = _routerDb.Network.GetVertex(to);
                shape.Add(targetLocation.Value);
            }

            // get edge details.
            var profile = _routerDb.EdgeProfiles.Get(edge.Data.Profile);
            var speed   = this._profile.Speed(profile);
            var time    = 0f;

            if (speed.Value != 0)
            {
                time = distance / speed.Value;
            }
            var meta       = _routerDb.EdgeMeta.Get(edge.Data.MetaId);
            var attributes = new AttributeCollection(meta);

            attributes.AddOrReplace(profile);
            attributes.AddOrReplace("profile", _profile.FullName);

            // add shape and meta.
            _shape.AddRange(shape);
            var previousMeta = _shapeMeta[_shapeMeta.Count - 1];
            var shapeMeta    = new Route.Meta()
            {
                Shape               = _shape.Count - 1,
                Attributes          = attributes,
                AttributesDirection = direction
            };

            shapeMeta.Distance = distance + previousMeta.Distance;
            shapeMeta.Time     = time + previousMeta.Time;
            _shapeMeta.Add(shapeMeta);
        }