def copy_georow_to_place(self, row, place: Loc): # Copy data from DB row into Place # self.logger.debug(row) place.admin1_id = '' place.admin2_id = '' place.city1 = '' place.country_iso = str(row[Entry.ISO]) place.country_name = str(self.get_country_name(row[Entry.ISO])) place.lat = row[Entry.LAT] place.lon = row[Entry.LON] place.feature = str(row[Entry.FEAT]) place.geoid = str(row[Entry.ID]) if place.feature == 'ADM0': self.place_type = Loc.PlaceType.COUNTRY pass elif place.feature == 'ADM1': place.admin1_id = row[Entry.ADM1] self.place_type = Loc.PlaceType.ADMIN1 elif place.feature == 'ADM2': place.admin1_id = row[Entry.ADM1] place.admin2_id = row[Entry.ADM2] self.place_type = Loc.PlaceType.ADMIN2 else: place.admin1_id = row[Entry.ADM1] place.admin2_id = row[Entry.ADM2] place.city1 = row[Entry.NAME] self.place_type = Loc.PlaceType.CITY place.admin1_name = str(self.get_admin1_name(place)) place.admin2_name = str(self.get_admin2_name(place)) if place.admin2_name is None: place.admin2_name = '' if place.admin1_name is None: place.admin1_name = '' place.city1 = str(place.city1) if place.city1 is None: place.city1 = ''
def select_admin2(self, place: Loc): """Search for Admin2 entry""" lookup_target = place.admin2_name if len(lookup_target) == 0: return place.target = lookup_target # sdx = get_soundex(lookup_target) # Try Admin query until we find a match - each query gets less exact query_list = [ Query( where="name = ? AND country = ? AND admin1_id = ? AND f_code=?", args=(lookup_target, place.country_iso, place.admin1_id, 'ADM2'), result=Result.STRONG_MATCH), Query(where="name = ? AND country = ? AND f_code=?", args=(lookup_target, place.country_iso, 'ADM2'), result=Result.PARTIAL_MATCH), Query(where="name LIKE ? AND country = ? AND f_code=?", args=(self.create_wildcard(lookup_target), place.country_iso, 'ADM2'), result=Result.PARTIAL_MATCH), Query(where="name = ? AND f_code=?", args=(lookup_target, 'ADM2'), result=Result.PARTIAL_MATCH), Query(where="name LIKE ? AND country = ? AND f_code=?", args=(lookup_target, place.country_iso, 'ADM2'), result=Result.WILDCARD_MATCH) ] # self.logger.debug(f'Admin2 lookup=[{lookup_target}] country=[{place.country_iso}]') place.georow_list, place.result_type = self.db.process_query_list( from_tbl='main.admin', query_list=query_list) if place.result_type == GeoKeys.Result.WILDCARD_MATCH: # Found as Admin2 without shire place.original_entry = re.sub('shire', '', place.original_entry) if len(place.georow_list) == 0: # Try city rather than County match. save_admin2 = place.admin2_name place.city1 = place.admin2_name place.admin2_name = '' # self.logger.debug(f'Try admin2 as city: [{place.target}]') self.select_city(place) if len(place.georow_list) == 0: # not found. restore admin place.admin2_name = save_admin2 place.city1 = '' else: # Found match as a City place.place_type = Loc.PlaceType.CITY match_adm1 = self.get_admin1_name_direct( lookup_target=place.georow_list[0][Entry.ADM1], iso=place.country_iso) # self.logger.debug(f'pl_iso [{place.country_iso}] pl_adm1 {place.admin1_name} match_adm1=[{match_adm1}] ') if place.admin1_name != match_adm1: place.prefix = place.admin1_name.title() place.admin1_name = '' return