}// end of method Run /// <summary> /// Add new location locally if it does not already exists /// </summary> private void StoreNewLocationLocally() { string[] searchCity = city.Split(','); foreach (GeoNamesGeoLocation.GeoNames foundCity in GeoNamesGeoLocation.cityGeographicalData .geonames) { if (foundCity.name.Equals(searchCity[0].Trim().ToLower(), StringComparison.OrdinalIgnoreCase) && foundCity.adminCodes1.iso.Equals(searchCity[1].Trim(), StringComparison.OrdinalIgnoreCase) && !UtilityMethod.IsNumeric(foundCity.adminCodes1.iso) || foundCity.countryName.Equals(searchCity[1].Trim(), StringComparison.OrdinalIgnoreCase)) { string cityName = UtilityMethod.ToProperCase(foundCity.name); string countryName = UtilityMethod.ToProperCase(foundCity.countryName); string countryCode = foundCity.countryCode.ToUpper(); string regionName = UtilityMethod .ToProperCase(foundCity.adminName1); string regionCode = null; regionCode = foundCity.adminCodes1.iso?.ToUpper(); float Latitude = float.Parse(foundCity.lat); float Longitude = float.Parse(foundCity.lng); CityData cityData = new CityData(cityName, countryName, countryCode, regionName, regionCode, Latitude, Longitude); string currentCity = regionCode != null ? $"{cityName}, {regionCode}" : $"{cityName}, {countryName}"; if (!UtilityMethod.IsFoundInDatabase(currentCity)) { UtilityMethod.AddCityToDatabase(cityName, countryName, countryCode, regionName, regionCode, Latitude, Longitude); }// end of if block if (!UtilityMethod.IsFoundInJSONStorage(currentCity)) { JSONHelper.ExportToJSON(cityData); }// end of if block if (!UtilityMethod.IsFoundInXMLStorage(currentCity)) { XMLHelper.ExportToXML(cityData); } // end of if block } // end of if block } // end of for each loop } // end of method StoreNewLocationLocally
} // end of method btnSearch_Click /// <summary> /// Load a list of previous place that were searched for /// </summary> private void LoadKnownPlaces() { List <CityData> previousSearches = JSONHelper.ImportFromJSON(); List <string> searchList = new List <string>(); // when the program is first runs there will be no previous searches so // this function does nothing on the first run if (previousSearches != null) { foreach (CityData city in previousSearches) { if (city.regionCode != null && !UtilityMethod.IsNumeric(city.regionCode)) { searchList.Add($"{city.cityName}, {city.regionCode}"); }// end of if block else { searchList.Add($"{city.cityName}, {city.countryName}"); } // end of else block } // end of for each loop searchList.Sort(); string[] s = searchList.ToArray(); cboLocation.Items.AddRange(s); var source = new AutoCompleteStringCollection(); source.AddRange(s); // Set the locations combo box to auto complete cboLocation.Items.Clear(); cboLocation.AutoCompleteCustomSource = source; cboLocation.AutoCompleteMode = AutoCompleteMode.SuggestAppend; cboLocation.AutoCompleteSource = AutoCompleteSource.CustomSource; // select the first of the previous weather locations if one // was'nt stored in the preferences if (WeatherLionMain.storedPreferences.StoredPreferences.Location == null) { cboLocation.SelectedIndex = 0; }// end of if block else { cboLocation.Text = WeatherLionMain.storedPreferences.StoredPreferences.Location; } // end of else block } // end of if block } // end of method LoadKnownPlaces
}// end of method AccessFormControlsOnOtherThread /// <summary> /// Retrieves data from the Geo Names Web Service /// </summary> private void GetGeoNamesSuggestions() { List <string> matches = new List <string>(); string localCityName = null; string response = null; cityName = null; countryName = null; countryCode = null; regionCode = null; regionName = null; if (strJSON != null) { if (UtilityMethod.IsValidJson(strJSON)) { GeoNamesGeoLocation.cityGeographicalData = JsonConvert.DeserializeObject <GeoNamesGeoLocation>(strJSON); int matchCount = GeoNamesGeoLocation.cityGeographicalData.totalResultsCount; if (matchCount == 1) { GeoNamesGeoLocation.GeoNames place = GeoNamesGeoLocation.cityGeographicalData.geonames[0]; cityName = place.name; countryCode = place.countryCode; countryName = place.countryName; localCityName = place.toponymName; regionCode = place.adminCode1; regionName = place.countryCode.Equals("US") ? UtilityMethod.usStatesByCode[regionCode].ToString() : null; Latitude = place.lat; Longitude = place.lng; if (regionName != null) { response = $"{cityName}, {regionName}, {countryName}"; }// end of if block else { response = $"{cityName}, {countryName}"; } // end of else block } // end of if block else { foreach (GeoNamesGeoLocation.GeoNames place in GeoNamesGeoLocation.cityGeographicalData.geonames) { StringBuilder match = new StringBuilder(); // We only need the cities with the same name if (!place.name.Equals(PreferencesForm.searchCity.ToString(), StringComparison.OrdinalIgnoreCase)) { continue; } match.Append(place.name); // Geo Names may not return adminCodes1 for some results if (place.adminCodes1 != null) { if (place.adminCodes1.iso != null && !UtilityMethod.IsNumeric(place.adminCodes1.iso)) { string region = place.adminCodes1.iso ?? null; match.Append(", " + region); } // end of outer if block } // end of if block match.Append(", " + place.countryName); // Always verify that the adminName1 and countryName does not indicate a city already added if (!matches.ToString().Contains($"{place.adminName1}, {place.countryName}")) { // Redundancy check if (!matches.Contains(match.ToString())) { matches.Add(match.ToString()); } // end of if block } // end of if block } // end of for each loop } // end of else block } // end of if block } // end of if block else { UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, "The web service returned invalid JSON data", "CityDataService::GetGeoNamesSuggestions"); }// end of else block // ensure that a service did not request this data if (frmPreferences != null) { if (matches.Count > 0) { matchList = matches.ToArray(); PreferencesForm.cityNames = matchList; }// end of if block else { matches.Clear(); matchList = new string[] { "No match found..." }; PreferencesForm.cityNames = matchList; }// end of else block // since it is known that invoke will be required, the if statement is not // necessary but, it is good for practice. if (frmPreferences.InvokeRequired) { AccessWidget wid = new AccessWidget(AccessFormControlProperty); frmPreferences.Invoke(wid, new object[] { frmPreferences }); } // end of if block } // end of if block } // end of method GetGeoNamesSuggestions