public Dictionary <LocationCoordinate, LocationDescription> ReadAllLocationsData()
        {
            Dictionary <LocationCoordinate, LocationDescription> locations = new Dictionary <LocationCoordinate, LocationDescription>();

            Mono.Data.Sqlite.SqliteTransaction sqlTransactionSelect;
            do
            {
                sqlTransactionSelect = dbTools.TransactionBeginSelect();

                #region SELECT Latitude, Longitude, Name, City, Province, Country FROM LocationName
                string sqlCommand = "SELECT Latitude, Longitude, Name, City, Province, Country FROM LocationName";
                using (CommonSqliteCommand commandDatabase = new CommonSqliteCommand(sqlCommand, dbTools.ConnectionDatabase, sqlTransactionSelect))
                {
                    //commandDatabase.Prepare();

                    using (CommonSqliteDataReader reader = commandDatabase.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            LocationCoordinate locationCoordinate = new LocationCoordinate(
                                (float)dbTools.ConvertFromDBValFloat(reader["Latitude"]),
                                (float)dbTools.ConvertFromDBValFloat(reader["Longitude"]));

                            LocationDescription locationDescription = new LocationDescription(
                                dbTools.ConvertFromDBValString(reader["Name"]),
                                dbTools.ConvertFromDBValString(reader["City"]),
                                dbTools.ConvertFromDBValString(reader["Province"]),
                                dbTools.ConvertFromDBValString(reader["Country"]));

                            if (!locations.ContainsKey(locationCoordinate))
                            {
                                locations.Add(locationCoordinate, locationDescription);
                            }
                            LocationCoordinateAndDescriptionUpdate(locationCoordinate, locationCoordinate, locationDescription);
                        }
                    }
                }
                #endregion
            } while (!dbTools.TransactionCommitSelect(sqlTransactionSelect));

            return(locations);
        }
        private void LocationCoordinateAndDescriptionUpdate(LocationCoordinate locationCoordinateSearch, LocationCoordinate locationCoordinateInDatabase, LocationDescription locationDescription)
        {
            lock (locationCoordinateAndDescriptionCacheLock)
            {
                if (!locationCoordinateConvertFromSearchToDatabaseCache.ContainsKey(locationCoordinateSearch))
                {
                    locationCoordinateConvertFromSearchToDatabaseCache.Add(locationCoordinateSearch, locationCoordinateInDatabase);
                }
                else
                {
                    locationCoordinateConvertFromSearchToDatabaseCache[locationCoordinateSearch] = locationCoordinateInDatabase;
                }

                if (!locationCoordinateAndDescriptionInDatbaseCache.ContainsKey(locationCoordinateInDatabase))
                {
                    locationCoordinateAndDescriptionInDatbaseCache.Add(locationCoordinateInDatabase, locationDescription);
                }
                else
                {
                    locationCoordinateAndDescriptionInDatbaseCache[locationCoordinateInDatabase] = locationDescription;
                }
            }
        }
        public LocationCoordinateAndDescription AddressLookupAndReverseGeocoder(
            LocationCoordinate locationCoordinateSearch, float locationAccuracyLatitude, float locationAccuracyLongitude,
            bool onlyFromCache, bool canReverseGeocoder, LocationDescription metadataLocationDescription, bool forceReloadUsingReverseGeocoder)
        {
            if (locationCoordinateSearch == null)
            {
                //DEBUG
                return(null);
            }

            #region Only From Cache - Nothing in cache - ** return null **
            if (onlyFromCache && !LocationCoordinateAndDescriptionExsistInCache(locationCoordinateSearch))
            {
                return(null);
            }
            #endregion

            if (!forceReloadUsingReverseGeocoder)
            {
                #region If exist - ** Return ** location from Cache or Database
                LocationCoordinateAndDescription locationInDatbaseCoordinateAndDescription =
                    ReadLocationNameFromDatabaseOrCache(locationCoordinateSearch, locationAccuracyLatitude, locationAccuracyLongitude);
                if (locationInDatbaseCoordinateAndDescription != null)
                {
                    return(locationInDatbaseCoordinateAndDescription);
                }
                #endregion

                #region If not exist in Database, but Metadata has Location info, save it and ** return it ***
                if (metadataLocationDescription != null)
                {
                    locationInDatbaseCoordinateAndDescription = new LocationCoordinateAndDescription(
                        locationCoordinateSearch, metadataLocationDescription);
                    WriteLocationName(locationCoordinateSearch, locationInDatbaseCoordinateAndDescription);
                    return(locationInDatbaseCoordinateAndDescription);
                }
                #endregion
            }
            else
            {
                #region When: Force to read from ReverseGeocode - Delete old data and continue to ReverseGeocode
                ReadLocationNameFromDatabaseOrCache(locationCoordinateSearch, locationAccuracyLatitude, locationAccuracyLongitude);
                DeleteLocation(locationCoordinateSearch, locationAccuracyLatitude, locationAccuracyLongitude);
                #endregion
            }

            if (canReverseGeocoder)
            {
                #region ReverseGeocoder
                LocationCoordinateAndDescription locationInDatbaseCoordinateAndDescription;
                try
                {
                    var y  = new ReverseGeocoder();
                    var r2 = y.ReverseGeocode(new ReverseGeocodeRequest
                    {
                        Longitude = locationCoordinateSearch.Longitude,
                        Latitude  = locationCoordinateSearch.Latitude,

                        BreakdownAddressElements = true,
                        ShowExtraTags            = true,
                        ShowAlternativeNames     = true,
                        ShowGeoJSON        = true,
                        PreferredLanguages = PreferredLanguagesString
                    });
                    r2.Wait();

                    if (r2.IsCompleted && !r2.IsFaulted && r2.Result != null && r2.Result.Address != null)
                    {
                        locationInDatbaseCoordinateAndDescription = new LocationCoordinateAndDescription();

                        locationInDatbaseCoordinateAndDescription.Coordinate.Latitude  = locationCoordinateSearch.Latitude;
                        locationInDatbaseCoordinateAndDescription.Coordinate.Longitude = locationCoordinateSearch.Longitude;
                        locationInDatbaseCoordinateAndDescription.Description.City     = (r2.Result.Address.City + " " + r2.Result.Address.Town + " " + r2.Result.Address.Village).Trim().Replace("  ", " ");
                        locationInDatbaseCoordinateAndDescription.Description.Country  = r2.Result.Address.Country;
                        locationInDatbaseCoordinateAndDescription.Description.Name     = (r2.Result.Address.Road + " " + r2.Result.Address.HouseNumber).Trim();
                        locationInDatbaseCoordinateAndDescription.Description.Region   = (r2.Result.Address.State + " " + r2.Result.Address.County + " " + r2.Result.Address.Suburb + " " + r2.Result.Address.District + " " + r2.Result.Address.Hamlet).Trim().Replace("  ", " ");

                        locationInDatbaseCoordinateAndDescription.Description.Name    = string.IsNullOrEmpty(locationInDatbaseCoordinateAndDescription.Description.Name) ? null : locationInDatbaseCoordinateAndDescription.Description.Name;
                        locationInDatbaseCoordinateAndDescription.Description.Region  = string.IsNullOrEmpty(locationInDatbaseCoordinateAndDescription.Description.Region) ? null : locationInDatbaseCoordinateAndDescription.Description.Region;
                        locationInDatbaseCoordinateAndDescription.Description.City    = string.IsNullOrEmpty(locationInDatbaseCoordinateAndDescription.Description.City) ? null : locationInDatbaseCoordinateAndDescription.Description.City;
                        locationInDatbaseCoordinateAndDescription.Description.Country = string.IsNullOrEmpty(locationInDatbaseCoordinateAndDescription.Description.Country) ? null : locationInDatbaseCoordinateAndDescription.Description.Country;

                        WriteLocationName(locationCoordinateSearch, locationInDatbaseCoordinateAndDescription);
                        return(locationInDatbaseCoordinateAndDescription);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "AddressLookup");
                }
                #endregion
            }
            return(null);
        }
예제 #4
0
 public LocationDescription(LocationDescription locationDescription) : this(locationDescription.Name, locationDescription.City, locationDescription.Region, locationDescription.Country)
 {
 }