def get_tts_name(self,
                  iso_country_code,
                  lang_code,
                  name_type,
                  phonetic_strings,
                  phonetic_lang_codes):
     ''''返回音素的MultiLangNameRDF object。'''
     phonetic_list = self.get_phonetic_texts(
                                             iso_country_code,
                                             lang_code,
                                             phonetic_strings,
                                             phonetic_lang_codes
                                             )
     tts_obj_all = None
     for phonetic in phonetic_list:
         phonetic_str = phonetic[0]
         phonetic_lang_code = phonetic[1]
         tts_obj = MultiLangNameRDF(phonetic_lang_code,
                                    phonetic_str,
                                    name_type,
                                    'phoneme')
         if tts_obj_all:
             tts_obj_all.add_tts(tts_obj)
         else:
             tts_obj_all = tts_obj
     return tts_obj_all
Example #2
0
    def get_tts_name(self, iso_country_code, lang_code, name_type,
                     phonetic_strings, phonetic_lang_codes):
        ''''返回音素的MultiLangNameRDF object。'''
        phonetic_list = self.get_phonetic_texts(iso_country_code, lang_code,
                                                phonetic_strings,
                                                phonetic_lang_codes)
        tts_obj = None
        language_code_list = []
        for phonetic_temp in phonetic_list:
            if phonetic_temp[1] not in language_code_list:
                language_code_list.append(phonetic_temp[1])
                phonetic_str = phonetic_temp[0]
                phonetic_lang_code = phonetic_temp[1]
                tts_temp = MultiLangNameRDF(phonetic_lang_code, phonetic_str,
                                            name_type, 'phoneme')
                if tts_obj:
                    tts_obj.add_tts(tts_temp)
                else:
                    tts_obj = tts_temp
        return tts_obj

        #        if phonetic_list:
        #            # [(phonetic_str, phonetic_lang_code)]
        ##             if len(phonetic_list) > 1:
        ##                 self.log.warning("phonetic_list>1, road_name_id=%d"
        ##                                   % road_name_id)
        #            phonetic_str = phonetic_list[0][0]
        #            phonetic_lang_code = phonetic_list[0][1]
        #            tts_obj = MultiLangNameRDF(phonetic_lang_code,
        #                                       phonetic_str,
        #                                       name_type,
        #                                       'phoneme')
        #            return tts_obj
        return None
    def set_trans_name(self,
                       multi_name,
                       iso_country_code,
                       trans_names,
                       luaguage_codes,
                       phonetic_strings,
                       phonetic_lang_codes,
                       split_char='|'):
        if not trans_names:
            return multi_name
        if type(trans_names) is not list:
            if split_char:
                trans_names = trans_names.split(split_char)
                luaguage_codes = luaguage_codes.split(split_char)
            else:  # 当split_char为空or空格:不做切割
                trans_names = [trans_names]
                luaguage_codes = [luaguage_codes]

        name_num = len(trans_names)
        name_cnt = 0
        while name_cnt < name_num:
            name = trans_names[name_cnt]
            if not name:
                name_cnt += 1
                continue
            lang_code = luaguage_codes[name_cnt]
            trans_obj = MultiLangNameRDF(lang_code,
                                         name,
                                         multi_name.get_name_type(),
                                         'not_tts'
                                         )
            if phonetic_strings:
                tts_obj = self.get_tts_name(iso_country_code,
                                            lang_code,
                                            trans_obj.get_name_type(),
                                            phonetic_strings,
                                            phonetic_lang_codes
                                            )
                if tts_obj:
                    trans_obj.set_tts(tts_obj)
                else:
                    if(len(phonetic_strings) > 1 and
                       len(set(phonetic_lang_codes)) > 1):  # 所有种别是相同
                        self.log.warning('No TTS, name=%s' % name)
                        pass
            multi_name.add_trans(trans_obj)
            name_cnt += 1
        return multi_name
 def _get_json_name(self, lang_codes, poi_names):
     name_list = poi_names.split('|')
     lang_list = lang_codes.split('|')
     # 以下!处理lang_code转换
     list_lan = list()
     for lan in lang_list:
         list_lan.append(LANG_CODE_DICT[lan])
     official_name = name_list[0]
     lang_code = list_lan[0]
     trans_langs = list_lan[1:]
     trans_names = name_list[1:]
     multi_name_obj = MultiLangNameRDF(lang_code, official_name)
     multi_name_obj = self.set_trans_name(multi_name_obj, None, trans_names,
                                          trans_langs, None, None)
     json_name = multi_name_obj.json_format_dump()
     return json_name
Example #5
0
 def get_tts_name(self, iso_country_code, lang_code, name_type,
                  phonetic_strings, phonetic_lang_codes):
     ''''返回音素的MultiLangNameRDF object。'''
     phonetic_list = self.get_phonetic_texts(iso_country_code, lang_code,
                                             phonetic_strings,
                                             phonetic_lang_codes)
     if phonetic_list:
         # [(phonetic_str, phonetic_lang_code)]
         #             if len(phonetic_list) > 1:
         #                 self.log.warning("phonetic_list>1, road_name_id=%d"
         #                                   % road_name_id)
         phonetic_str = phonetic_list[0][0]
         phonetic_lang_code = phonetic_list[0][1]
         tts_obj = MultiLangNameRDF(phonetic_lang_code, phonetic_str,
                                    name_type, 'phoneme')
         return tts_obj
     return None
