Beispiel #1
0
    def _get_shield_info(self,
                         link_id,
                         shieldids,
                         shieldnums,
                         routenums,
                         l_lang_code,
                         r_lang_code,
                         phoneme_infos=None):
        multi_shield_obj = None
        route_num_list = list()
        num_phoneme_list = list()
        shield_obj = ShieldTA()
        temp_m_name = MultiLangNameTa(n_id=link_id)
        temp_m_name.set_lang_code(l_lang_code, r_lang_code)
        lang_infos = temp_m_name.get_side_lang_infos()  # 取各左右两边的语种
        if not phoneme_infos:
            phoneme_infos = [None] * len(shieldids)
        for shieldid, shieldnum, routenum, phoneme_info in \
                zip(shieldids, shieldnums, routenums, phoneme_infos):
            if not shieldid:  # shield id 无
                self.log.error("No new shield id. link_id=%d", link_id)
                continue
            if not shieldnum:  # shield number 无
                self.log.error("No new shield num. link_id=%d", link_id)
                continue
            if not routenum:  # route number 无
                self.log.error("No new route num. link_id=%d", link_id)
                continue

            # 判断是否多个番号, 如: “17/92”,“60  70”,“14-16”
            if shield_obj.is_mulit_shield_num(shieldnum):
                if shield_obj.is_shield_num_repeat(shieldnum, shieldnums):
                    # 每个单独番号已存在, 就不在收录。
                    continue
            # 转成新的全球统一的shield id
            new_shieldid = shield_obj.convert_shield_id(shieldid)
            if not new_shieldid:  # 新shield id无
                self.log.error("No new shield id. link_id=%s, shieldid=%s",
                               link_id, shieldid)
            for (left_right, lang_code) in lang_infos:
                if lang_code is None:
                    lang_code = 'ENG'

                if multi_shield_obj:
                    # ## shield number
                    alter_shield = MultiLangShield(new_shieldid, shieldnum,
                                                   lang_code, TTS_TYPE_NOT_TTS,
                                                   left_right)
                    multi_shield_obj.add_alter(alter_shield)
                else:
                    # ## shield number
                    multi_shield_obj = MultiLangShield(new_shieldid, shieldnum,
                                                       lang_code,
                                                       TTS_TYPE_NOT_TTS,
                                                       left_right)
            # route number
            route_num_list.append(routenum)
            num_phoneme_list.append(phoneme_info)

        return multi_shield_obj, route_num_list, num_phoneme_list
