Пример #1
0
        /// <summary>
        /// Executes the actual algorithm.
        /// </summary>
        protected override void DoRun()
        {
            _results = new List <RouterPoint>();

            // get the closest edge.
            var edges = _graph.SearchCloserThan(_latitude, _longitude,
                                                _maxOffset, _maxDistance, _isAcceptable);

            if (edges.Count == 0)
            { // oeps, no edge was found, too far from road network.
                this.ErrorMessage = string.Format("Could not resolve point at [{0}, {1}]. Probably too far from closest road or outside of the loaded network.",
                                                  _latitude.ToInvariantString(), _longitude.ToInvariantString());
                return;
            }

            // project onto the edge.
            for (var e = 0; e < edges.Count; e++)
            {
                var   edgeId = edges[e];
                var   edge = _graph.GetEdge(edgeId);
                float projectedLatitude, projectedLongitude, projectedDistanceFromFirst, totalLength, distanceToProjected;
                int   projectedShapeIndex;
                if (!_graph.ProjectOn(edge, _latitude, _longitude,
                                      out projectedLatitude, out projectedLongitude, out projectedDistanceFromFirst,
                                      out projectedShapeIndex, out distanceToProjected, out totalLength))
                {
                    var points   = _graph.GetShape(edge);
                    var previous = points[0];

                    var bestProjectedDistanceFromFirst = 0.0f;
                    projectedDistanceFromFirst = 0;
                    var bestDistanceToProjected = Coordinate.DistanceEstimateInMeter(previous,
                                                                                     new Coordinate(_latitude, _longitude));
                    projectedLatitude  = previous.Latitude;
                    projectedLongitude = previous.Longitude;
                    for (var i = 1; i < points.Count; i++)
                    {
                        var current = points[i];
                        projectedDistanceFromFirst += Coordinate.DistanceEstimateInMeter(current, previous);
                        distanceToProjected         = Coordinate.DistanceEstimateInMeter(current,
                                                                                         new Coordinate(_latitude, _longitude));
                        if (distanceToProjected < bestDistanceToProjected)
                        {
                            bestDistanceToProjected        = distanceToProjected;
                            bestProjectedDistanceFromFirst = projectedDistanceFromFirst;
                            projectedLatitude  = current.Latitude;
                            projectedLongitude = current.Longitude;
                        }
                        previous = current;
                    }

                    // set best distance.
                    projectedDistanceFromFirst = bestProjectedDistanceFromFirst;
                }

                var offset = (ushort)((projectedDistanceFromFirst / totalLength) * ushort.MaxValue);
                _results.Add(new RouterPoint(_latitude, _longitude, edgeId, offset));
            }

            this.HasSucceeded = true;
        }