def copy_georow_to_place(self, row, place: Loc, fast: bool): """ Copy data from DB row into place instance Country, admin1_id, admin2_id, city, lat/lon, feature, geoid are updated if available #Args: row: georow from geoname database place: Loc instance fast: Currently ignored #Returns: None. Place instance is updated with data from georow """ place.admin1_id = '' place.admin2_id = '' place.admin1_name = '' place.admin2_name = '' place.city = '' place.country_iso = str(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]) place.prefix = row[Entry.PREFIX] place.place_type = Loc.PlaceType.CITY if place.feature == 'ADM0': place.place_type = Loc.PlaceType.COUNTRY pass elif place.feature == 'ADM1': place.admin1_id = row[Entry.ADM1] place.place_type = Loc.PlaceType.ADMIN1 elif place.feature == 'ADM2': place.admin1_id = row[Entry.ADM1] place.admin2_id = row[Entry.ADM2] place.place_type = Loc.PlaceType.ADMIN2 else: place.admin1_id = row[Entry.ADM1] place.admin2_id = row[Entry.ADM2] place.city = row[Entry.NAME] self.s.update_names(place) if place.admin2_name is None: place.admin2_name = '' if place.admin1_name is None: place.admin1_name = '' place.city = str(place.city) if place.city is None: place.city = '' try: place.score = row[Entry.SCORE] except IndexError: pass
def process_results(self, place: Loc, flags) -> None: """ Update fields in place record using first entry in place.georow_list Updates fields with available data: city, admin1, admin2, country, lat/long, feature, etc. #Args: place: Loc instance flags: Flags tuple as returned by sort_results #Returns: None. place instance fields are updated """ # self.logger.debug(f'**PROCESS RESULT: Res={place.result_type} Georow_list={place.georow_list}') if place.result_type == GeoUtil.Result.NOT_SUPPORTED: place.place_type = Loc.PlaceType.COUNTRY if place.result_type in GeoUtil.successful_match and len( place.georow_list) > 0: self.geo_build.geodb.copy_georow_to_place(row=place.georow_list[0], place=place, fast=False) elif len(place.georow_list ) > 0 and place.result_type != GeoUtil.Result.NOT_SUPPORTED: # self.logger.debug(f'***RESULT={place.result_type} Setting to Partial') place.result_type = GeoUtil.Result.PARTIAL_MATCH place.set_place_type_text()
def _find_type_as_city(self, place: Loc, typ) -> int: """ Do a lookup using the field specifed by typ as a city name. E.g. if typ is PlaceType.ADMIN1 then use the place.admin1_name field to do the city lookup #Args: place: Loc instance typ: Loc.PlaceType - Specifies which field to use as target for lookup #Returns: None place.georow_list is updated with matches """ # place.standard_parse = False typ_name = '' best = 999 if typ == Loc.PlaceType.CITY: # Try City as city (do as-is) typ_name = 'City' pass elif typ == Loc.PlaceType.ADMIN2: # Try ADMIN2 as city if place.admin2_name != '': # if '*' not in place.city: # place.prefix += ' ' + place.city place.city = place.admin2_name place.admin2_name = '' typ_name = 'Admin2' elif typ == Loc.PlaceType.PREFIX: # Try Prefix as City if place.prefix != '': place.city = place.prefix # if '*' not in tmp: # place.prefix = tmp typ_name = 'Prefix' elif typ == Loc.PlaceType.ADVANCED_SEARCH: # Advanced Search best = self.geo_build.geodb.lookup_place(place=place) return best else: self.logger.warning(f'Unknown TYPE {typ}') if typ_name != '': result_list = [] self.logger.debug( f'2) Try {typ_name} as City. Target={place.city} pref [{place.prefix}] ' ) place.place_type = Loc.PlaceType.CITY best = self.geo_build.geodb.s.lookup_place(place=place) #best_score = self.geo_build.geodb.assign_scores(result_list, place, '', fast=True, quiet=False) self.logger.debug(f'best={best}') if best >= MatchScore.Score.POOR_CUTOFF: self.logger.debug('--- DEEP SEARCH ADM2 ---') best = self.geo_build.geodb.s.deep_lookup(place=place) return best
def _lookup_city_as_admin2(self, place: Loc, result_list) -> int: """ Lookup place.city as admin2 name #Args: place: result_list: #Returns: """ # Try City as ADMIN2 # place.standard_parse = False place.admin2_name = place.city place.city = '' place.place_type = Loc.PlaceType.ADMIN2 self.logger.debug( f' Try admin2 [{place.admin2_name}] as city [{place.get_five_part_title()}]' ) best = self.geo_build.geodb.lookup_place(place=place) result_list.extend(place.georow_list) return best
def is_country_valid(self, place: Loc) -> bool: """ See if COUNTRY is present and is in the supported country list #Args: place: #Returns: True if country is valid """ if place.country_iso == '': place.result_type = GeoUtil.Result.NO_COUNTRY is_valid = False elif place.country_iso not in self.geo_build.supported_countries_dct: self.logger.debug(f'Country [{place.country_iso}] not supported') place.result_type = GeoUtil.Result.NOT_SUPPORTED place.place_type = Loc.PlaceType.COUNTRY is_valid = False else: is_valid = True return is_valid