/// <summary> /// Geocode order, using local geocoder. /// </summary> /// <param name="order">Order to geocode.</param> /// <param name="useLocalAsPrimary">If true and record exists in storage, than dont use server geocoder.</param> /// <param name="includeDisabledLocators">Is need to add candidates from disabled locators.</param> /// <returns>Candidates list.</returns> private static List <AddressCandidate> _DoLocalOrderGeocode(Order order, bool useLocalAsPrimary, bool includeDisabledLocators) { LocalGeocoder localGeocoder = new LocalGeocoder(App.Current.Geocoder, App.Current.NameAddressStorage); NameAddress nameAddress = new NameAddress(); nameAddress.Name = order.Name; nameAddress.Address = (Address)order.Address.Clone(); List <AddressCandidate> candidates = new List <AddressCandidate>(); AddressCandidate candidateFromLocalGeocoder = localGeocoder.Geocode(nameAddress); if (useLocalAsPrimary && candidateFromLocalGeocoder != null) { candidates.Add(candidateFromLocalGeocoder); } else { AddressCandidate[] candidatesArray = localGeocoder.GeocodeCandidates(nameAddress, includeDisabledLocators); candidates.AddRange(candidatesArray); } return(candidates); }
/// <summary> /// Read record from database. /// </summary> /// <param name="reader">Database reader.</param> /// <returns>Readed record.</returns> private NameAddressRecord _ReadRecord(SqlCeDataReader reader) { NameAddressRecord nameAddressRecord = new NameAddressRecord(); NameAddress nameAddress = new NameAddress(); nameAddressRecord.NameAddress = nameAddress; nameAddress.Name = (string)reader[NAME_FIELD_NAME]; Address address = new Address(); nameAddress.Address = address; address.Unit = (string)reader[UNIT_FIELD_NAME]; address.AddressLine = (string)reader[ADDRESSLINE_FIELD_NAME]; address.Locality1 = (string)reader[LOCALITY1_FIELD_NAME]; address.Locality2 = (string)reader[LOCALITY2_FIELD_NAME]; address.Locality3 = (string)reader[LOCALITY3_FIELD_NAME]; address.CountyPrefecture = (string)reader[COUNTYPREFECTURE_FIELD_NAME]; address.PostalCode1 = (string)reader[POSTALCODE1_FIELD_NAME]; address.PostalCode2 = (string)reader[POSTALCODE2_FIELD_NAME]; address.StateProvince = (string)reader[STATEPROVINCE_FIELD_NAME]; address.Country = (string)reader[COUNTRY_FIELD_NAME]; address.FullAddress = (string)reader[FULL_ADDRESS_FIELD_NAME]; float x = (float)reader[X_FIELD_NAME]; float y = (float)reader[Y_FIELD_NAME]; nameAddressRecord.GeoLocation = new ESRI.ArcLogistics.Geometry.Point(x, y); Address matchedAddress = new Address(); nameAddressRecord.MatchedAddress = matchedAddress; matchedAddress.Unit = (string)reader[MUNIT_FIELD_NAME]; matchedAddress.AddressLine = (string)reader[MADDRESSLINE_FIELD_NAME]; matchedAddress.Locality1 = (string)reader[MLOCALITY1_FIELD_NAME]; matchedAddress.Locality2 = (string)reader[MLOCALITY2_FIELD_NAME]; matchedAddress.Locality3 = (string)reader[MLOCALITY3_FIELD_NAME]; matchedAddress.CountyPrefecture = (string)reader[MCOUNTYPREFECTURE_FIELD_NAME]; matchedAddress.PostalCode1 = (string)reader[MPOSTALCODE1_FIELD_NAME]; matchedAddress.PostalCode2 = (string)reader[MPOSTALCODE2_FIELD_NAME]; matchedAddress.StateProvince = (string)reader[MSTATEPROVINCE_FIELD_NAME]; matchedAddress.Country = (string)reader[MCOUNTRY_FIELD_NAME]; matchedAddress.FullAddress = (string)reader[MFULL_ADDRESS_FIELD_NAME]; string matchMethod = (string)reader[MATCHMETHOD_FIELD_NAME]; if (CommonHelpers.IsAllAddressFieldsEmpty(matchedAddress) && string.IsNullOrEmpty(matchedAddress.MatchMethod)) { address.MatchMethod = matchMethod; } else { matchedAddress.MatchMethod = matchMethod; } return(nameAddressRecord); }
/// <summary> /// Get address candidates from local storage and from server. /// </summary> /// <param name="nameAddress">Name\Address pair to geocode.</param> /// <param name="includeDisabledLocators">Is need to add candidates from disabled locators.</param> /// <returns>Candidates list.</returns> public AddressCandidate[] GeocodeCandidates(NameAddress nameAddress, bool includeDisabledLocators) { Debug.Assert(nameAddress != null); List <AddressCandidate> candidates = _GeocodeCandidates(nameAddress.Address, includeDisabledLocators); AddressCandidate candidateFromLocalStorage = Geocode(nameAddress); if (candidateFromLocalStorage != null) { // If candidate from local storage exists - insert it to first position. candidates.Insert(0, candidateFromLocalStorage); } return(candidates.ToArray()); }
/// <summary> /// Get record from database. /// </summary> /// <param name="nameAddress">Name\address pair to find.</param> /// <param name="format">Current Address Format.</param> /// <returns>Extracted record.</returns> private NameAddressRecord _GetRecordFromDB(NameAddress nameAddress, AddressFormat format) { NameAddressRecord nameAddressRecord = null; string queryStr = string.Empty; // Choose queries in dependence of used address format. if (format == AddressFormat.SingleField) { queryStr = FULL_ADDRESS_SELECT_QUERY; } else if (format == AddressFormat.MultipleFields) { queryStr = SELECT_QUERY; } else { // Do nothing. } SqlCeCommand command = new SqlCeCommand(queryStr, _connection); _FillCommandParameters(nameAddress, command); SqlCeDataReader reader = null; try { reader = command.ExecuteReader(); if (reader.Read()) { nameAddressRecord = _ReadRecord(reader); } } finally { if (reader != null) { reader.Close(); } } return(nameAddressRecord); }
/// <summary> /// Fill parameters of SQL command from name\address pair. /// </summary> /// <param name="nameAddress">Source name\address record.</param> /// <param name="command">Command to fill.</param> private void _FillCommandParameters(NameAddress nameAddress, SqlCeCommand command) { _AddParameter(command, NAME_PARAMETER_NAME, nameAddress.Name); Address address = nameAddress.Address; _AddParameter(command, UNIT_PARAMETER_NAME, address.Unit); _AddParameter(command, ADDRESSLINE_PARAMETER_NAME, address.AddressLine); _AddParameter(command, LOCALITY1_PARAMETER_NAME, address.Locality1); _AddParameter(command, LOCALITY2_PARAMETER_NAME, address.Locality2); _AddParameter(command, LOCALITY3_PARAMETER_NAME, address.Locality3); _AddParameter(command, COUNTYPREFECTURE_PARAMETER_NAME, address.CountyPrefecture); _AddParameter(command, POSTALCODE1_PARAMETER_NAME, address.PostalCode1); _AddParameter(command, POSTALCODE2_PARAMETER_NAME, address.PostalCode2); _AddParameter(command, STATEPROVINCE_PARAMETER_NAME, address.StateProvince); _AddParameter(command, COUNTRY_PARAMETER_NAME, address.Country); _AddParameter(command, FULL_ADDRESS_PARAMETER_NAME, address.FullAddress); }
/// <summary> /// Searches in local storage by name/address pair. /// </summary> /// <param name="nameAddress">Name/address pair to search.</param> /// <param name="format">Current Address Format.</param> /// <returns>Name address record.</returns> public NameAddressRecord Search(NameAddress nameAddress, AddressFormat format) { Debug.Assert(nameAddress != null); Debug.Assert(_IsInitialized()); NameAddressRecord recordFromDB = null; try { recordFromDB = _GetRecordFromDB(nameAddress, format); } catch (Exception ex) { Logger.Warning(ex); } return(recordFromDB); }
/// <summary> /// Get address candidate from local storage. /// </summary> /// <param name="nameAddress">Name\Address pair to geocode.</param> /// <returns>Address candidate if exists in local storage. Null otherwise.</returns> public AddressCandidate Geocode(NameAddress nameAddress) { Debug.Assert(nameAddress != null); Debug.Assert(_nameAddressStorage != null); // Search record in storage. NameAddressRecord nameAddressRecord = _nameAddressStorage.Search( nameAddress, App.Current.Geocoder.AddressFormat); AddressCandidate candidateFromLocalStorage = null; // Extract candidate from record, if record exists. if (nameAddressRecord != null) { candidateFromLocalStorage = _ConvertToCandidate(nameAddressRecord); } return(candidateFromLocalStorage); }