Beispiel #2
0
    def _make_hwy_exit_name(self):
        self.log.info('Make exit Name.')
        self.CreateIndex2('org_ig_elemid_idx')
        self.CreateIndex2('org_ig_id_idx')
        self.CreateIndex2('org_is_id_idx')
        sqlcmd = """
        SELECT distinct node_tbl.node_id, org_is.fwinttyp,
               org_is.name, org_is.namelc
        FROM org_is
        LEFT JOIN org_ig
          ON org_is.id = org_ig.id
        INNER JOIN node_tbl
        on org_ig.elemid = node_tbl.node_id
        WHERE org_is.inttyp = 1 -- FREE WAY
          AND org_is.fwinttyp IN (1, 2)  -- MOTORWAY
          AND org_ig.elemtyp = 4120 -- Junction
          AND org_is.name is not null;
        """
        temp_file_obj = cache_file.open('hwy_exit_name')

        names = self.get_batch_data(sqlcmd)
        for name_info in names:
            node_id = name_info[0]
            fwin_type = name_info[1]
            name = name_info[2]
            lang_code = name_info[3]
            name_type = NAME_TYPE_OFFICIAL
            is_exit_name, is_junction_name = self._get_name_type(fwin_type)
            if not name:
                self.log.warning('No name and number. id=%d' % node_id)
                continue
            else:
                multi_name = MultiLangNameTa(node_id, lang_code, name,
                                             name_type)

                json_name = multi_name.json_format_dump()
                if not json_name:
                    self.log.error('Json Name is none. node_id=%d' % node_id)
            self._store_name_to_temp_file(temp_file_obj, node_id, is_exit_name,
                                          is_junction_name, json_name)
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_exit_name_ta')
        self.pg.commit2()
        cache_file.close(temp_file_obj, True)
        self.log.info('End Make exit Name.')
        return 0
 def _cvt_signpost_name_2_json(self, sp_name_list, sp_name_num_limited=True):
     multi_name = None
     num = 0
     added_sp_names = {}
     for sp_name in sp_name_list:
         str_name = sp_name.get(SP_TEXT)
         lang_code = sp_name.get(SP_LANG_CODE)
         lang_code = lang_code_dict.get(lang_code)
         key = (str_name, lang_code)
         if not multi_name:
             multi_name = MultiLangNameTa(lang_code=lang_code,
                                          str_name=str_name,
                                          name_type=NAME_TYPE_OFFICIAL)
         else:
             if key in added_sp_names:  # 名称已经收录
                 continue
             other_name = MultiLangNameTa(lang_code=lang_code,
                                          str_name=str_name,
                                          name_type=NAME_TYPE_OFFICIAL)
             multi_name.add_alter(other_name)
         added_sp_names[key] = None
         num += 1
         if sp_name_num_limited == True:
             if num > MAX_SP_NAME_NUM:
                 break
     if multi_name:
         return multi_name.json_format_dump()
     return None
    def _make_hwy_sapa_name(self):
        self.log.info('Make mid_temp_hwy_sapa_name')
        sqlcmd = '''
        SELECT id, name, lancd
        FROM org_mnpoi_pi
        -- 7358:Truck Stop, 7395: Rest Area, 7369: Open Parking Area
        WHERE feattyp in (7358, 7395, 7369) AND name is not null
        '''
        temp_file_obj = cache_file.open('hwy_sapa_name')
        contry_name = getProjCountry()
        names = self.get_batch_data(sqlcmd)
        default_lang_code = DEFAULT_LANG_CODES.get(contry_name.lower())
        for name_info in names:
            poi_id = name_info[0]
            name = name_info[1]
            lang_code = name_info[2]
            name_type = NAME_TYPE_OFFICIAL
            if not name:
                self.log.warning('No name and number. id=%d' % poi_id)
                continue
            if not lang_code:
                lang_code = default_lang_code
            if not lang_code:
                self.log.error('language code is none.poi_id=%d' % poi_id)
            multi_name = MultiLangNameTa(poi_id, lang_code, name, name_type)

            json_name = multi_name.json_format_dump()
            if not json_name:
                self.log.error('Json Name is none. poi_id=%d' % poi_id)

            self._store_name_to_temp_file(temp_file_obj, poi_id, json_name)
        temp_file_obj.seek(0)
        self.pg.copy_from2(temp_file_obj, 'mid_temp_hwy_sapa_name')
        self.pg.commit2()

        cache_file.close(temp_file_obj, True)

        self.log.info('End make mid_temp_hwy_sapa_name')
        return 0
