Ejemplo n.º 1
0
    def save_metadata_spatial(self) -> str:
        """
        完成空间元数据的入库更新操作
        :return:
        """
        mdt_spatial_result, mdt_spatial_memo, mdt_spatial = self.metadata.metadata_spatial()
        params = {
            'dsoid': self.object_id,
            'dso_spatial_result': mdt_spatial_result,
            'dso_spatial_parsermemo': mdt_spatial_memo,
            'dso_prj_wkt': mdt_spatial.prj_wkt,
            'dso_prj_proj4': mdt_spatial.prj_proj4,
            'dso_prj_project': mdt_spatial.prj_project,
            'dso_prj_coordinate': mdt_spatial.prj_coordinate,
            'dso_prj_degree': mdt_spatial.prj_degree,
            'dso_prj_zone': mdt_spatial.prj_zone,
            'dso_prj_source': mdt_spatial.prj_source
        }
        database = CFactory().give_me_db(self.file_info.db_server_id)

        params['dso_center_native'] = CFile.file_2_str(mdt_spatial.native_center)
        params['dso_geo_bb_native'] = CFile.file_2_str(mdt_spatial.native_box)
        params['dso_geo_native'] = CFile.file_2_str(mdt_spatial.native_geom)

        if CFile.file_or_path_exist(mdt_spatial.wgs84_center):
            dso_center_wgs84 = database.sql.func_wkt2geometry(
                CUtils.quote(CFile.file_2_str(mdt_spatial.wgs84_center)), self.SRID_WGS84)
        else:
            dso_center_wgs84 = 'null'

        if CFile.file_or_path_exist(mdt_spatial.wgs84_bbox):
            dso_geo_bb_wgs84 = database.sql.func_wkt2geometry(
                CUtils.quote(
                    CFile.file_2_str(mdt_spatial.wgs84_bbox)), self.SRID_WGS84)
        else:
            dso_geo_bb_wgs84 = 'null'

        if CFile.file_or_path_exist(mdt_spatial.wgs84_geom):
            dso_geo_wgs84 = database.sql.func_wkt2geometry(
                CUtils.quote(
                    CFile.file_2_str(mdt_spatial.wgs84_geom)), self.SRID_WGS84)
        else:
            dso_geo_wgs84 = 'null'
            # 所有元数据入库
        CFactory().give_me_db(self.file_info.db_server_id).execute(
            '''
            update dm2_storage_object
            set dso_spatial_result = :dso_spatial_result
                , dso_spatial_parsermemo = :dso_spatial_parsermemo
                , dso_center_native = :dso_center_native
                , dso_geo_bb_native = :dso_geo_bb_native
                , dso_geo_native = :dso_geo_native
                , dso_center_wgs84 = {0}
                , dso_geo_bb_wgs84 = {1}
                , dso_geo_wgs84 = {2}
                , dso_prj_wkt = :dso_prj_wkt
                , dso_prj_proj4 = :dso_prj_proj4
                , dso_prj_project = :dso_prj_project
                , dso_prj_coordinate = :dso_prj_coordinate
                , dso_prj_degree = :dso_prj_degree
                , dso_prj_zone = :dso_prj_zone
                , dso_prj_source = :dso_prj_source
            where dsoid = :dsoid
            '''.format(dso_center_wgs84, dso_geo_bb_wgs84, dso_geo_wgs84),
            params
        )
        return CResult.merge_result(self.Success, '空间元数据处理完毕!')
