public void PopulateLocationFromGeolocationCache(List <ChargePoint> itemList, CoreReferenceData coreRefData) { OCM.Import.Analysis.SpatialAnalysis spatialAnalysis = new Analysis.SpatialAnalysis(TempFolder + "\\Shapefiles\\World\\ne_10m_admin_0_map_units.shp"); //process list of locations, populating country refreshing cache where required foreach (var item in itemList) { if (item.AddressInfo.Country == null && item.AddressInfo.CountryID == null) { Country country = null; var test = spatialAnalysis.ClassifyPoint((double)item.AddressInfo.Latitude, (double)item.AddressInfo.Longitude); if (test != null) { country = coreRefData.Countries.FirstOrDefault(c => c.ISOCode == test.CountryCode || c.Title == test.CountryName); } if (country == null) { var geoLookup = geolocationCacheManager.PerformLocationLookup((double)item.AddressInfo.Latitude, (double)item.AddressInfo.Longitude, coreRefData.Countries); if (geoLookup != null) { country = coreRefData.Countries.FirstOrDefault(c => c.ID == geoLookup.CountryID || c.ISOCode == geoLookup.CountryCode || c.Title == geoLookup.CountryName); } } if (country != null) { item.AddressInfo.Country = country; //remove country name from address line 1 if present if (item.AddressInfo.AddressLine1 != null) { if (item.AddressInfo.AddressLine1.ToLower().Contains(country.Title.ToLower())) { item.AddressInfo.AddressLine1 = item.AddressInfo.AddressLine1.Replace(country.Title, "").Trim(); } } } if (item.AddressInfo.Country == null) { LogHelper.Log("Failed to resolve country for item:" + item.AddressInfo.Title); } else { item.AddressInfo.CountryID = item.AddressInfo.Country.ID; } } } //cache may have updates, save for next time geolocationCacheManager.SaveCache(); }
/// <summary> /// For a list of ChargePoint objects, attempt to populate the AddressInfo (at least country) based on lat/lon if not already populated /// </summary> /// <param name="itemList"></param> /// <param name="coreRefData"></param> public void PopulateLocationFromGeolocationCache(List <ChargePoint> itemList, CoreReferenceData coreRefData) { GeolocationCacheManager geolocationCacheManager = new GeolocationCacheManager(TempFolder); geolocationCacheManager.GeonamesAPIUserName = GeonamesAPIUserName; geolocationCacheManager.LoadCache(); //process list of locations, populating country refreshing cache where required foreach (var item in itemList) { if (item.AddressInfo.Country == null) { var geoLookup = geolocationCacheManager.PerformLocationLookup((double)item.AddressInfo.Latitude, (double)item.AddressInfo.Longitude, coreRefData.Countries); if (geoLookup != null) { var country = coreRefData.Countries.FirstOrDefault(c => c.ID == geoLookup.CountryID || c.ISOCode == geoLookup.CountryCode || c.Title == geoLookup.CountryName); if (country != null) { item.AddressInfo.Country = country; //remove country name from address line 1 if present if (item.AddressInfo.AddressLine1.ToLower().Contains(country.Title.ToLower())) { item.AddressInfo.AddressLine1 = item.AddressInfo.AddressLine1.Replace(country.Title, "").Trim(); } } } } if (item.AddressInfo.Country == null) { LogHelper.Log("Failed to resolve country for item:" + item.AddressInfo.Title); } } //cache may have updates, save for next time geolocationCacheManager.SaveCache(); }