Example #6
0
    def _make_hwy_path_name(self):
        ''' make hwy path name'''
        self.log.info('start make hwy path name')
        temp_file_obj = cache_file.open('path_name')
        multi_name_obj = None
        for row in self._get_hwy_path_name():
            path_id = row[0]
            lang_list = row[1]
            path_name_list = row[2]
            one_name_list = zip(lang_list, path_name_list)
            for index in xrange(0, len(one_name_list)):
                (org_lang, name) = one_name_list[index]
                lang = LANG_CODE_DICT.get(org_lang)
                if not lang:
                    self.log.error('Unknown language_code=%s' % org_lang)
                    continue
                if index == 0:
                    if lang not in ('CHI', 'CHT'):
                        self.log.error('no CHI or CHT name')
                    multi_name_obj = MultiLangNameRDF(lang, name)
                else:
                    multi_name_obj = self.set_trans_name(
                        multi_name_obj,
                        None,
                        name,
                        lang,
                        None,  # phonetic
                        None  # lang_code
                    )
            if multi_name_obj:
                json_name = multi_name_obj.json_format_dump()
                if not json_name:
                    self.log.error('no json name')
                self._store_name_to_temp_file(temp_file_obj, path_id,
                                              json_name)
        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_path_name_ni')
        self.pg.commit2()

        cache_file.close(temp_file_obj, True)
        self.log.info('end make hwy path name')
        return 0
Example #7
0
 def _get_ic_list(self, road_code, updown, node_list, cycle_flag):
     G = self.data_mng.get_graph()
     ic_group = []  # 一个点上的所有设施做为一个元素
     for node_idx in range(0, len(node_list)):
         node_id = node_list[node_idx]
         if node_idx == 0 or node_idx == len(node_list) - 1:
             # 起末点,不做边界点
             boundary_flag = False
         else:
             # 判断第十层Tile边界
             boundary_flag = self.is_hwy_tile_boundary(node_id, road_code)
         if boundary_flag:
             ml_object = MultiLangNameRDF('ENG', HWY_TILE_BOUNDARY_NAME)
             bd_name = ml_object.json_format_dump()
             boundary_facil_info = HwyFacilInfo(road_code,
                                                HWY_INVALID_FACIL_ID_17CY,
                                                IC_TYPE_INVALID, updown,
                                                node_id, INOUT_TYPE_NONE,
                                                bd_name)
             boundary_facil_info.boundary_flag = boundary_flag
         toll_flag = G.is_tollgate(node_id)
         if G.is_hwy_node(node_id) or toll_flag:
             # get facilities of this node
             facil_infos = self.data_mng.get_hwy_facils_by_nodeid(
                 node_id, road_code)
             if boundary_flag:
                 facil_infos.append(boundary_facil_info)
             # 对当前点的多个设施进行排序
             facil_infos = self._sort_facils(facil_infos)
             if facil_infos:
                 ic_group.append(facil_infos)
         elif boundary_flag:  # 边界
             ic_group.append([boundary_facil_info])
     ic_list = []
     # ## 重排头尾设施
     # 第一个设施(同个点上可能有多个)
     ic_list += self._sort_start_facils(ic_group[0], cycle_flag)
     # 最后一个设施(同个点上可能有多个)
     ic_group[-1] = self._sort_end_facils(ic_group[-1], cycle_flag)
     for ic_group_idx in range(1, len(ic_group) - 1):
         ic_list += ic_group[ic_group_idx]
     ic_list += ic_group[-1]
     return ic_list
    def _make_exit_sapa_name(self):
        ''' '''
        self.log.info('start make exit sapa name...')
        temp_file_obj = cache_file.open('temp_exit_sapa_name')
        for nameinfo in self._get_exit_sapa_name():
            node_id = nameinfo[0]
            poi_id = nameinfo[1]
            poi_name = nameinfo[2]
            lang = nameinfo[3]

            name_list = poi_name.split('|')
            lang_list = lang.split('|')
            # 以下!处理lang_code转换
            list_lan = list()
            for lan in lang_list:
                list_lan.append(LANG_CODE_DICT[lan])
            official_name = name_list[0]
            lang_code = list_lan[0]
            trans_langs = list_lan[1:]
            trans_names = name_list[1:]

            if not node_id:
                self.log.error('no node id')
                continue

            multi_name_obj = MultiLangNameRDF(lang_code, official_name)
            multi_name_obj = self.set_trans_name(multi_name_obj, None,
                                                 trans_names, trans_langs,
                                                 None, None)
            json_name = multi_name_obj.json_format_dump()
            if not json_name:
                self.log.error('no json name. node id is %s' % node_id)

            self._store_name_to_temp_file(temp_file_obj, node_id, poi_id,
                                          json_name)

        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_exit_poi_name')
        self.pg.commit2()
        cache_file.close(temp_file_obj, True)

        self.log.info('end make exit sapa name...')
        return 0