Ejemplo n.º 2
0
    def __prepare_update_data(self) -> list:
        sql_list = []

        temp_helper_code_list = []

        sql_update_set = ''
        sql_update_params = dict()
        for column_index in range(self.__column_list.size()):
            column = self.__column_list.column_by_index(column_index)

            if column.is_primary_key or (column.value is None):
                continue

            try:
                column_type = self.__database.db_column_type_by_name(
                    column.db_column_type)
                column_value_type = CUtils.dict_value_by_name(
                    column.value, self.Name_Type, self.DataValueType_SQL)
                column_value_as_text = CUtils.any_2_str(
                    CUtils.dict_value_by_name(column.value, self.Name_Text,
                                              ''))
                # 如果值为原生sql, 则不管字段类型为何值, 都直接把sql存入insert_data语句中
                if CUtils.equal_ignore_case(column_value_type,
                                            self.DataValueType_SQL):
                    column_update_set = '{0}={1}'.format(
                        column.name, column_value_as_text)
                elif CUtils.equal_ignore_case(column_value_type,
                                              self.DataValueType_File):
                    column_update_set = '{0}={1}'.format(
                        column.name,
                        ':{0}'.format(self.__param_name(column.name)))
                    self.__database.file2param(sql_update_params,
                                               self.__param_name(column.name),
                                               column_value_as_text)
                else:
                    if CUtils.equal_ignore_case(
                            column_type.set_value_method,
                            self.DB_Column_Set_Method_Function):
                        if len(column_value_as_text
                               ) > column_type.function_param_max_size >= 0:
                            column_data_id = CUtils.one_id()
                            temp_helper_code_list.append(column_data_id)
                            sql_exchange = '''
                            insert into ro_global_spatialhandle(code, data) values(:code, :data)
                            '''
                            param_exchange = {
                                'code': column_data_id,
                                'data': column_value_as_text
                            }
                            sql_list.append((sql_exchange, param_exchange))

                            column_update_set = '{0}={1}'.format(
                                column.name,
                                CUtils.replace_placeholder(
                                    column_type.set_value_template,
                                    dict({
                                        self.Name_Value:
                                        "(select data from ro_global_spatialhandle where code = '{0}')"
                                        .format(column_data_id)
                                    })))
                        else:
                            if column_type.function_param_quoted:
                                column_value_as_text = CUtils.quote(
                                    column_value_as_text)
                            column_update_set = '{0}={1}'.format(
                                column.name,
                                CUtils.replace_placeholder(
                                    column_type.set_value_template,
                                    dict({
                                        self.Name_Value: column_value_as_text
                                    })))
                    elif CUtils.equal_ignore_case(
                            column_type.set_value_method,
                            self.DB_Column_Set_Method_Geometry):
                        if len(column_value_as_text
                               ) > column_type.function_param_max_size >= 0:
                            column_data_id = CUtils.one_id()
                            temp_helper_code_list.append(column_data_id)
                            sql_exchange = '''
                            insert into ro_global_spatialhandle(code, data) values(:code, :data)
                            '''
                            param_exchange = {
                                'code': column_data_id,
                                'data': column_value_as_text
                            }
                            sql_list.append((sql_exchange, param_exchange))

                            column_update_set = '{0}={1}'.format(
                                column.name,
                                CUtils.replace_placeholder(
                                    column_type.set_value_template,
                                    dict({
                                        self.Name_Value:
                                        "(select data from ro_global_spatialhandle where code = '{0}')"
                                        .format(column_data_id),
                                        self.Name_Srid:
                                        CUtils.dict_value_by_name(
                                            column.value, self.Name_Srid,
                                            settings.application.xpath_one(
                                                self.Path_Setting_Spatial_Srid,
                                                self.SRID_WGS84))
                                    })))
                        else:
                            if column_type.function_param_quoted:
                                column_value_as_text = CUtils.quote(
                                    column_value_as_text)
                            column_update_set = '{0}={1}'.format(
                                column.name,
                                CUtils.replace_placeholder(
                                    column_type.set_value_template,
                                    dict({
                                        self.Name_Value:
                                        column_value_as_text,
                                        self.Name_Srid:
                                        CUtils.dict_value_by_name(
                                            column.value, self.Name_Srid,
                                            settings.application.xpath_one(
                                                self.Path_Setting_Spatial_Srid,
                                                self.SRID_WGS84))
                                    })))
                    else:  # if CUtils.equal_ignore_case(column_type.set_value_method, self.DB_Column_Set_Method_Param):
                        column_update_set = '{0}={1}'.format(
                            column.name,
                            ':{0}'.format(self.__param_name(column.name)))
                        sql_update_params[self.__param_name(
                            column.name)] = column_value_as_text

                sql_update_set = CUtils.str_append(sql_update_set,
                                                   column_update_set, ', ')
            except Exception as error:
                print(error.__str__())
                raise

        sql_where = ''
        for column_index in range(self.__column_list.size()):
            column = self.__column_list.column_by_index(column_index)
            if column.is_primary_key:
                sql_where, sql_update_params = self.__prepare_where_condition(
                    column, sql_where, sql_update_params)

        if not CUtils.equal_ignore_case(sql_where, ''):
            sql_where = CUtils.str_append(' where ', sql_where, ' ')

        sql_update = 'update {0} set {1} {2}'.format(self.__table_name,
                                                     sql_update_set, sql_where)
        sql_list.append((sql_update, sql_update_params))

        for temp_helper_code in temp_helper_code_list:
            sql_list.append(
                ("delete from ro_global_spatialhandle where code = '{0}'".
                 format(temp_helper_code), None))

        return sql_list