Esempio n. 1
0
    def lookup_geoid(self, place: Loc) -> None:
        """Search for GEOID"""
        result_place: Loc = Loc.Loc()

        query_list = [
            Query(where="geoid = ? ",
                  args=(place.target, ),
                  result=Result.STRONG_MATCH)
        ]
        place.georow_list, place.result_type = self.db.process_query_list(
            from_tbl='main.geodata', query_list=query_list)
        if len(place.georow_list) == 0:
            place.georow_list, place.result_type = self.db.process_query_list(
                from_tbl='main.admin', query_list=query_list)
        else:
            place.georow_list = place.georow_list[:1]
            place.result_type = GeoKeys.Result.STRONG_MATCH

        # Add search quality score to each entry
        for idx, rw in enumerate(place.georow_list):
            self.copy_georow_to_place(row=rw, place=result_place)
            update = list(rw)
            update.append(1)  # Extend list row and assign score
            result_place.prefix = ''
            res_nm = result_place.format_full_nm(None)
            score = 0.0

            # Remove items in prefix that are in result
            tk_list = res_nm.split(",")
            for item in tk_list:
                place.prefix = re.sub(
                    item.strip(' ').lower(), '', place.prefix)

            update[GeoKeys.Entry.SCORE] = int(score * 100)
            place.georow_list[idx] = tuple(update)
Esempio n. 2
0
    def select_admin1(self, place: Loc):
        """Search for Admin1 entry"""
        lookup_target = place.admin1_name

        #pattern = self.create_wildcard(lookup_target)
        if len(lookup_target) == 0:
            return
        sdx = get_soundex(lookup_target)

        # self.logger.debug(f'sel adm1 patt={pattern} iso={place.country_iso}')

        # Try each query until we find a match - each query gets less exact
        query_list = [
            Query(where="name = ? AND country = ? AND f_code = ? ",
                  args=(lookup_target, place.country_iso, 'ADM1'),
                  result=Result.STRONG_MATCH),
            Query(where="name LIKE ? AND country = ?  AND f_code = ?",
                  args=(lookup_target, place.country_iso, 'ADM1'),
                  result=Result.WILDCARD_MATCH),
            Query(where="sdx = ? AND country = ? AND f_code=?",
                  args=(sdx, place.country_iso, 'ADM1'),
                  result=Result.SOUNDEX_MATCH)
        ]
        place.georow_list, place.result_type = self.db.process_query_list(
            from_tbl='main.admin', query_list=query_list)
Esempio n. 3
0
 def lookup_admin_dbid(self, place: Loc) -> None:
     """Search for DB ID"""
     query_list = [
         Query(where="id = ? ",
               args=(place.target, ),
               result=Result.STRONG_MATCH)
     ]
     place.georow_list, place.result_type = self.db.process_query_list(
         from_tbl='main.admin', query_list=query_list)
Esempio n. 4
0
    def select_country(self, place: Loc):
        """Search for Admin1 entry"""
        lookup_target = place.country_iso
        if len(lookup_target) == 0:
            return
        sdx = get_soundex(lookup_target)

        # Try each query until we find a match - each query gets less exact
        query_list = [
            Query(where="country = ? AND f_code = ? ",
                  args=(place.country_iso, 'ADM0'),
                  result=Result.STRONG_MATCH),
            Query(where="sdx = ?  AND f_code=?",
                  args=(sdx, 'ADM0'),
                  result=Result.SOUNDEX_MATCH)
        ]

        place.georow_list, place.result_type = self.db.process_query_list(
            from_tbl='main.admin', query_list=query_list)
