/// <summary> /// Validates geolocation candidates by streets geocoder. /// </summary> /// <param name="addresses">Address to geocode.</param> /// <param name="candidates">Geocded candidates.</param> private void _ValidateLocation(Address[] addresses, AddressCandidate[] candidates) { Debug.Assert(null != addresses); // created Debug.Assert(null != candidates); // created Debug.Assert(candidates.Length == addresses.Length); // valid stated Debug.Assert(null != _checker); // inited // init location validator var streetsGeocoder = App.Current.StreetsGeocoder; var locationValidator = new LocationValidator(streetsGeocoder); // do validation var incorrectCandidates = locationValidator .FindIncorrectLocations(candidates) .GroupBy(index => addresses[index]) .ToArray(); // get incorrect candidate indexes var addressesForGeocoding = incorrectCandidates .Select(item => item.Key) .ToArray(); _checker.ThrowIfCancellationRequested(); // regeocoding by streets geocoder var fixedCandidates = streetsGeocoder.BatchGeocode(addressesForGeocoding); // update regeocoded candidates for (var index = 0; index < fixedCandidates.Length; ++index) { _checker.ThrowIfCancellationRequested(); foreach (var j in incorrectCandidates[index]) { candidates[j] = fixedCandidates[index]; } } }
/// <summary> /// Validates and fixes candidates with incorrect locations. /// </summary> /// <param name="candidates">The reference to the collection of address candidates to /// be validated and fixed.</param> /// <param name="geocodable">The reference to the geocodable object used for retrieving /// candidates collection.</param> /// <returns>A reference to the collection of address candidates with fixed /// locations.</returns> private IEnumerable<AddressCandidate> _GetValidLocations( IEnumerable<AddressCandidate> candidates, IGeocodable geocodable) { Debug.Assert(candidates != null); Debug.Assert(candidates.All(candidate => candidate != null)); Debug.Assert(geocodable != null); List<int> incorrectCandidates = new List<int>(); try { var streetsGeocoder = App.Current.StreetsGeocoder; var locationValidator = new LocationValidator(streetsGeocoder); incorrectCandidates = locationValidator .FindIncorrectLocations(candidates) .ToList(); } catch (Exception ex) { if (GeocodeHelpers.MustThrowException(ex)) { throw; } } if (!incorrectCandidates.Any()) { return candidates; } // Get incorrect address candidates. List<AddressCandidate> allCandidates = candidates.ToList(); var invalidAddressCandidates = incorrectCandidates .Select(index => allCandidates[index]) .ToList(); // Get all candidates which is not invalid. var result = candidates .Except(invalidAddressCandidates) .ToList(); return result; }