Example #9
0
 def _make_road_code(self):
     '''make road_code_info '''
     self.log.info('start make road code info')
     self.CreateTable2('road_code_info')
     sqlcmd = '''
     SELECT distinct pathid, pathname
       FROM org_highwaypath
       ORDER BY pathid
     '''
     for row in self.get_batch_data(sqlcmd):
         path_id = row[0]
         path_name = row[1]
         multi_name = None
         json_name = None
         if path_name:
             multi_name = MultiLangNameRDF('CHT', path_name)
             json_name = multi_name.json_format_dump()
         if not json_name:
             json_name = None
         self._insert_road_info((path_id, json_name))
     self.pg.commit2()
     self.log.info('end make road code info')
     return 0
Example #10
0
    def _make_facil_name(self):
        '''制作设施出口名称'''
        self.log.info('Start Make Facil Name.')
        self.pg.connect1()
        self.CreateTable2('mid_temp_hwy_facil_name')
        sqlcmd = '''
            SELECT distinct a.road_code, a.updown_c, a.road_seq, pntname
            FROM
            (
             select distinct road_code, updown_c, road_seq, node_id, inout_c
             from mid_temp_hwy_ic_path
            ) as a
            LEFT JOIN org_highwaypoint as b
            ON a.road_code = b.pathid and a.road_seq = b.pntid
            ORDER BY road_code, updown_c, road_seq
        '''
        for row in self.get_batch_data(sqlcmd):
            (r_code, updown, r_seq, facil_name) = row
            multi_name = None
            json_name = None
            if facil_name:
                temp_multi_name = MultiLangNameRDF('CHT', facil_name)
                if multi_name:
                    multi_name.add_alter(temp_multi_name)
                else:
                    multi_name = temp_multi_name
            if multi_name:
                json_name = multi_name.json_format_dump()
            if not json_name:
                json_name = None
            self._insert_facil_name(r_code, updown, r_seq, json_name)
        self.pg.execute1(sqlcmd)
        self.pg.commit1()

        self.log.info('End Make Facility Name.')
        return 0
    def _make_signpost_exit_no(self):
        self.CreateTable2('mid_temp_signpost_exit_no')
        temp_file_obj = cache_file.open('signpost_exit_no')  # 创建临时文件
        sqlcmd = """
        SELECT dest.sign_id, dest.destination_number,
               exit_number, language_code,
               alt_exit_number,
               trans_exit_langs,
               trans_exit_nums,
               iso_country_code,
               phonetic_strings,
               phonetic_language_codes
          FROM rdf_sign_destination as dest
          left join mid_temp_sign_destination_trans_group as trans_group
          ON dest.sign_id = trans_group.sign_id
             and dest.destination_number = trans_group.destination_number
          LEFT JOIN temp_rdf_nav_link
          on dest_link_id = link_id
          LEFT JOIN mid_temp_sign_destination_phonetic AS phonetic
          ON dest.sign_id = phonetic.sign_id AND
             dest.destination_number = phonetic.destination_number
          WHERE exit_number is not null
          ORDER BY dest.sign_id, dest.destination_number;
        """
        dict_rdf = comp_dictionary_rdf()
        exits = self.get_batch_data(sqlcmd)
        for exit_info in exits:
            sign_id = exit_info[0]
            dest_number = exit_info[1]
            exit_number = exit_info[2]
            lang_code = exit_info[3]
            alt_exit_number = exit_info[4]
            trans_langs = exit_info[5]  # 翻译语言
            trans_exit_nums = exit_info[6]  # 翻译文本
            iso_country_code = exit_info[7]
            phonetic_strings = exit_info[8]
            phonetic_lang_codes = exit_info[9]
            sign_post_exit = SignPostExit(sign_id, dest_number)
            name_type = 'office_name'
            if exit_number:
                exit_no = MultiLangNameRDF(lang_code, exit_number, name_type)
                if phonetic_strings:  # 有音素
                    tts_obj = dict_rdf.get_tts_name(iso_country_code,
                                                lang_code,
                                                exit_no.get_name_type(),
                                                phonetic_strings,
                                                phonetic_lang_codes
                                                )
                    if tts_obj:
                        exit_no.set_tts(tts_obj)
                    else:
                        self.log.warning('No TTS. sign_id=%d, dest_number=%d' %
                                         (sign_id, dest_number))
                exit_no = dict_rdf.set_trans_name(exit_no,
                                                  iso_country_code,
                                                  trans_exit_nums,
                                                  trans_langs,
                                                  phonetic_strings,
                                                  phonetic_lang_codes,
                                                  )
                # exit_no.set_trans(trans_exit_nums, trans_langs)
                sign_post_exit.set_exit_no(exit_no)
            else:
                continue
            if alt_exit_number:
                name_type = 'alter_name'
                alt_exit_no = MultiLangNameRDF(lang_code,
                                               alt_exit_number,
                                               name_type)

                sign_post_exit.set_alter_exit_no(alt_exit_no)
            str_info = sign_post_exit.to_string()
            self._store_name_to_temp_file(temp_file_obj, str_info)

        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_signpost_exit_no')
        self.pg.commit2()
        # close file
        #temp_file_obj.close()
        cache_file.close(temp_file_obj,True)
        return 0
    def _make_signpost_element(self):
        self.CreateTable2('mid_temp_signpost_element')
        temp_file_obj = cache_file.open('signpost_element')  # 创建临时文件
        # 排序:sign_id, destination_number(方向ID), entry_number
        # 同个方向上的名称做group (sign_id, destination_number)
        sqlcmd = """
            SELECT element.sign_id,
                   element.destination_number,
                   entry_number,
                   text_type,
                   element."text" as official_name,
                   element.language_code,
                   direction_code,
                   entry_type,
                   text_number,
                   trans_types,
                   trans_group.trans_names as trans_names,
                   iso_country_code,
                   phonetic_strings,
                   phonetic_language_codes
              FROM rdf_sign_element as element
              LEFT JOIN mid_temp_sign_element_trans_group as trans_group
              ON element.sign_element_id = trans_group.sign_element_id
              LEFT JOIN rdf_sign_destination AS dest
              ON element.sign_id = dest.sign_id and
                  element.destination_number = dest.destination_number
              LEFT JOIN temp_rdf_nav_link
              on dest_link_id = link_id
              LEFT JOIN mid_temp_sign_element_phonetic as phonetic
              ON element.sign_element_id = phonetic.sign_element_id
              order by sign_id, destination_number, entry_number
              ;
        """
        signs = self.get_batch_data(sqlcmd)
        from component.rdf.dictionary_rdf import INVALID_SHIELD_ID
        prev_sign_id = None
        curr_sign_id = None
        prev_dest_num = None
        curr_dest_num = None
        signpost = None
        signpost_name = None
        dict_rdf = comp_dictionary_rdf()
        for sign_info in signs:
            curr_sign_id = sign_info[0]
            curr_dest_num = sign_info[1]  # 方向ID
            # entry_number = sign_info[2]  # 同个方向上的名称序号
            text_type = sign_info[3]  # T: 名称, R: 番号
            official_name = sign_info[4]  # 官方语言下的文本
            lang_code = sign_info[5]  # 官方语言种别——ISO Country Code
            # dir_code = sign_info[6]  # 方向, E, S, W, N, '-'
            trans_types = sign_info[9]  # 翻译语言种别
            trans_names = sign_info[10]  # 翻译文本
            iso_country_code = sign_info[11]
            phonetic_strings = sign_info[12]
            phonetic_lang_codes = sign_info[13]
            if not curr_sign_id:
                self.log.error('None sign_id.')
                continue
            if not curr_dest_num:
                self.log.error('None destination_number. sign_id=%d'
                               % curr_sign_id)
                continue
            if (prev_sign_id != curr_sign_id or  # 不同的看板
                prev_dest_num != curr_dest_num):  # 不同的方向
                # 保存上一条
                if prev_dest_num:
                    signpost.set_signpost_name(signpost_name)
                    str_info = signpost.to_string()
                    self._store_name_to_temp_file(temp_file_obj, str_info)
                # 新的一条
                prev_sign_id = curr_sign_id
                prev_dest_num = curr_dest_num
                signpost = SignPostElement(curr_sign_id, curr_dest_num)
                signpost_name = None

            # 名称不存在,或为空
            if not official_name:
                self.log.error('None Name. sign_id=%d.' % prev_sign_id)
                continue
            # ## 番号
            if self._is_route_number(text_type):
                if iso_country_code == 'SGP':
                    shield_id = 4111
                else:
                    shield_id = INVALID_SHIELD_ID
                shield_number = official_name
                shield_obj = MultiLangShieldRDF(shield_id,
                                                shield_number,
                                                lang_code
                                                )
                shield_obj.set_trans(trans_names, trans_types)
                # ## shield不做TTS
