IName 与 IQueryName2

一、IName

Name名称对象标识并且定义了GeoDatabase对象(例如数据集、工作空间、地图对象)等等。Name对象中的Open方法允许用户根据关联的对象的Name得到该对象的实例,它充当了支持绑定到指定的对象的代理角色。通过使用Name对象,可以访问它指定的对象的一些基本属性,而不用将整个对象调入内存。而我们用IWorkspace获得一个workspace,那可是会调入内存的,可是IWorkspaceName就不会。

得到指定的工作空间中的要素类代码:
        /// <summary>
        /// 从指定的工作空间中打开指定的要素类表
        /// 注:打开非栅格数据层
        /// </summary>
        /// <param name="pWS">工作空间对象</param>
        /// <param name="sTabName">数据表名</param>
        /// <param name="sAttrFilter">属性过滤SQL语句</param>
        /// <returns></returns>
        public static IFeatureClass GetFeatureClass(IWorkspace pWS, string sTabName, string sAttrFilter)
        {
            try
            {
                if (pWS == null || sTabName == null) return null;
                IFeatureClass pDS = null;
                IWorkspace2 pWS2 = pWS as IWorkspace2;
                if (pWS2 != null)
                {
                    if (!pWS2.get_NameExists(esriDatasetType.esriDTFeatureClass, sTabName)) return null;
                }
                IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;
                //不使用属性过滤
                if (sAttrFilter == null || sAttrFilter.Trim() == string.Empty)
                    pDS = pFWS.OpenFeatureClass(sTabName);
                //使用属性过滤
                else
                {
                    IQueryDef pQueryDef = pFWS.CreateQueryDef();  //IQueryDef支持在多个表和要素类中进行查询
                    pQueryDef.Tables = sTabName;         //不多表或者要素类以","隔开,SDE数据源用"owner.tablename"格式。
                    pQueryDef.WhereClause = sAttrFilter; //简单的SQL语句where子句,注意SQL部分关键字不能使用
                    IQueryName2 pQueryName = new FeatureQueryNameClass();
                    pQueryName.PrimaryKey = "OBJECTID";
                    pQueryName.QueryDef = pQueryDef;

                    //其中---1、---2为得到IWorkspace[pFWS]所对应的IWorkspaceName
                    IDataset pDS1 = (IDataset)pFWS;  ---------------------------1
                    IWorkspaceName pWSName = (IWorkspaceName)pDS1.FullName; -------------2

                    //务必设置FeatureClass所在的Workspace,这里通过设置它对应的IDatasetName的WorkspaceName 属性来完成
                    IDatasetName pDSName = (IDatasetName)pQueryName;  -------------3 

                    pDSName.WorkspaceName = pWSName;-------------4    

                    //这里可以设置FeatureClass的名称,如pDSName.Name属性;也可以设置名称采用默认值。

                   

                    //pQueryName这里FeatureQueryNameClass的接口,所以最后通过QI 得到的IName中 Open()代开的是个FeatureClass

                    IName pName = (IName)pQueryName;
                    pDS = pName.Open() as IFeatureClass;
                }
                return pDS;
            }
            catch (Exception Err)
            {
                string s = Err.Message;
                return null;
            }
        }

二、通过Name子类创建的的相关对象

    1、创建工作空间IWorkSpace[SDE数据库],列举其中的两种方法:

        I、 直接通过IName中的Open方法

                    IPropertySet propertySet = new PropertySetClass();
                    propertySet.SetProperty("Server", "beniy");
                    propertySet.SetProperty("Instance", "5151");
                    propertySet.SetProperty("Database", "sde"); //Ignored with ArcSDE for Oracle      
                    propertySet.SetProperty("user", "sde");      
                    propertySet.SetProperty("password", "arcsde");      
                    propertySet.SetProperty("version", "sde.DEFAULT");
                    IWorkspaceName pWorkspaceName = new WorkspaceNameClass();
                    pWorkspaceName.WorkspaceFactoryProgID = "esriCore.SdeWorkspaceFactory.1";
                    pWorkspaceName.ConnectionProperties = propertySet;

                    IName pName = pWorkspaceName as IName;
                    IWorkspace pWorkspace = pName.Open() as IWorkspace;

        II、通过IWorkspace中的Create方法

                IPropertySet pProSet = new PropertySetClass();
                pProSet.SetProperty("SERVER", "beniy");
                pProSet.SetProperty("INSTANCE", "5151");
                pProSet.SetProperty("USER", "sde");
                pProSet.SetProperty("PASSWORD", "arcsde");
                pProSet.SetProperty("VERSION", "SDE.DEFAULT");

                IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();
                IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("C:\\temp\\", "Test_Connection.sde", pProSet, 0);
                IName pSdeName = pWorkspaceName as IName;
                IWorkspace pWorkspace = pSdeName.Open() as IWorkspace;

//第二种方法,将在C:\\temp\\目录下保存文件名为:Test_Connection.sde的数据库连接信息文件。这样下次可使用Open()打开,例如:IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass();  pWorkspace  = pWorkspaceFactory.OpenFromFile("C:\\temp\\Test_Connection1.sde", 0);得到对应的 IWorkspace 。

 

三、关于QueryName2


QueryName2是继承于IQueryName的接口,由FeatureQueryName和TableQueryName对象实现。通过设置该接口的QueryDef得到感兴趣的对象或要素。使用QueryDef的表不需要注册为geodatabase,以便为根据QueryDef产生的的表有ObjectID。如果一个表支持选择的话 ObjectID列是必须的,如果QueryDef的表包含几何列,FeatureClass 就可以通过QueryDef后的Iname的Open方法来创建。
怎么设置PrimaryKey和CopyLocally 属性决定了表或要素是否含有ObjectID列,以下描述了各种不同的情形:
1、如果QueryDef本身不包含ObjectID列,PrimaryKey属性设置为某个字段,该字段就被用作ObjectID,无论CopyLocally 是否进行设置。
2、如果QueryDef包含有ObjectID列,而且QueryDef涉及的是单个表或者要素类,则该列就被设置为ObjectID列无论PrimaryKey是否设置
为其他列,当然也不会考虑是否设置了CopyLocally 属性。
3、其他情况下,如果PrimaryKey为空,且将CopyLocally属性设置为true将数据复制到本地的scratch workspace,并将在temp目录下建立临时PGeoDB(*.mdb),然后自动加入ObjectID字段,如果设置为false时,数据将被直接从数据库中连接使用,而每行将没有ObjectID字段。

 

     待续……

                 

posted @ 2009-05-06 14:31  爱图--UpdooGIS  阅读(949)  评论(0编辑  收藏  举报