def _get_location_entries(parsed_location): # If there is municipality information, use it as an main entry name village_information = None location_records = [] # Parsed result set may countain municipality and village information. If only one result is in the # result set, interpret it as municipality if 'municipality' in parsed_location: # Try to normalize place names first so that the coordinate fetch from DB might work better entry_name, entry_region = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( parsed_location['municipality'], return_region=True) village_information = self._get_village(parsed_location) else: entry_name, entry_region = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( parsed_location['place'], return_region=True) geocoordinates = get_coordinates_by_name(entry_name) # If region was in db associated to coordinates, override previously set region with it if 'region' in geocoordinates: entry_region = geocoordinates['region'] entry_name = validate_location_name(entry_name, geocoordinates) if 'year_information' in parsed_location: for migration in parsed_location['year_information']: if 'moved_in' in migration: moved_in = text_utils.int_or_none( migration['moved_in']) else: moved_in = None if 'moved_out' in migration: moved_out = text_utils.int_or_none( migration['moved_out']) else: moved_out = None location_records.append( # FIXME: Refactor this to the _postprocess method? place_name_cleaner.clean_place_name( self._get_location_entry(entry_name, entry_region, geocoordinates, village_information, moved_in, moved_out))) else: location_records.append( # FIXME: Refactor this to the _postprocess method? place_name_cleaner.clean_place_name( self._get_location_entry(entry_name, entry_region, geocoordinates, village_information))) return location_records
def _get_village(self, parsed_location): """ Some BNF-parsed location data objects contain information about village in a municipality. If so, record name and coordinates of the said village. :param parsed_location: :return: dict, None """ village_name = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( parsed_location['place'], return_region=False) village_name = validate_village_name(village_name) if village_name: # TODO: There could be a check if the region is correct for possible found place village_coordinates = get_coordinates_by_name(village_name) village_information = { KEYS['otherlocation']: village_name or None, KEYS['othercoordinate']: { KEYS['latitude']: village_coordinates['latitude'], KEYS['longitude']: village_coordinates['longitude'] } } return village_information else: return None
def should_return_fixed_name(self): result = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( 'Rääkkylässä') assert result == 'Rääkkylä'
def should_return_region_as_None_if_it_was_not_found(self): place_name, region = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( 'Testimaa', True) assert place_name == 'Testimaa' assert region is None
def should_return_region_if_it_is_asked(self): place_name, region = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( 'Rääkkylässä', True) assert place_name == 'Rääkkylä' assert region == 'other'
def should_return_place_name_unmodified_if_it_was_not_found_from_list( self): result = place_name_cleaner.try_to_normalize_place_name_with_known_aliases( 'Testimaa') assert result == 'Testimaa'