#                 shield_obj = dict_rdf.set_trans_name(shield_obj,
#                                                      iso_country_code,
#                                                      trans_names,
#                                                      trans_types,
#                                                      phonetic_strings,
#                                                      phonetic_lang_codes,
#                                                     )
                signpost.add_route_no(shield_obj)
            else:  # 名称
                name_type = 'office_name'
                if signpost_name:  # 同个方向上的其他名称
                    trans_name_obj = MultiLangNameRDF(lang_code,
                                                      official_name,
                                                      name_type)
                    if phonetic_strings:  # 有音素
                        tts_obj = dict_rdf.get_tts_name(iso_country_code,
                                                        lang_code,
                                                        name_type,
                                                        phonetic_strings,
                                                        phonetic_lang_codes
                                                        )
                        if tts_obj:
                            trans_name_obj.set_tts(tts_obj)
                        else:
                            self.log.warning('No TTS.sign_id=%d,dest_number=%d'
                                              % (curr_sign_id, curr_dest_num))
                    trans_name_obj = dict_rdf.set_trans_name(trans_name_obj,
                                                       iso_country_code,
                                                       trans_names,
                                                       trans_types,
                                                       phonetic_strings,
                                                       phonetic_lang_codes,
                                                       )
                    signpost_name.add_alter(trans_name_obj)
                else:  # 当前方向上的第一名称
                    signpost_name = MultiLangNameRDF(lang_code,
                                                     official_name,
                                                     name_type)
                    if phonetic_strings:  # 有音素
                        name_type = signpost_name.get_name_type()
                        tts_obj = dict_rdf.get_tts_name(iso_country_code,
                                                        lang_code,
                                                        name_type,
                                                        phonetic_strings,
                                                        phonetic_lang_codes
                                                        )
                        if tts_obj:
                            signpost_name.set_tts(tts_obj)
                        else:
                            self.log.warning('No TTS.sign_id=%d,dest_number=%d'
                                              % (curr_sign_id, curr_dest_num))

                    signpost_name = dict_rdf.set_trans_name(signpost_name,
                                                            iso_country_code,
                                                            trans_names,
                                                            trans_types,
                                                            phonetic_strings,
                                                            phonetic_lang_codes
                                                            )
        # 最后一条
        signpost.set_signpost_name(signpost_name)
        str_info = signpost.to_string()
        self._store_name_to_temp_file(temp_file_obj, str_info)

        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_signpost_element')
        self.pg.commit2()
        # close file
        #temp_file_obj.close()
        cache_file.close(temp_file_obj,True)
        return 0