Beispiel #5
0
 def _make_signpost_element(self):
     self.log.info('Start Make SignPost element.')
     # 注:有TTS音素的名称:4G, 6T, 4I, 9D, 4E, RN
     # 前五个种别做成名称,4E做成出口番号,RN,不收录
     sqlcmd = """
     SELECT id::bigint,
            array_agg(seqnr) as  seqnrs,
            array_agg(destseq) as destseqs,
            array_agg(infotyp) as infotyps,
            array_agg(rnpart) as rnparts,
            array_agg(txtcont) as names,
            array_agg(txtcontlc) as lang_codes,
            array_agg(phoneme_info) as phoneme_infos
       FROM (
             SELECT id, seqnr, destseq, infotyp,
                    rnpart, txtcont, txtcontlc,
                    mid_get_phoneme(id::bigint, 2128, 0,   -- 2128: SignPost
                            txtcont, txtcontlc, infotyp) as phoneme_info
               FROM org_si
               order by id, seqnr, destseq, rnpart
       ) AS A
       GROUP BY id;
     """
     self.CreateTable2('mid_temp_signpost_element')
     temp_file_obj = cache_file.open('signpost_element')  # 创建临时文件
     shield_obj = ShieldTA()
     signs = self.get_batch_data(sqlcmd)
     for sign_info in signs:
         sign_id = sign_info[0]  # 看板id
         seqnr = sign_info[1]  # 方向番号
         destseqs = sign_info[2]  # 同个方向内顺序号
         infotyps = sign_info[3]  # 种别
         # rnparts = sign_info[3]  # 道路番号内部无素的序号
         names = sign_info[5]  # 名称、番号
         lang_codes = sign_info[6]  # 语种
         phoneme_infos = sign_info[7]  # 音素
         sign_post = SignPostElementTa(sign_id, seqnr)
         shield_id = None
         exit_no = None
         route_no = None
         signpost_name = None
         for destseq, infotyp, name, lang_code, phoneme_info in \
                 zip(destseqs, infotyps, names, lang_codes, phoneme_infos):
             sign_type = self._get_sign_type(infotyp)
             if not sign_type:  # 空不收录
                 continue
             name_type = self._cvt_name_type(sign_type)
             if not lang_code or lang_code == 'UND':
                 lang_code = MultiLangNameTa.get_lang_code_by_id(sign_id)
             # ## 处理各种种别的数据
             if SIGN_POST_TYPE_SHIELD_ID == sign_type:  # shield id
                 # 取得shield id
                 shield_id = shield_obj.convert_shield_id(int(name))
             elif SIGN_POST_TYPE_SHIELD_NUM == sign_type:  # shield number
                 if shield_id:  # 盾牌号存在
                     shield_number = name
                     route_no = MultiLangShield(shield_id, shield_number,
                                                lang_code)
                     sign_post.add_route_no(route_no)
                     shield_id = None
                 else:
                     # 盾牌和番号是成对出现的,而且盾牌在前
                     self.log.error(
                         'No Shield id. sign_id=%d, seqnr=%d, destseq=%d' %
                         (sign_id, seqnr, destseq))
             elif SIGN_POST_TYPE_EXIT_NO == sign_type:  # 出口番号
                 if exit_no:
                     # 存在多个出口番号,其他出口番号做出口番号的别名
                     alter_exit_no = MultiLangNameTa(
                         sign_id, lang_code, name, name_type)
                     # ## TTS音素
                     phoneme_list, language_list = \
                         alter_exit_no.split_phoneme_info(phoneme_info)
                     alter_exit_no.add_all_tts(phoneme_list, language_list,
                                               lang_code)
                     exit_no.add_alter(alter_exit_no)
                 else:
                     # 第一个出口番号
                     exit_no = MultiLangNameTa(sign_id, lang_code, name,
                                               name_type)
                     # ## TTS音素
                     phoneme_list, language_list = \
                         exit_no.split_phoneme_info(phoneme_info)
                     exit_no.add_all_tts(phoneme_list, language_list,
                                         lang_code)
             elif SIGN_POST_TYPE_NAME == sign_type:  # 方面名称
                 if signpost_name:
                     # 其他名称番号
                     alter_sign_name = MultiLangNameTa(
                         sign_id, lang_code, name, name_type)
                     # ## TTS音素
                     phoneme_list, language_list = \
                         alter_sign_name.split_phoneme_info(phoneme_info)
                     alter_sign_name.add_all_tts(phoneme_list,
                                                 language_list, lang_code)
                     signpost_name.add_alter(alter_sign_name)
                 else:  # 第一个名称
                     signpost_name = MultiLangNameTa(
                         sign_id, lang_code, name, name_type)
                     # ## TTS音素
                     phoneme_list, language_list = \
                         signpost_name.split_phoneme_info(phoneme_info)
                     signpost_name.add_all_tts(phoneme_list, language_list,
                                               lang_code)
             else:
                 continue
         if route_no or exit_no or signpost_name:
             sign_post.set_exit_no(exit_no)
             sign_post.set_signpost_name(signpost_name)
             str_info = sign_post.to_string()
             if str_info:
                 self._store_name_to_temp_file(temp_file_obj, str_info)
             else:
                 self.log.error('No SignPost Info. sign_id=%d' % sign_id)
         else:
             self.log.warning(
                 'No route_no/exit_no/signpost_name.sign_id=%d' % (sign_id))
     # ## 把名称导入数据库
     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)
     self.log.info('End Make SignPost element.')
     return 0
