Пример #1
0
        /// <summary>
        /// Executes the actual run of the algorithm.
        /// </summary>
        protected override void DoRun()
        {
            if (_path.Count == 0)
            { // an empty path.
                this.ErrorMessage = "Path was empty.";
                this.HasSucceeded = false;
                return;
            }

            // check source.
            var source = _path[0];

            if (source != Constants.NO_VERTEX &&
                !_source.IsVertex(_routerDb, source))
            {
                this.ErrorMessage = "The source is a vertex but the source is not a match.";
                this.HasSucceeded = false;
                return;
            }

            // check target.
            var target = _path[_path.Count - 1];

            if (target != Constants.NO_VERTEX &&
                !_target.IsVertex(_routerDb, target))
            {
                this.ErrorMessage = "The target is a vertex but the target is not a match.";
                this.HasSucceeded = false;
                return;
            }

            // build the route.
            _shape     = new List <Coordinate>();
            _shapeMeta = new List <Route.Meta>();
            _branches  = new List <Route.Branch>();

            // add source.
            lock (_routerDb)
            {
                this.AddSource();

                if (_path.Count == 1)
                { // there is only the source/target location.
                    this.HasSucceeded = true;
                }
                else
                { // there are at least two points.
                    var i = 0;
                    for (i = 0; i < _path.Count - 2; i++)
                    {
                        this.Add(_path[i], _path[i + 1], _path[i + 2]);
                    }
                    this.Add(_path[i], _path[i + 1]);
                    this.HasSucceeded = true;
                }
            }

            // set stops.
            var stops = new Route.Stop[]
            {
                new Route.Stop()
                {
                    Shape      = 0,
                    Attributes = new AttributeCollection(_source.Attributes),
                    Coordinate = _source.Location()
                },
                new Route.Stop()
                {
                    Shape      = _shape.Count - 1,
                    Attributes = new AttributeCollection(_target.Attributes),
                    Coordinate = _target.Location()
                }
            };

            stops[0].Distance = 0;
            stops[0].Time     = 0;
            stops[1].Distance = _shapeMeta.Last().Distance;
            stops[1].Time     = _shapeMeta.Last().Time;

            // build route.
            _route = new Route()
            {
                Shape         = _shape.ToArray(),
                ShapeMeta     = _shapeMeta.ToArray(),
                Stops         = stops,
                Branches      = _branches.ToArray(),
                TotalDistance = _shapeMeta.Last().Distance,
                TotalTime     = _shapeMeta.Last().Time,
                Profile       = _profile.FullName
            };
        }