Example #13
0
    def _make_feature_name(self):
        self.log.info('Make Feature Name.')
        self.CreateTable2('mid_temp_feature_name')
        temp_file_obj = cache_file.open('feature_name')  # 创建临时文件
        sqlcmd = """
        SELECT feature_id, fns.name_id, language_code, name,
               trans_type_array, trans_name_array,
               iso_country_code,
               phonetic_strings,
               phonetic_language_codes
          FROM rdf_admin_hierarchy AS adm
          LEFT JOIN rdf_feature_names AS fns
          ON adm.admin_place_id = fns.feature_id
          LEFT JOIN rdf_feature_name as fn
          ON fns.name_id = fn.name_id
          LEFT JOIN (
              SELECT name_id,
                     array_agg(trans_type) as trans_type_array,
                     array_agg(trans_name) as trans_name_array
              FROM (
                -- Transliteration Name
                SELECT name_id,
                      transliteration_type AS trans_type,
                      "name" as trans_name
                  FROM rdf_feature_name_trans
                  order by name_id, transliteration_type
              ) AS A
              GROUP BY name_id
          ) as trans
          ON fn.name_id = trans.name_id
          LEFT JOIN mid_temp_feature_phontetic as tts
          ON tts.name_id = fns.name_id
          ORDER BY feature_id, is_exonym, fns.name_id;
        """
        features = self.get_batch_data(sqlcmd)
        curr_feature_id = None
        prev_feature_id = None
        feature_name = None
        for feature in features:
            curr_feature_id = feature[0]
            name_id = feature[1]
            lang_code = feature[2]
            name = feature[3]
            trans_types = feature[4]  # 翻译语言种别
            trans_names = feature[5]  # 翻译名称
            iso_country_code = feature[6]
            phonetic_strings = feature[7]
            phonetic_lang_codes = feature[8]
            if not curr_feature_id:
                self.log.error('No feature_id.')
                continue
            if not name_id:
                self.log.error('No name_id. feature_id=' % curr_feature_id)
                continue
            if prev_feature_id != curr_feature_id:  # feature变更
                if prev_feature_id:
                    json_name = feature_name.json_format_dump()
                    # 存到本地的临时文件
                    self._store_name_to_temp_file(temp_file_obj,
                                                  prev_feature_id, json_name)
                # 初始化
                prev_feature_id = curr_feature_id
                feature_name = MultiLangNameRDF(lang_code, name)
                if phonetic_strings:  # 有音素
                    tts_obj = self.get_tts_name(iso_country_code, lang_code,
                                                feature_name.get_name_type(),
                                                phonetic_strings,
                                                phonetic_lang_codes)
                    if tts_obj:
                        feature_name.set_tts(tts_obj)
                    else:
                        self.log.warning('No TTS. feature_id=%d, name_id=%d' %
                                         (prev_feature_id, name_id))
                feature_name = self.set_trans_name(feature_name,
                                                   iso_country_code,
                                                   trans_names, trans_types,
                                                   phonetic_strings,
                                                   phonetic_lang_codes)
            else:  # feature没有变更
                alter_name = MultiLangNameRDF(lang_code, name)
                if phonetic_strings:  # 有音素
                    tts_obj = self.get_tts_name(iso_country_code, lang_code,
                                                feature_name.get_name_type(),
                                                phonetic_strings,
                                                phonetic_lang_codes)
                    if tts_obj:
                        alter_name.set_tts(tts_obj)
                    else:
                        self.log.warning('No TTS. feature_id=%d, name_id=%d' %
                                         (prev_feature_id, name_id))
                alter_name = self.set_trans_name(alter_name, iso_country_code,
                                                 trans_names, trans_types,
                                                 phonetic_strings,
                                                 phonetic_lang_codes)
                # feature的别名,暂时做成翻译(即多语言,而不是多名称)
                feature_name.add_trans(alter_name)
        # 最后一条
        if feature_name:
            json_name = feature_name.json_format_dump()
            # 存到本地的临时文件
            self._store_name_to_temp_file(temp_file_obj, prev_feature_id,
                                          json_name)

        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_feature_name')
        self.pg.commit2()
        # close file
        #temp_file_obj.close()
        cache_file.close(temp_file_obj, True)
        self.log.info('End Make Link Name.')
        return 0
    def _make_hwy_exit_poi_name(self):
        '''
        '''
        self.log.info('start make mid_temp_hwy_exit_poi_name')
        sqlcmd = '''
        SELECT distinct node_id, poi.poi_id,
                rdf_poi_name.language_code,
                rdf_poi_name.name,
                trans_group.trans_type,
                trans_group.trans_name
         FROM mid_temp_hwy_exit_poi as poi
         LEFT JOIN rdf_poi_names
         ON poi.poi_id = rdf_poi_names.poi_id
         LEFT JOIN rdf_poi_name
         ON rdf_poi_names.name_id = rdf_poi_name.name_id
         LEFT JOIN mid_temp_hwy_exit_poi_name_trans_group AS trans_group
         ON rdf_poi_names.name_id = trans_group.name_id
         LEFT JOIN rdf_poi_address
         ON poi.poi_id = rdf_poi_address.poi_id
         LEFT JOIN wkt_location
         ON rdf_poi_address.location_id = wkt_location.location_id
         LEFT JOIN node_tbl
         ON  ST_DWithin(node_tbl.the_geom, poi.the_geom,
                        0.000002694945852358566745)
         ORDER BY node_id, poi.poi_id, language_code
        '''
        temp_file_obj = cache_file.open('hwy_exit_poi_name')
        names = self.get_batch_data(sqlcmd)
        multi_name = None
        pre_node_id = None
        curr_node_id = None
        for nameinfo in names:
            curr_node_id = nameinfo[0]
            poi_id = nameinfo[1]
            lang_code = nameinfo[2]
            official_name = nameinfo[3]
            trans_type = nameinfo[4]
            trans_name = nameinfo[5]
            if not curr_node_id:  # POI id不存在
                self.log.error("No node ID.")
                continue
            if pre_node_id != curr_node_id:
                if pre_node_id:
                    json_name = multi_name.json_format_dump()
                    if not json_name:
                        self.log.error('Json Name is none. node_id=%d' %
                                       curr_node_id)

                    self._store_name_to_temp_file(temp_file_obj, pre_node_id,
                                                  poi_id, json_name)
                # 初始化
                multi_name = MultiLangNameRDF(lang_code, official_name)
                multi_name = self.set_trans_name(multi_name, None, trans_name,
                                                 trans_type, None, None)
                pre_node_id = curr_node_id

            else:
                alter_name = MultiLangNameRDF(lang_code, official_name)
                alter_name = self.set_trans_name(alter_name, None, trans_name,
                                                 trans_type, None, None)
                multi_name.add_alter(alter_name)

        # ## 最后一个node的名称
        if multi_name:
            json_name = multi_name.json_format_dump()
            if not json_name:
                self.log.error('Json Name is none. nodeid=%s, poi_id=%s' %
                               (curr_node_id, poi_id))
            self._store_name_to_temp_file(temp_file_obj, pre_node_id, poi_id,
                                          json_name)
        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_exit_poi_name')
        self.pg.commit2()
        cache_file.close(temp_file_obj, True)
        self.log.info('end make mid_temp_hwy_exit_poi_name')
        return 0
    def _make_tollgate_name(self):
        '''高速收费站名称'''
        self.log.info('Start make hwy toll name')
        self.CreateTable2('hwy_tollgate_name')
        temp_file_obj = cache_file.open('temp_toll_facil_name')
        multi_name_obj = None
        alter_name_obj = None
        for one_junction_name in self._get_tollgate_names():
            (node_id, seq_nm_list, language_list,
             names_list) = one_junction_name
            name_list = zip(seq_nm_list, language_list, names_list)
            name_list.sort(cmp=lambda x, y: cmp(x[0], y[0]), reverse=False)
            for name_info in name_list:
                seq_nm = name_info[0]
                language = name_info[1].split('|')
                name = name_info[2].split('|')
                one_names = zip(language, name)
                one_names.sort(cmp=lambda x, y: cmp(x[0], y[0]), reverse=False)
                if int(seq_nm) == 1:
                    for index in xrange(0, len(one_names)):
                        (org_lang, name) = one_names[index]
                        lang = LANG_CODE_DICT.get(int(org_lang))
                        if not lang:
                            self.log.error('Unknown language code.')
                            continue
                        if index == 0:
                            if lang not in ('CHI', 'CHT'):
                                self.log.error('no CHI or CHT name')
                                break
                            multi_name_obj = MultiLangNameRDF(lang, name)
                        else:
                            multi_name_obj = self.set_trans_name(
                                multi_name_obj,
                                None,
                                name,
                                lang,
                                None,  # phone
                                None  # lang
                            )
                else:
                    for index in xrange(0, len(one_names)):
                        (org_lang, name) = one_names[index]
                        lang = LANG_CODE_DICT.get(int(org_lang))
                        if not lang:
                            self.log.error('Unknown language_code=%s.' %
                                           org_lang)
                            continue
                        if index == 0:
                            if lang not in ('CHI', 'CHT'):
                                self.log.error('no CHI or CHT name')
                                break
                            alter_name_obj = MultiLangNameRDF(lang, name)
                        else:
                            alter_name_obj = self.set_trans_name(
                                alter_name_obj,
                                None,
                                name,
                                lang,
                                None,  # phonetic
                                None  # lang_code
                            )
                    multi_name_obj.add_alter(alter_name_obj)
            if multi_name_obj:
                json_name = multi_name_obj.json_format_dump()
                if not json_name:
                    self.log.error('no json name o.O')
                # 存到本地的临时文件
                self._store_name_to_temp_file(temp_file_obj, node_id,
                                              json_name)
        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'hwy_tollgate_name')
        self.pg.commit2()

        cache_file.close(temp_file_obj, True)
        self.log.info('end make hwy toll name')
