IDataSet接口的使用心得

时间:2021-08-17 16:43:22

在ArcGIS的体系内,IDataset是一个非常重要的接口,从这个接口出发,可以访问各个层的数据源,包括矢量和栅格等。在IDataset接口的内部还提供了Workspace的属性,用于访问数据源的Workspace,继而可以从Workspace来得到WorkspaceFactory对象,这一点是非常重要的,因为在一些情况下,需要分辨数据源的类型,比如是Shapefile格式还是Coverage等问题,就可以通过WorkspaceFactory对象的类型类判断了。需要说明的是:在IFeatureLayer接口内还有提供了一个属性(DataSourceType),也可以用于判断数据源的类型,但缺点是类型简单,而且仅限于矢量数据层。

虽然FeatureClass支持IDataset接口,但为null,因此,如果需要IDataset接口对象,需要从FeatureLayer、RasterLayer等层对象上直接检索。

ArcGIS似乎没有支持矢量的层的Copy(IDataset虽然有该成员函数,但没有的实现),需要自己写代码,以下是从已有矢量层Copy数据集的代码:

 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace _featureWorkspace = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
 ESRI.ArcGIS.Carto.IFeatureLayer _featureLayer = featureLayer as ESRI.ArcGIS.Carto.IFeatureLayer;
 if (_featureLayer != null)
 {
  ESRI.ArcGIS.Geodatabase.IWorkspaceEdit _workspaceEdit = _featureWorkspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
  _workspaceEdit.StartEditing(true);
  _workspaceEdit.StartEditOperation();

  try
  {
   ESRI.ArcGIS.Geodatabase.IFeatureClass _featureClass = _featureWorkspace.CreateFeatureClass(datasetName, _featureLayer.FeatureClass.Fields, _featureLayer.FeatureClass.CLSID, _featureLayer.FeatureClass.EXTCLSID, _featureLayer.FeatureClass.FeatureType, _featureLayer.FeatureClass.ShapeFieldName, "");
  
   ESRI.ArcGIS.Geodatabase.IFeatureCursor _featureCursor = _featureLayer.Search(null, false);
   ESRI.ArcGIS.Geodatabase.IFeature _feature = _featureCursor.NextFeature();
   while (_feature != null)
   {
    ESRI.ArcGIS.Geodatabase.IFeature _featureNew = _featureClass.CreateFeature();

    for (int i=0;i<_featureClass.Fields.FieldCount;i++)
    {
     ESRI.ArcGIS.Geodatabase.IField _field = _featureNew.Fields.get_Field(i);
     if (_field.Editable == true)
     {
      _featureNew.set_Value(i, _feature.get_Value(i));
     }
    }
    _featureNew.Store();
    _feature = _featureCursor.NextFeature();
   }
   _workspaceEdit.StopEditOperation();
   _workspaceEdit.StopEditing(true);

   return _featureClass;
  }
  catch(System.Exception err)
  {
   _workspaceEdit.UndoEditOperation();
   _workspaceEdit.StopEditing(false);

   throw err;
  }
 }