Beispiel #6
0
    def merge_links_name(self, link_list):
        '''把所有给定link的名称、番号合并起来,返回JSON格式的名称和shield。'''
        self.pg.connect1()
        self.set_language_code()
        str_name = None
        str_shield = None
        if not link_list:
            return str_name, str_shield
        # ## 取得link的语种
        l_lang_code, r_lang_code = None, None
        l_langs, r_langs = self._merge_link_lang_code(link_list)
        if l_langs:
            l_lang_code = l_langs[0]
        if r_langs:
            r_lang_code = r_langs[0]
        if len(l_langs) > 1 or len(r_langs) > 1:
            self.log.warning('Number of language code > 1. links=%s' %
                             link_list)
        # 合并盾牌
        multi_shield, routenums, num_phoneme_infos = \
                self._merge_links_shield(link_list, l_lang_code, r_lang_code)
        if multi_shield:
            json_shield = multi_shield.json_format_dump()
            if not json_shield:
                self.log.error('Error JSON Name. linkid=%d' % link_list[-1])
        else:
            json_shield = None

        sqlcmd = """
        SELECT array_agg(id) as ids,
               array_agg(fullname) as fullnames,
               array_agg(namelc) as lang_codes,
               array_agg(nametyp) as nametypes,
               array_agg(sol) as sols,
               array_agg(phoneme_info) as phoneme_infos
          FROM (
                SELECT 1 as id_for_group, id, fullname, namelc,
                       nametyp, sol, phoneme_info
                  FROM (
                        SELECT min(id) as id, fullname, namelc, nametyp, sol,
                               -- 4110: Road
                               mid_get_phoneme(id::bigint,
                                               4110,  -- 4110: Road
                                               sol,
                                               fullname,
                                               namelc,
                                               nametyp::character varying
                                               ) as phoneme_info
                          FROM org_gc
                          WHERE nametyp & 4 <> 4  -- not route number
                                and id in (links)
                          group by fullname, namelc, nametyp, sol, phoneme_info
                  ) as A
                  ORDER BY sol,  -- 0:both, 1:left, 2:right
                           (nametyp & 1) desc,   -- official name
                           (nametyp & 16) desc,  -- Street Name
                           (nametyp & 8) desc,   -- Brunnel Name
                           (nametyp & 32) desc,  -- Locality Name
                           (nametyp & 64) desc,  -- Postal Street Name
                           id
          ) as B
          group by id_for_group;
        """
        str_link_ist = str(tuple(link_list)).replace('L', '')
        sqlcmd = sqlcmd.replace('(links)', str_link_ist)
        self.pg.execute1(sqlcmd)
        row = self.pg.fetchone()
        self.pg.close1()
        json_name = None
        multi_name = MultiLangNameTa(link_list[-1])
        # 设置语种
        multi_name.set_lang_code(l_lang_code, r_lang_code)
        if row:
            # ids = row[0]
            names = row[1]
            lang_codes = row[2]
            nametypes = row[3]
            sols = row[4]  # 左右侧名称
            phoneme_infos = row[5]
            # 去重处理,同一个番号,在不同的link间,等组不同,引起的
            # ...
            if names:
                multi_name.set_multi_name(names, lang_codes, nametypes, sols,
                                          phoneme_infos)
                multi_name.set_multi_routenum(names, routenums,
                                              num_phoneme_infos)

                json_name = multi_name.json_format_dump()
                if not json_name:
                    self.log.error('Error JSON Name.linkid=%d' % link_list[-1])
                else:
                    pass
            else:  # 没有名称list(这个不可能进来)
                pass
        else:  # 没有名称
            if routenums:
                multi_name.set_multi_routenum(None, routenums,
                                              num_phoneme_infos)
                json_name = multi_name.json_format_dump()
                if not json_name:
                    self.log.error('Error JSON Name.linkid=%d' % link_list[-1])
                else:
                    pass
            else:
                pass
        if json_shield and not json_name:  # 有番号,没有名称
            self.log.error('Exist JSON shield, but no JSON Name.linkid=%d' %
                           link_list[-1])
        return json_name, json_shield