Example #16
0
    def _make_hwy_sapa_name(self):
        temp_file_obj = cache_file.open('sapa_name')  # 创建临时文件
        curr_poi_id = None
        prev_poi_id = None
        multi_name_obj = None
        for nameinfo in self._get_sapa_names():
            curr_poi_id = nameinfo[0]
            lang_code = nameinfo[2]
            official_name = nameinfo[3]
            trans_langs = nameinfo[4]
            trans_names = nameinfo[5]

            if not curr_poi_id:  # POI id不存在
                self.log.error("No poi ID.")
                continue
            if prev_poi_id != curr_poi_id:  # POI id 变更
                if prev_poi_id:
                    # ## 保存上一POI的名称
                    json_name = multi_name_obj.json_format_dump()
                    if not json_name:
                        self.log.error('Json Name is none. poi_id=%d' %
                                       prev_poi_id)
                    # 存到本地的临时文件
                    self._store_name_to_temp_file(temp_file_obj, prev_poi_id,
                                                  json_name)
                # ## 初始化
                multi_name_obj = MultiLangNameRDF(lang_code, official_name)

                multi_name_obj = self.set_trans_name(
                    multi_name_obj,
                    None,
                    trans_names,
                    trans_langs,
                    None,  # phonetic_strings
                    None)
                prev_poi_id = curr_poi_id
            else:  # POI_id 没有变化
                alter_name = MultiLangNameRDF(lang_code, official_name)

                alter_name = self.set_trans_name(alter_name, None, trans_names,
                                                 trans_langs, None, None)
                multi_name_obj.add_alter(alter_name)

        # ## 最后一条link的名称
        if multi_name_obj:
            json_name = multi_name_obj.json_format_dump()
            if not json_name:
                self.log.error('Json Name is none. pio_id=%s' % prev_poi_id)
            # 存到本地的临时文件
            self._store_name_to_temp_file(temp_file_obj, prev_poi_id,
                                          json_name)
        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_sapa_name')
        self.pg.commit2()
        # close file
        # temp_file_obj.close()
        cache_file.close(temp_file_obj, True)
        self.CreateIndex2('temp_link_name_link_id_idx')
        self.log.info('End Make Link Name.')
        return 0
