private List <RouterPoint> _resolvedPoints; // only the valid resolved points. /// <summary> /// Executes the actual algorithm. /// </summary> protected override void DoRun(CancellationToken cancellationToken) { if (_resolvedPoints != null) { // points are already available. _errors = new Dictionary <int, LocationError>(); this.HasSucceeded = true; return; } _errors = new Dictionary <int, LocationError>(_locations.Length); _resolvedPoints = new List <RouterPoint>(_locations.Length); _originalLocationIndexes = new List <int>(_locations.Length); // resolve all locations. var resolvedPoints = new RouterPoint[_locations.Length]; for (var i = 0; i < _locations.Length; i++) { Result <RouterPoint> resolveResult = null; if (_matchEdge != null) { // use an edge-matcher. resolveResult = _router.TryResolve(_profiles, _locations[i], (edge) => { return(_matchEdge(edge, i)); }, _maxSearchDistance); } else { // don't use an edge-matcher. resolveResult = _router.TryResolve(_profiles, _locations[i], _maxSearchDistance); } if (!resolveResult.IsError) { // resolving was succesful. resolvedPoints[i] = resolveResult.Value; } } // remove all points that could not be resolved. for (var i = 0; i < resolvedPoints.Length; i++) { if (resolvedPoints[i] == null) { // could not be resolved! _errors[i] = new LocationError() { Code = LocationErrorCode.NotResolved, Message = "Location could not be linked to the road network." }; } else { // resolve is ok. resolvedPoints[i].Attributes.AddOrReplace("index", i.ToInvariantString()); _originalLocationIndexes.Add(i); _resolvedPoints.Add(resolvedPoints[i]); } } this.HasSucceeded = true; }
/// <summary> /// Executes the algorithm. /// </summary> protected override void DoRun() { _errors = new Dictionary <int, LocationError>(_locations.Length); _resolvedPoints = new List <RouterPoint>(_locations.Length); _resolvedPointsIndices = new List <int>(_locations.Length); // resolve all locations. var resolvedPoints = new RouterPoint[_locations.Length]; var profiles = new Profile[] { _profile }; for (var i = 0; i < _locations.Length; i++) { Result <RouterPoint> resolveResult = null; if (_matchEdge != null) { // use an edge-matcher. resolveResult = _router.TryResolve(profiles, _locations[i], (edge) => { return(_matchEdge(edge, i)); }, this.SearchDistanceInMeter); } else { // don't use an edge-matcher. resolveResult = _router.TryResolve(profiles, _locations[i], this.SearchDistanceInMeter); } if (!resolveResult.IsError) { // resolving was succesful. resolvedPoints[i] = resolveResult.Value; } } // remove all points that could not be resolved. for (var i = 0; i < resolvedPoints.Length; i++) { if (resolvedPoints[i] == null) { // could not be resolved! _errors[i] = new LocationError() { Code = LocationErrorCode.NotResolved, Message = "Location could not be linked to the road network." }; } else { // resolve is ok. resolvedPoints[i].Attributes.AddOrReplace("index", i.ToInvariantString()); _resolvedPointsIndices.Add(i); _resolvedPoints.Add(resolvedPoints[i]); } } // calculate matrix. var nonNullResolvedArray = _resolvedPoints.ToArray(); var nonNullInvalids = new HashSet <int>(); _weights = _router.CalculateWeight(_profile, _weightHandler, nonNullResolvedArray, nonNullInvalids); // take into account the non-null invalids now. if (nonNullInvalids.Count > 0) { // shrink lists and add errors. foreach (var invalid in nonNullInvalids) { _errors[_resolvedPointsIndices[invalid]] = new LocationError() { Code = LocationErrorCode.NotRoutable, Message = "Location could not routed to or from." }; } _resolvedPoints = _resolvedPoints.ShrinkAndCopyList(nonNullInvalids); _resolvedPointsIndices = _resolvedPointsIndices.ShrinkAndCopyList(nonNullInvalids); _weights = _weights.SchrinkAndCopyMatrix(nonNullInvalids); } this.HasSucceeded = true; }