如何使用Name对象,包括WorkspaceNames和DatasetNames

一、Name对象
    Name对象是一个代表性对象。通过使用Name对象,可以访问它所代表的对象的一些基本属性,而不用将整个对象调入内存,如果需要用到Name对象所代表的对象本身,可以通过Name来获取它。
    Name对象可以应用于Tables,FeatureClasses和Workspaces。

Name:
       1、WorkspaceName
       2、DatasetName
             1>RelationshipClassName
             2>FeatureDatasetName
             3>RasterDatasetName
             4>TinName
             5>TableName   
                        △--FeatureClassName
        使用Name对象最重要的一点是通过Name来获取所代表的对象,要访问Name子类里的任何对象,使用IName接口的Open方法。
示例:查找某个数据集并打开它
public void OpenCities()
{
    IWorkspaceFactory pWSFactory;
    IWorkspace pWS;
    IEnumDatasetName pEnumDSNames;
    IDatasetName pDSName;
    IFeatureClass pCities;

    pWSFactory = new ShapefileWorkspaceFactoryClass();
    pWS = pWSFactory.OpenFromFile("C:\\Data\\Africa", 0);
    pEnumDSNames = pWS.DatasetNames(esriDatasetType.esriDTFeatureClass);//feature class
    pDSName = pEnumDSNames.Next();
    while (pDSName != null)
    {
       if (pDSName.Name == "AfricanCities")
       {
          IName pName = pDSName as IName;
          pCities = pName.Open() as IFeatureClass;
          return;
        }
    pDSName = pEnumDSNames.Next();
}

二、使用DataSetNames
    Name对象的一个常用用法是用来在磁盘中新建数据集时指定名字。例如,在数据转换过程中的输入输出参数中,使用DatasetName对象即可,而不必用dataset对象。DatasetName的所有子类都是可以用New关键字新建的CoClass,一旦创建好就可以设置Name属性来代表数据集。
示例:在C:\Data下创建一个新表PositianFroys.dbf。
IWorkspaceFactory pShapeWSFactory;
IWorkspaceName pWorkName;
IDatasetName pTableName;

pShapeWSFactory = new ShapefileWorkspaceFactoryClass();
pWorkName = pShapeWSFactory.Create("C:\\", "Data", null, 0);
pTableName = new TableNameClass();
pTableName.WorkspaceName = pWorkName;
pTable.Name = "PositianFroys.dbf";

三、数据的转换、输出和装载
    FeatureDataConverter是一个能将要素类、要素集或表转换成其它数据集的CoClass,它不仅可以转换单个的要素类或表,也可以转换整个数据集(如ArcInfo Coverage)。FeatureDataConverter对象能够使数据在geodatabase,shapefile和coverages之间转换支持大多数的数据类型(除了标注)。在把数据引入geodatabase时,可以指定数据的区别;对ArcSDE Geodatabase还可以通过配置ArcSDE的某些关键字来指定存储参数。
    ExportOperation是一个用于输出要素类或表的CoClass。它提供的功能与要素数据转换器大体相似,但是形式更简单。在ArcMap的目录表中右击一个图层时就是调用了这个数据输出函数。
    ObjectLoader用于向表(或要素类)中追加一个表(或要素类)。IObjectLoader只包含一个方法--LoadObjects。

四、验证字段和记录
    FieldChecker对象可以用来验证一个字段集,这对于在不同格式间转换数据时很有用,因为不同格式的数据集字段名可能不通用。FieldChecker遇到非法字段名时能够根据一定的标准(如加底线UID)来纠正错误从而产生一个合法的字段集。在把数据转换成Geodatabase格式时,FieldChecker将为Geometry字段和OID字段生成标准的名字(Shape和OBJECTID)。
    在用ObjectLoader或FeatureDataConverter装载或者转换要素时,非法对象将以枚举的形式被返回(IEnumInvalidObject),通过查看这个枚举,很容易知道哪些对象不能被装载或转换。

五、使用Feature Data Converter
    IFeatureDataConverter接口有3个方法用于转换数据:
1、ConvertFeatureClass
2、ConvertFeatureDataset
3、ConvertTable

public IEnumInvalidObject ConvertFeatureClass (
    IFeatureClassName InputDatasetName,
    IQueryFilter InputQueryFilter,
    IFeatureDatasetName outputFDatasetName,
    IFeatureClassName outputFClassName,
    IGeometryDef OutputGeometryDef,
    IFields OutputFields,
    string configKey,
    int FlushInterval,
    int parentHWND
);
InputDatasetName--IFeatureClassName用于指定转换的要素类的一个Name对象;
InputQueryFilter--IQueryFilter用于过滤要转换的要素的QueryFilter对象;
outputFDatasetName--IFeatureDatasetName要素类输出的新数据集成现存数据集的Name对象;
outputFClassName--IFeatureClassName新输出的要素类的Name对象;
OutputGeometryDef--用于指定输出要素类的空间参考信息的GeometryDef对象。如果知道为null就使用输出的要素集成输入的要素类的空间参考信息。
OutputFields--IFields用于指定输出要素类的字段集。如果把要素类简单的输出为同样的格式,可以使用输入的要素类的字段集。如果格式有变化,建议使用FieldChecker以确保输出的格式有效;
configKey--string用于指定ArcSDE配置关键字的字符串;
FlushInterval--在输出到Geodatabase时用于指定把要素转换到新的要素类时的区间整数值;
parentHWND--指定应用程序的窗口句柄;

示例://IFeatureDataConverter ConvertFeatureClass Example(From .NET Help)

    //e.g., nameOfSourceFeatureClass = "ctgFeatureshp.shp"
    //      nameOfTargetFeatureClass = "ctgFeature"
    public void IFeatureDataConverter_ConvertFeatureClass_Example(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
    {
        //create source workspace name
        IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
        IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;

        //create source dataset name
        IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
        IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
        sourceDatasetName.WorkspaceName = sourceWorkspaceName;
        sourceDatasetName.Name = nameOfSourceFeatureClass;

        //create target workspace name
        IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
        IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;

        //create target dataset name
        IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
        IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
        targetDatasetName.WorkspaceName = targetWorkspaceName;
        targetDatasetName.Name = nameOfTargetFeatureClass;

        //Open input Featureclass to get field definitions.
        ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
        IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();

        //Validate the field names because you are converting between different workspace types.
        IFieldChecker fieldChecker = new FieldCheckerClass();
        IFields targetFeatureClassFields;
        IFields sourceFeatureClassFields = sourceFeatureClass.Fields;
        IEnumFieldError enumFieldError;

        // Most importantly set the input and validate workspaces!
        fieldChecker.InputWorkspace = sourceWorkspace;
        fieldChecker.ValidateWorkspace = targetWorkspace;
        fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);

        // Loop through the output fields to find the geomerty field
        IField geometryField;
        for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
        {
            if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
            {
                geometryField = targetFeatureClassFields.get_Field(i);
                // Get the geometry field's geometry defenition
                IGeometryDef geometryDef = geometryField.GeometryDef;

                //Give the geometry definition a spatial index grid count and grid size
                IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;

                targetFCGeoDefEdit.GridCount_2 = 1;
                targetFCGeoDefEdit.set_GridSize(0, 0); //Allow ArcGIS to determine a valid grid size for the data loaded
                targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;

                // we want to convert all of the features
                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = "";

                // Load the feature class
                IFeatureDataConverter fctofc = new FeatureDataConverterClass();
                IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
                break;
            }
        }
    }