Example #17
0
    def _make_link_name(self):
        "道路名称(包括道路番号)字典"
        self.log.info('Make Link Name.')
        sqlcmd = """
        SELECT gid, name.link_id, language_code, street_name,
               trans_types, trans_names,
               route_type, is_name_on_roadsign,
               name.road_name_id,
               iso_country_code,
               phonetic_strings,
               phonetic_language_codes
          FROM mid_temp_road_name as name
          LEFT JOIN mid_temp_road_name_trans_group as trans
          ON name.road_name_id = trans.road_name_id
          LEFT JOIN temp_rdf_nav_link
          ON name.link_id = temp_rdf_nav_link.link_id
          LEFT JOIN mid_temp_road_phonetic as tts
          ON name.road_name_id = tts.road_name_id
          order by link_id, gid;
        """
        temp_file_obj = cache_file.open('link_name')  # 创建临时文件
        names = self.get_batch_data(sqlcmd)
        prev_link_id = None
        curr_link_id = None
        multi_name_obj = None
        for nameinfo in names:
            curr_link_id = nameinfo[1]
            lang_code = nameinfo[2]  # 官方语言种别
            official_name = nameinfo[3]  # 官方语言种别道路名称
            trans_langs = nameinfo[4]  # 翻译语言种别
            trans_names = nameinfo[5]  # 翻译语言种别道路名称
            route_type = nameinfo[6]  # 番号种别
            is_office_name = nameinfo[7]
            road_name_id = nameinfo[8]
            iso_country_code = nameinfo[9]
            phonetic_strings = nameinfo[10]  # TTS音素
            phonetic_lang_codes = nameinfo[11]  # 音素语言种别
            if not curr_link_id:  # link id不存在
                self.log.error("No Link ID.")
                continue

            if prev_link_id != curr_link_id:  # Link id 变更
                if prev_link_id:
                    # ## 保存上一条link的名称
                    json_name = multi_name_obj.json_format_dump()
                    if not json_name:
                        self.log.error('Json Name is none. linkid=%d' %
                                       prev_link_id)
                    # 存到本地的临时文件
                    self._store_name_to_temp_file(temp_file_obj, prev_link_id,
                                                  json_name)
                # ## 初始化
                name_type = self._get_name_type(is_office_name, route_type,
                                                True)
                multi_name_obj = MultiLangNameRDF(lang_code, official_name,
                                                  name_type)
                if phonetic_strings:  # 有音素
                    tts_obj = self.get_tts_name(iso_country_code, lang_code,
                                                name_type, phonetic_strings,
                                                phonetic_lang_codes)
                    if tts_obj:
                        multi_name_obj.set_tts(tts_obj)
                    else:
                        self.log.warning('No TTS. linkid=%d, name_id=%d' %
                                         (curr_link_id, road_name_id))
                multi_name_obj = self.set_trans_name(multi_name_obj,
                                                     iso_country_code,
                                                     trans_names, trans_langs,
                                                     phonetic_strings,
                                                     phonetic_lang_codes)
                prev_link_id = curr_link_id
            else:  # Link_id 没有变化
                name_type = self._get_name_type(is_office_name, route_type)
                alter_name = MultiLangNameRDF(lang_code, official_name,
                                              name_type)
                if phonetic_strings:
                    tts_obj = self.get_tts_name(iso_country_code, lang_code,
                                                name_type, phonetic_strings,
                                                phonetic_lang_codes)
                    if tts_obj:
                        alter_name.set_tts(tts_obj)
                    else:
                        self.log.warning('No TTS. linkid=%d, name_id=%d' %
                                         (curr_link_id, road_name_id))
                alter_name = self.set_trans_name(alter_name, iso_country_code,
                                                 trans_names, trans_langs,
                                                 phonetic_strings,
                                                 phonetic_lang_codes)
                multi_name_obj.add_alter(alter_name)

        # ## 最后一条link的名称
        json_name = multi_name_obj.json_format_dump()
        if not json_name:
            self.log.error('Json Name is none. linkid=' % prev_link_id)
        # 存到本地的临时文件
        self._store_name_to_temp_file(temp_file_obj, prev_link_id, json_name)
        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'temp_link_name')
        self.pg.commit2()
        # close file
        #temp_file_obj.close()
        cache_file.close(temp_file_obj, True)

        self.CreateIndex2('temp_link_name_link_id_idx')

        self.log.info('End Make Link Name.')
        return 0
