AE(C#)Shapefile导入mdb数据库

在CSDN这里得到过许多人提供的帮助,非常感激。一直想把自己写过的代码记录下来,跟各位交流,另一方面以备不时之需。最近在编写Shapefile导入数据库功能,发现在这里没找到多少这方面的知识。经过几天的搜寻,也终于完成了这个功能,可能还存在缺陷,不过程序运行情况良好,下面提供我的主要步骤和核心代码供各位Giser参考。

将Shapefile存入到个人地理数据库,我分为三个步骤:

1.创建个人地理数据库

2.创建要素数据集

3.将Shapefile导入到要素数据集

以下是三个主要方法

  1.         /// <summary>
  2.         /// 创建数据库
  3.         /// </summary>
  4.         /// <param name="mdbFolder"></param>
  5.         /// <param name="mdbName"></param>
  6.         /// <returns></returns>
  7. public IWorkspace CreateMdb(string mdbFolder, string mdbName)
  8. {
  9. IWorkspaceFactory pFtWsFct = new AccessWorkspaceFactory();
  10. IWorkspaceName workspaceName = pFtWsFct.Create(mdbFolder, mdbName, null, 0);
  11. IFeatureWorkspace pFeatureWorkSpace = (workspaceName as IName).Open() as IFeatureWorkspace;
  12. IWorkspace pWorkspace = (workspaceName as IName).Open() as IWorkspace;
  13. return pWorkspace;
  14. }
  1. /// <summary>
  2. // 创建要素数据集
  3. /// </summary>
  4. /// <param name="workspace"></param>
  5. /// <param name="code"></param>
  6. /// <param name="datasetName"></param>
  7. /// <returns></returns>
  8. public IFeatureDataset CreateFeatureClass(IWorkspace workspace, int code, string datasetName)
  9. {
  10. IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
  11. //创建一个要素集创建一个投影
  12. ISpatialReferenceFactory spatialRefFactory = new SpatialReferenceEnvironmentClass();
  13. ISpatialReference spatialReference = spatialRefFactory.CreateProjectedCoordinateSystem(code);
  14. //确定是否支持高精度存储空间
  15. Boolean supportsHighPrecision = false;
  16. IWorkspaceProperties workspaceProperties = (IWorkspaceProperties)workspace;
  17. IWorkspaceProperty workspaceProperty = workspaceProperties.get_Property
  18. (esriWorkspacePropertyGroupType.esriWorkspacePropertyGroup,
  19. (int)esriWorkspacePropertyType.esriWorkspacePropSupportsHighPrecisionStorage);
  20. if (workspaceProperty.IsSupported)
  21. {
  22. supportsHighPrecision = Convert.ToBoolean(workspaceProperty.PropertyValue);
  23. }
  24. //设置投影精度
  25. IControlPrecision2 controlPrecision = (IControlPrecision2)spatialReference;
  26. controlPrecision.IsHighPrecision = supportsHighPrecision;
  27. //设置容差
  28. ISpatialReferenceResolution spatialRefResolution = (ISpatialReferenceResolution)spatialReference;
  29. spatialRefResolution.ConstructFromHorizon();
  30. spatialRefResolution.SetDefaultXYResolution();
  31. ISpatialReferenceTolerance spatialRefTolerance = (ISpatialReferenceTolerance)spatialReference;
  32. spatialRefTolerance.SetDefaultXYTolerance();
  33. //创建要素集
  34. IFeatureDataset featureDataset = featureWorkspace.CreateFeatureDataset(datasetName, spatialReference);
  35. return featureDataset;
  36. }
  1. /// <summary>
  2. /// 获得参照投影的编码
  3. /// </summary>
  4. /// <param name="tFeatureLayer"></param>
  5. /// <returns></returns>
  6. public int getSpatialReferenceCode(IFeatureClass tFeatureClass)
  7. {
  8. IDataset dataset = tFeatureClass as IDataset;
  9. IGeoDataset geoDataset = (IGeoDataset)dataset;
  10. int code = geoDataset.SpatialReference.FactoryCode;
  11. return code;
  12. }
  1. /// <summary>
  2. /// 将Shapefile导入到数据库
  3. /// </summary>
  4. /// <param name="pFeaClass"></param>
  5. /// <param name="pWorkspace"></param>
  6. /// <param name="tFeatureClass"></param>
  7. private void importToDB(IFeatureClass pFeaClass, IWorkspace pWorkspace, IFeatureDataset tFeatureClass,string SHPName)
  8. {
  9. IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass();
  10. IObjectClassDescription objectClassDescription = featureClassDescription as IObjectClassDescription;
  11. IFields pFields = pFeaClass.Fields;
  12. IFieldChecker pFieldChecker = new FieldCheckerClass();
  13. IEnumFieldError pEnumFieldError = null;
  14. IFields vFields = null;
  15. pFieldChecker.ValidateWorkspace = pWorkspace as IWorkspace;
  16. pFieldChecker.Validate(pFields, out pEnumFieldError, out vFields);
  17. IFeatureWorkspace featureWorkspace = pWorkspace as IFeatureWorkspace;
  18. IFeatureClass sdeFeatureClass = null;
  19. if (sdeFeatureClass == null)
  20. {
  21. sdeFeatureClass = tFeatureClass.CreateFeatureClass(SHPName, vFields,
  22. objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID,
  23. pFeaClass.FeatureType , pFeaClass.ShapeFieldName, "");
  24. IFeatureCursor featureCursor = pFeaClass.Search(null, true);
  25. IFeature feature = featureCursor.NextFeature();
  26. IFeatureCursor sdeFeatureCursor = sdeFeatureClass.Insert(true);
  27. IFeatureBuffer sdeFeatureBuffer;
  28. while (feature != null)
  29. {
  30. sdeFeatureBuffer = sdeFeatureClass.CreateFeatureBuffer();
  31. IField shpField = new FieldClass();
  32. IFields shpFields = feature.Fields;
  33. for (int i = 0; i < shpFields.FieldCount; i++)
  34. {
  35. shpField = shpFields.get_Field(i);
  36. if (shpField.Name.Contains("Area") || shpField.Name.Contains("Leng") || shpField.Name.Contains("FID")) continue;
  37. int index = sdeFeatureBuffer.Fields.FindField(shpField.Name);
  38. if (index != -1)
  39. {
  40. sdeFeatureBuffer.set_Value(index, feature.get_Value(i));
  41. }
  42. }
  43. sdeFeatureCursor.InsertFeature(sdeFeatureBuffer);
  44. sdeFeatureCursor.Flush();
  45. feature = featureCursor.NextFeature();
  46. }
  47. featureCursor.Flush();
  48. Marshal.ReleaseComObject(feature);
  49. Marshal.ReleaseComObject(featureCursor);
  50. }
  51. }
  1. /// <summary>
  2. /// 导入到数据库
  3. /// </summary>
  4. /// <param name="sourceFeatureClass"></param>
  5. /// <param name="pWorkspace"></param>
  6. /// <param name="targetFeatureDataset"></param>
  7. private void importToDB(IFeatureClass sourceFeatureClass, IWorkspace pWorkspace, IFeatureDataset targetFeatureDataset,string SHPName)
  8. {
  9. IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass();
  10. IObjectClassDescription objectClassDescription = featureClassDescription as IObjectClassDescription;
  11. IFields pFields = sourceFeatureClass.Fields;
  12. IFieldChecker pFieldChecker = new FieldCheckerClass();
  13. IEnumFieldError pEnumFieldError = null;
  14. IFields vFields = null;
  15. pFieldChecker.ValidateWorkspace = pWorkspace as IWorkspace;
  16. pFieldChecker.Validate(pFields, out pEnumFieldError, out vFields);
  17. IFeatureWorkspace featureWorkspace = pWorkspace as IFeatureWorkspace;
  18. IFeatureClass sdeFeatureClass = null;
  19. if (sdeFeatureClass == null)
  20. {
  21. sdeFeatureClass = targetFeatureDataset.CreateFeatureClass(SHPName, vFields,
  22. objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID,
  23. sourceFeatureClass.FeatureType , sourceFeatureClass.ShapeFieldName, "");
  24. IFeatureCursor featureCursor = sourceFeatureClass.Search(null, true);
  25. IFeature feature = featureCursor.NextFeature();
  26. IFeatureCursor sdeFeatureCursor = sdeFeatureClass.Insert(true);
  27. IFeatureBuffer sdeFeatureBuffer;
  28. while (feature != null)
  29. {
  30. sdeFeatureBuffer = sdeFeatureClass.CreateFeatureBuffer();
  31. IField shpField = new FieldClass();
  32. IFields shpFields = feature.Fields;
  33. for (int i = 0; i < shpFields.FieldCount; i++)
  34. {
  35. shpField = shpFields.get_Field(i);
  36. if (shpField.Name.Contains("Area") || shpField.Name.Contains("Leng") || shpField.Name.Contains("FID")) continue;
  37. int index = sdeFeatureBuffer.Fields.FindField(shpField.Name);
  38. if (index != -1)
  39. {
  40. sdeFeatureBuffer.set_Value(index, feature.get_Value(i));
  41. }
  42. }
  43. sdeFeatureCursor.InsertFeature(sdeFeatureBuffer);
  44. sdeFeatureCursor.Flush();
  45. feature = featureCursor.NextFeature();
  46. }
  47. featureCursor.Flush();
  48. Marshal.ReleaseComObject(feature);
  49. Marshal.ReleaseComObject(featureCursor);
  50. }
  51. }


调用方法:

  1. public void startOutput()
  2. {
  3. IFeatureLayer mCphFeatureLayer = OpenShapeFile.openShapeFile(mCphPath);//这是获得要入库的shapefile,获取其FeatureLayer即可
  4. //1.创建数据库
  5. IWorkspace pWorkspace = CreateMdb(mDBPath, mDBName);//参数为mdb的目录路径和数据库名
  6. //2.创建要素数据集
  7. IFeatureClass pCphFeatureClass = mCphFeatureLayer.FeatureClass;
  8. int code = getSpatialReferenceCode(mCphFeatureLayer);//参照投影的代号
  9. string datasetName = pCphFeatureClass.AliasName;//要素数据集的名称
  10. IFeatureDataset pCphDataset = CreateFeatureClass(pWorkspace, code, datasetName);
  11. //3.导入SHP到要素数据集(
  12. importToDB(pCphFeatureClass, pWorkspace, pCphDataset, pCphFeatureClass.AliasName);
  13. }


至此,SHP入库完成。由于网上代码比较少,费了较长时间在这个功能上,中间还出了很多错误。借鉴我的错误在此提醒一下大家:

1.导入到库里的Shapefile名字不能以数字开头(上课没认真听,我竟然不知道有这个,害我瞎折腾了一番),不然导出来的图层在放大到一定范围后会报错。

2.使用线程运行的话,在线程外传递IFeatureLayer等参数到线程内操作是不行的,不熟悉线程,不知道是ArcEngine的问题还是线程操作不当的问题,在set_Value的时候,一直报“The coordinates or measures are out of bounds.”的错误。后来受同学指点,线程参数只传递string类型,就不报错了。

这两个问题让我折腾了好几天,在这里给大家提醒一下了。当然大牛就忽略吧,或者指点下小弟为什么会有这两个问题,小弟会感激不尽的。

 希望这些代码能帮助到有需要的人,在此也感谢CSDN各位大牛的帮助。第一次写博客,就到这里吧

(0)

相关推荐