码迷,mamicode.com
首页 > 其他好文 > 详细

将图层导入SDE

时间:2015-06-25 13:42:20      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

将图层导入SDE

 (2012-09-17 10:07:14)
标签: 

育儿

分类: GIS

将图层导入SDE

 

将图层导入SDE有两种情况,一种是直接导入到SDE的根目录下,一种是SDE中用户建立了要素集(Dataset),然后往某个要素集中导入数据

第一种情况:直接往SDE根目录下导入图层:

  1. bool impLayerByName(IName lName,IWorkspace inWorkspace,IWorkspace outWorkspace)
  2. {
  3. try
  4. {
  5. //Open input Featureclass to get field definitions.
  6. IFeatureClass inFeatureClass = (IFeatureClass)lName.Open();
  7. IFeatureClassName inFeatureClassName = (IFeatureClassName)lName;
  8. // Set out dataset and feature class names.
  9. IFeatureClassName outFeatureClassName = new FeatureClassNameClass();
  10. IDatasetName outDatasetName = (IDatasetName)outFeatureClassName;
  11. IDataset outWorkspaceDataset = (IDataset)outWorkspace;
  12. IWorkspaceName outWorkspaceName = (IWorkspaceName)outWorkspaceDataset.FullName;
  13. outDatasetName.WorkspaceName = outWorkspaceName;
  14. //string outFullName = ((IDatasetName)lName).Name;
  15. //string outFileName = outFullName.Substring(outFullName.LastIndexOf(".") + 1);
  16. outDatasetName.Name = "SDE."+((IDatasetName)lName).Name;
  17. //Validate the field names.
  18. IFieldChecker fieldChecker = new FieldCheckerClass();
  19. IFields outFeatureClassFields;
  20. IFields inFeatureClassFields = inFeatureClass.Fields;
  21. IEnumFieldError enumFieldError;
  22. fieldChecker.InputWorkspace = inWorkspace;
  23. fieldChecker.ValidateWorkspace = outWorkspace;
  24. // Validate the fields.
  25. fieldChecker.Validate(inFeatureClassFields,out enumFieldError,outoutFeatureClassFields);
  26. // Set up the geometry definition.
  27. IField geometryField;
  28. geometryField = outFeatureClassFields.get_Field(outFeatureClassFields.FindField(inFeatureClass.ShapeFieldName));
  29. // Get the geometry field‘s geometry definition.
  30. IGeometryDef geometryDef = geometryField.GeometryDef;
  31. //Set up the IQueryFilter to convert all the features by leaving a blank WhereClause
  32. QueryFilter qf = new QueryFilterClass();
  33. qf.WhereClause = "";
  34. //Load the feature class
  35. IFeatureDataConverter fctofc = new FeatureDataConverterClass();
  36. IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(inFeatureClassName,qf,null,outFeatureClassName,geometryDef,outFeatureClassFields,"",1000,0);
  37. return true;
  38. }
  39. catch (Exception ex)
  40. {
  41. MessageBox.Show(ex.Message);
  42. return false;
  43. }
  44. }

 

其中,lName是源图层的IName,inWorkspace是源图层所在的工作空间,outWorkspace是要导出到的工作空间,这段代码默认将导出后的图层命名为与源图层一样

第二种情况:往SDE中某个要素集导入图层

  1. bool impLayerByNameIntoDataset(IName lName,IWorkspace inWorkspace,IFeatureDataset featureDataset)
  2. {
  3. try
  4. {
  5. //IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
  6. //IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;
  7. //Open input Featureclass to get field definitions.
  8. IFeatureClass inFeatureClass = (IFeatureClass)lName.Open();
  9. IFeatureClassName inFeatureClassName = (IFeatureClassName)lName;
  10. // Set out dataset and feature class names.
  11. IFeatureClassName outFeatureClassName = new FeatureClassNameClass();
  12. IDatasetName outDatasetName = (IDatasetName)outFeatureClassName;
  13. IDataset outWorkspaceDataset = (IDataset)featureDataset.Workspace;
  14. IWorkspaceName outWorkspaceName = (IWorkspaceName)outWorkspaceDataset.FullName;
  15. outDatasetName.WorkspaceName = outWorkspaceName;
  16. outDatasetName.Name = "SDE." + ((IDatasetName)lName).Name;
  17. //Validate the field names.
  18. IFieldChecker fieldChecker = new FieldCheckerClass();
  19. IFields outFeatureClassFields;
  20. IFields inFeatureClassFields = inFeatureClass.Fields;
  21. IEnumFieldError enumFieldError;
  22. fieldChecker.InputWorkspace = inWorkspace;
  23. fieldChecker.ValidateWorkspace = featureDataset.Workspace;
  24. // Validate the fields.
  25. fieldChecker.Validate(inFeatureClassFields,out enumFieldError,outoutFeatureClassFields);
  26. //featureDataset.CreateFeatureClass("SDE." + ((IDatasetName)lName).Name,outFeatureClassFields,ocDesc.InstanceCLSID,ocDesc.ClassExtensionCLSID,esriFeatureType.esriFTSimple,fcDesc.ShapeFieldName,"");
  27. // Set up the geometry definition.
  28. IField geometryField;
  29. geometryField = outFeatureClassFields.get_Field(outFeatureClassFields.FindField(inFeatureClass.ShapeFieldName));
  30. // Get the geometry field‘s geometry definition.
  31. IGeometryDef geometryDef = geometryField.GeometryDef;
  32. //Set up the IQueryFilter to convert all the features by leaving a blank WhereClause
  33. IQueryFilter qf = new QueryFilterClass();
  34. qf.WhereClause = "";
  35. //Load the feature class
  36. IFeatureDataConverter fctofc = new FeatureDataConverterClass();
  37. IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(inFeatureClassName,qf,(IFeatureDatasetName)featureDataset.FullName,outFeatureClassName,geometryDef,outFeatureClassFields,"",1000,0);
  38. return true;
  39. }
  40. catch (Exception ex)
  41. {
  42. MessageBox.Show(ex.Message);
  43. return false;
  44. }
  45. }

 

其中,lName是源图层的IName,inWorkspace是源图层所在的工作空间,featureDataset是目标数据集,这段代码默认将导出后的图层命名为与源图层一样

附:获取SDE中数据集(Dataset)的方法:

  1. //在SDE里查找名称为datasetName的Dataset
  2. IFeatureDataset outSet = null;
  3. IEnumDatasetName idNames = outSpace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
  4. IDatasetName idNames.Reset();
  5. fNameBase = idNames.Next();
  6. while (fNameBase != null)
  7. {
  8. if(fNameBase.Name == datasetName) outSet = (IFeatureDataset)((IName)fNameBase).Open();
  9. fNameBase = idNames.Next();
  10. }

 

其中outSpace为已经打开的SDE工作空间

将图层导入SDE

标签:

原文地址:http://www.cnblogs.com/cuizhiyuan/p/4599606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!