Beispiel #7
0
    def _make_link_name(self):
        "道路名称(包括显示用番号)"
        self.log.info('Make Link Name.')
        # 比较名称的种别和音素的种别
        self.CreateFunction2('mid_compare_nametype')
        # 取得音素信息,返回格式'[音素文本1|音素文本2|...][语种1|语种2|...]'
        self.CreateFunction2('mid_get_phoneme')

        # 名称排序规则:
        # 1. id, 2. sol(左右侧)
        # 3. official name, 4. Street Name,
        # 5. Brunnel Name(桥/隧道名), 6. Locality Name
        # 7. Postal Street Name,   8. gid
        # 9. 所有番号排在名称之后
        # 注:1. 这里使用Full Join是因为,有些道路只有番号,没有名称。
        # 2. 一个越南语文本,一般会有一个越南音素和一个英文音素
        # 3. 音素种别ON, AN, RN, RJ, BU,其中RJ不知道是什么名称
        sqlcmd = """
        SELECT code.id::bigint, fullnames, lang_codes,
               nametypes, sols, phoneme_infos,
               routenums, num_phoneme_infos, l_laxonlc,
               r_laxonlc
          FROM mid_temp_link_lang_code AS code
          LEFT JOIN
          -- link names
          (
                SELECT id, array_agg(fullname) as fullnames,
                       array_agg(namelc) as lang_codes,
                       array_agg(nametyp) as nametypes,
                       array_agg(sol) as sols,
                       array_agg(phoneme_info) as phoneme_infos
                FROM (
                        SELECT id, fullname, namelc, nametyp, sol,
                               mid_get_phoneme(id::bigint,
                                               4110,     -- 4110: Road
                                               sol,
                                               fullname,
                                               namelc,
                                               nametyp::character varying
                                               ) as phoneme_info
                          FROM org_gc
                          WHERE nametyp & 4 <> 4
                          ORDER BY id,
                                   sol,  -- left, right
                                   (nametyp & 1) desc,   -- official name
                                   (nametyp & 16) desc,  -- Street Name
                                   (nametyp & 8) desc,   -- Brunnel Name
                                   (nametyp & 32) desc,  -- Locality Name
                                   (nametyp & 64) desc,  -- Postal Street Name
                                   gid
                   ) AS A
                   GROUP BY id
          ) AS link_name
          ON code.id = link_name.id
          -- Route Numbers
          LEFT JOIN (
                SELECT link_id, array_agg(routenum) as routenums,
                       array_agg(num_phoneme_info) as num_phoneme_infos
                  from (
                        SELECT link_id, routenum,
                               -- 4110: Road, 0: Both, 4: Route Number
                               mid_get_phoneme(link_id,
                                               4110, -- 4110: Road
                                               0,    -- 0: both side
                                               routenum,
                                               'UND',
                                               '4'   -- 4: Route Num
                                               ) as num_phoneme_info
                          FROM temp_link_route_num
                          order by gid
                  ) as rn
                  group by link_id
          ) as route_num
          on code.id = link_id;
        """
        temp_file_obj = cache_file.open('link_name')  # 创建临时文件
        self.CreateTable2('temp_link_name')
        names = self.get_batch_data(sqlcmd)
        for name_info in names:
            link_id = name_info[0]
            names = name_info[1]
            lang_codes = name_info[2]
            nametypes = name_info[3]
            sols = name_info[4]  # 左右侧名称
            phoneme_infos = name_info[5]  # 名称音素
            routenums = name_info[6]
            num_phoneme_infos = name_info[7]  # 番号的音素
            l_lang_code = name_info[8]  # 左边官方语种
            r_lang_code = name_info[9]  # 右边官方语种
            if not names and not routenums:
                self.log.warnging('No name and number. id=%d' % link_id)
                continue
            multi_name = MultiLangNameTa(link_id)
            multi_name.set_lang_code(l_lang_code, r_lang_code)
            multi_name.set_multi_name(names, lang_codes, nametypes, sols,
                                      phoneme_infos)
            multi_name.set_multi_routenum(names, routenums, num_phoneme_infos)

            json_name = multi_name.json_format_dump()
            if not json_name:
                self.log.error('Json Name is none. linkid=%d' % link_id)
            # 存到本地的临时文件
            self._store_name_to_temp_file(temp_file_obj, 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.log.info('End Make Link Name.')
        return 0
 def _get_last_main_link_name(self, node_id, pathes):
     
     exit_main_link_name_list = [] # 保存不同路径的脱出道路番号/道路名称
     driving_side = -1
     for (path, horizontal_degree) in pathes:
         exit_main_link_names = [] # 保存一条路径脱出link的道路番号&道路名称(道路番号在前,道路名称在后)
         (rad, driving_side) = self._get_inlink_outnode_rad(path, node_id, horizontal_degree)
         last_link = (path[-2], path[-1])  # 最后一条脱出link
         last_data = self.G[last_link[0]][last_link[1]]
         
         # 一条路径同时存在道路番号/名称时,优先保存道路番号(存在多个时仅保留第一个)
         # 其后保存道路名称(存在多个时仅保留第一个)
         road_nums = last_data.get(HWY_ROAD_NUMS)
         if road_nums:
             exit_main_link_names.append(road_nums[0])
         
         road_names = last_data.get(HWY_ROAD_NAMES)
         if road_names:
             exit_main_link_names.append(road_names[0])
         
         exit_main_link_name_list.append((exit_main_link_names, rad))
         
     if len(exit_main_link_name_list) > 1:
         self.log.warning('exit_main_link_name_list > 1!!! node=%s' % node_id)
     
     # 不同JCT路径对应的道路番号/道路名称根据左右行、脱出link角度进行排序   
     if driving_side == 1:
         # 左行 升序
         exit_main_link_name_list.sort(key=lambda x:x[1], reverse=False)
     elif driving_side == 0:
         # 右行 降序
         exit_main_link_name_list.sort(key=lambda x:x[1], reverse=True)
     else:
         self.log.error('driving_side error!!! node=%s' % node_id)
         return None
     
     sorted_main_link_names = []
     for (exit_main_link_names, rad) in exit_main_link_name_list:
         sorted_main_link_names += exit_main_link_names
     
     added_main_link_names = {} # 保存已收录的名称(已收录过的名称不再收录)
     multi_name = None
     for main_link_name in sorted_main_link_names:
         str_name = main_link_name.get(u'val')
         name_type = main_link_name.get(u'type')
         lang_code = main_link_name.get(u'lang')
         #lang_code = lang_code_dict.get(lang_code)
         key = (str_name, lang_code)
         
         if not multi_name:
             multi_name = MultiLangNameTa(lang_code=lang_code,
                                          str_name=str_name,
                                          name_type=name_type)
         else:
             if key in added_main_link_names:  # 名称已经收录
                 continue
             other_name = MultiLangNameTa(lang_code=lang_code,
                                          str_name=str_name,
                                          name_type=name_type)
             multi_name.add_alter(other_name)
         added_main_link_names[key] = None
     
     if multi_name:
         return multi_name.json_format_dump()
         
     return None