将图层导入SDE
将图层导入SDE有两种情况,一种是直接导入到SDE的根目录下,一种是SDE中用户建立了要素集(Dataset),然后往某个要素集中导入数据
第一种情况:直接往SDE根目录下导入图层:
- bool impLayerByName(IName lName,IWorkspace inWorkspace,IWorkspace outWorkspace)
- {
- try
- {
- //Open input Featureclass to get field definitions.
- IFeatureClass inFeatureClass = (IFeatureClass)lName.Open();
- IFeatureClassName inFeatureClassName = (IFeatureClassName)lName;
- // Set out dataset and feature class names.
- IFeatureClassName outFeatureClassName = new FeatureClassNameClass();
- IDatasetName outDatasetName = (IDatasetName)outFeatureClassName;
- IDataset outWorkspaceDataset = (IDataset)outWorkspace;
- IWorkspaceName outWorkspaceName = (IWorkspaceName)outWorkspaceDataset.FullName;
- outDatasetName.WorkspaceName = outWorkspaceName;
- //string outFullName = ((IDatasetName)lName).Name;
- //string outFileName = outFullName.Substring(outFullName.LastIndexOf(".") + 1);
- outDatasetName.Name = "SDE."+((IDatasetName)lName).Name;
- //Validate the field names.
- IFieldChecker fieldChecker = new FieldCheckerClass();
- IFields outFeatureClassFields;
- IFields inFeatureClassFields = inFeatureClass.Fields;
- IEnumFieldError enumFieldError;
- fieldChecker.InputWorkspace = inWorkspace;
- fieldChecker.ValidateWorkspace = outWorkspace;
- // Validate the fields.
- fieldChecker.Validate(inFeatureClassFields,out enumFieldError,outoutFeatureClassFields);
- // Set up the geometry definition.
- IField geometryField;
- geometryField = outFeatureClassFields.get_Field(outFeatureClassFields.FindField(inFeatureClass.ShapeFieldName));
- // Get the geometry field‘s geometry definition.
- IGeometryDef geometryDef = geometryField.GeometryDef;
- //Set up the IQueryFilter to convert all the features by leaving a blank WhereClause
- QueryFilter qf = new QueryFilterClass();
- qf.WhereClause = "";
- //Load the feature class
- IFeatureDataConverter fctofc = new FeatureDataConverterClass();
- IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(inFeatureClassName,qf,null,outFeatureClassName,geometryDef,outFeatureClassFields,"",1000,0);
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
其中,lName是源图层的IName,inWorkspace是源图层所在的工作空间,outWorkspace是要导出到的工作空间,这段代码默认将导出后的图层命名为与源图层一样
第二种情况:往SDE中某个要素集导入图层
- bool impLayerByNameIntoDataset(IName lName,IWorkspace inWorkspace,IFeatureDataset featureDataset)
- {
- try
- {
- //IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
- //IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;
- //Open input Featureclass to get field definitions.
- IFeatureClass inFeatureClass = (IFeatureClass)lName.Open();
- IFeatureClassName inFeatureClassName = (IFeatureClassName)lName;
- // Set out dataset and feature class names.
- IFeatureClassName outFeatureClassName = new FeatureClassNameClass();
- IDatasetName outDatasetName = (IDatasetName)outFeatureClassName;
- IDataset outWorkspaceDataset = (IDataset)featureDataset.Workspace;
- IWorkspaceName outWorkspaceName = (IWorkspaceName)outWorkspaceDataset.FullName;
- outDatasetName.WorkspaceName = outWorkspaceName;
- outDatasetName.Name = "SDE." + ((IDatasetName)lName).Name;
- //Validate the field names.
- IFieldChecker fieldChecker = new FieldCheckerClass();
- IFields outFeatureClassFields;
- IFields inFeatureClassFields = inFeatureClass.Fields;
- IEnumFieldError enumFieldError;
- fieldChecker.InputWorkspace = inWorkspace;
- fieldChecker.ValidateWorkspace = featureDataset.Workspace;
- // Validate the fields.
- fieldChecker.Validate(inFeatureClassFields,out enumFieldError,outoutFeatureClassFields);
- //featureDataset.CreateFeatureClass("SDE." + ((IDatasetName)lName).Name,outFeatureClassFields,ocDesc.InstanceCLSID,ocDesc.ClassExtensionCLSID,esriFeatureType.esriFTSimple,fcDesc.ShapeFieldName,"");
- // Set up the geometry definition.
- IField geometryField;
- geometryField = outFeatureClassFields.get_Field(outFeatureClassFields.FindField(inFeatureClass.ShapeFieldName));
- // Get the geometry field‘s geometry definition.
- IGeometryDef geometryDef = geometryField.GeometryDef;
- //Set up the IQueryFilter to convert all the features by leaving a blank WhereClause
- IQueryFilter qf = new QueryFilterClass();
- qf.WhereClause = "";
- //Load the feature class
- IFeatureDataConverter fctofc = new FeatureDataConverterClass();
- IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(inFeatureClassName,qf,(IFeatureDatasetName)featureDataset.FullName,outFeatureClassName,geometryDef,outFeatureClassFields,"",1000,0);
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
其中,lName是源图层的IName,inWorkspace是源图层所在的工作空间,featureDataset是目标数据集,这段代码默认将导出后的图层命名为与源图层一样
附:获取SDE中数据集(Dataset)的方法:
- //在SDE里查找名称为datasetName的Dataset
- IFeatureDataset outSet = null;
- IEnumDatasetName idNames = outSpace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
- IDatasetName idNames.Reset();
- fNameBase = idNames.Next();
- while (fNameBase != null)
- {
- if(fNameBase.Name == datasetName) outSet = (IFeatureDataset)((IName)fNameBase).Open();
- fNameBase = idNames.Next();
- }
其中outSpace为已经打开的SDE工作空间