Example #18
0
    def _make_hwy_exit_name(self):
        temp_file_obj = cache_file.open('link_name')  # 创建临时文件
        prev_link_id = None
        curr_link_id = None
        prev_is_exit_name = None
        curr_is_exit_name = None
        prev_is_junction_name = None
        curr_is_junction_name = None
        multi_name_obj = None
        for nameinfo in self._get_exit_names():
            curr_link_id = nameinfo[0]
            ref_node_id = ''
            nonref_node_id = ''
            curr_is_exit_name = nameinfo[1]
            curr_is_junction_name = nameinfo[2]
            lang_code = nameinfo[3]  # 官方语言种别
            official_name = nameinfo[4]  # 官方语言种别道路名称
            trans_langs = nameinfo[5]  # 翻译语言种别
            trans_names = nameinfo[6]  # 翻译语言种别道路名称
            if not curr_link_id:  # link id不存在
                self.log.error("No Link ID.")
                continue

            if prev_link_id != curr_link_id:  # Link id 变更
                if prev_link_id:
                    # ## 保存上一条link的名称
                    json_name = multi_name_obj.json_format_dump()
                    if not json_name:
                        self.log.error('Json Name is none. linkid=%d' %
                                       prev_link_id)
                    # 存到本地的临时文件
                    self._store_name_to_temp_file(temp_file_obj,
                                                  prev_link_id,
                                                  ref_node_id,
                                                  nonref_node_id,
                                                  prev_is_exit_name,
                                                  prev_is_junction_name,
                                                  json_name)
                # ## 初始化
                multi_name_obj = MultiLangNameRDF(lang_code,
                                                  official_name)

                multi_name_obj = self.set_trans_name(multi_name_obj,
                                                     None,
                                                     trans_names,
                                                     trans_langs,
                                                     None,  # phonetic
                                                     None  # lang_codes
                                                     )
                prev_link_id = curr_link_id
                prev_is_exit_name = curr_is_exit_name
                prev_is_junction_name = curr_is_junction_name
            else:  # Link_id 没有变化
                if(prev_is_exit_name != curr_is_exit_name or
                   prev_is_junction_name != curr_is_junction_name):
                    json_name = multi_name_obj.json_format_dump()
                    if not json_name:
                        self.log.error('Json Name is none. linkid=%d' %
                                       prev_link_id)
                    # 存到本地的临时文件
                    self._store_name_to_temp_file(temp_file_obj,
                                                  prev_link_id,
                                                  ref_node_id,
                                                  nonref_node_id,
                                                  prev_is_exit_name,
                                                  prev_is_junction_name,
                                                  json_name)
                    multi_name_obj = MultiLangNameRDF(lang_code,
                                                      official_name)

                    multi_name_obj = self.set_trans_name(multi_name_obj,
                                                         None,
                                                         trans_names,
                                                         trans_langs,
                                                         None,  # phonetic
                                                         None  # lang_code
                                                         )
                    prev_link_id = curr_link_id
                    prev_is_exit_name = curr_is_exit_name
                    prev_is_junction_name = curr_is_junction_name
                else:
                    alter_name = MultiLangNameRDF(lang_code,
                                                  official_name)

                    alter_name = self.set_trans_name(alter_name,
                                                     None,
                                                     trans_names,
                                                     trans_langs,
                                                     None,
                                                     None
                                                     )
                    multi_name_obj.add_alter(alter_name)

        # ## 最后一条link的名称
        if multi_name_obj:
            json_name = multi_name_obj.json_format_dump()
            if not json_name:
                self.log.error('Json Name is none. linkid=%s' % prev_link_id)
            # 存到本地的临时文件
            self._store_name_to_temp_file(temp_file_obj,
                                          prev_link_id,
                                          ref_node_id,
                                          nonref_node_id,
                                          prev_is_exit_name,
                                          prev_is_junction_name,
                                          json_name)
        # ## 把名称导入数据库
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_exit_name')
        self.pg.commit2()
        # close file
        # temp_file_obj.close()
        cache_file.close(temp_file_obj, True)
        self.CreateIndex2('temp_link_name_link_id_idx')
        self.log.info('End Make Link Name.')
        return 0