Esempio n. 5
0
    def advanced_search(self, place: Loc):
        """
        Advanced search - support parameters for ISO and Feature class
        """
        lookup_target = place.target
        if len(lookup_target) == 0:
            return
        pattern = self.create_wildcard(lookup_target)
        feature_pattern = self.create_wildcard(place.feature)
        self.logger.debug(
            f'Advanced Search. Targ=[{pattern}] feature=[{feature_pattern}]'
            f'  iso=[{place.country_iso}] ')

        if len(place.feature) > 0:
            query_list = [
                Query(where="name LIKE ? AND country LIKE ? AND f_code LIKE ?",
                      args=(pattern, place.country_iso, feature_pattern),
                      result=Result.PARTIAL_MATCH)
            ]
        else:
            query_list = [
                Query(where="name LIKE ? AND country LIKE ?",
                      args=(pattern, place.country_iso),
                      result=Result.PARTIAL_MATCH)
            ]

        # Search main DB
        place.georow_list, place.result_type = self.db.process_query_list(
            from_tbl='main.geodata', query_list=query_list)

        # self.logger.debug(f'main Result {place.georow_list}')

        # Search admin DB
        admin_list, place.result_type = self.db.process_query_list(
            from_tbl='main.admin', query_list=query_list)
        place.georow_list.extend(admin_list)
Esempio n. 6
0
    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
Esempio n. 7
0
    def select_city(self, place: Loc):
        """
        Search for  entry - try the most exact match first, then less exact matches
        """
        lookup_target = place.target
        if len(lookup_target) == 0:
            return
        #pattern = self.create_wildcard(lookup_target)
        #quick_pattern = self.create_quick_wildcard(lookup_target)

        sdx = get_soundex(lookup_target)
        #self.logger.debug(f'CITY lkp targ=[{lookup_target}] adm1 id=[{place.admin1_id}]'
        #                  f' adm2 id=[{place.admin2_id}] iso=[{place.country_iso}] patt =[{pattern}] sdx={sdx} pref={place.prefix}')

        query_list = []

        if len(place.country_iso) == 0:
            # No country present - try lookup by name.
            query_list.append(
                Query(where="name = ?",
                      args=(lookup_target, ),
                      result=Result.PARTIAL_MATCH))
            # lookup by wildcard name
            query_list.append(
                Query(
                    where="name LIKE ?",
                    args=(lookup_target, ),  # quick_pattern,),
                    result=Result.WILDCARD_MATCH))
            # lookup by soundex
            query_list.append(
                Query(where="sdx = ?",
                      args=(sdx, ),
                      result=Result.SOUNDEX_MATCH))

            place.georow_list, place.result_type = self.db.process_query_list(
                from_tbl='main.geodata', query_list=query_list)
            # self.logger.debug(place.georow_list)
            return

        # Build query list - try each query in order until a match is found
        # Start with the most exact match depending on the data provided.
        if len(place.admin1_name) > 0:
            # lookup by name, ADMIN1, country
            query_list.append(
                Query(where="name = ? AND country = ? AND admin1_id = ?",
                      args=(lookup_target, place.country_iso, place.admin1_id),
                      result=Result.STRONG_MATCH))

            # lookup by wildcard name, ADMIN1, country
            query_list.append(
                Query(where="name LIKE ? AND country = ? AND admin1_id = ?",
                      args=(lookup_target, place.country_iso, place.admin1_id),
                      result=Result.WILDCARD_MATCH))
        else:
            # lookup by wildcard  name, country
            query_list.append(
                Query(where="name LIKE ? AND country = ?",
                      args=(lookup_target, place.country_iso),
                      result=Result.WILDCARD_MATCH))

        # Lookup by name, country
        query_list.append(
            Query(where="name = ? AND country = ?",
                  args=(lookup_target, place.country_iso),
                  result=Result.PARTIAL_MATCH))

        if len(place.admin1_name) > 0:
            # lookup by Soundex name, country and admin1
            query_list.append(
                Query(where="sdx = ? AND admin1_id = ? AND country = ?",
                      args=(sdx, place.admin1_id, place.country_iso),
                      result=Result.SOUNDEX_MATCH))
        else:
            # lookup by Soundex name, country
            query_list.append(
                Query(where="sdx = ? AND country = ?",
                      args=(sdx, place.country_iso),
                      result=Result.SOUNDEX_MATCH))

        # Try each query in list
        place.georow_list, place.result_type = self.db.process_query_list(
            from_tbl='main.geodata', query_list=query_list)