• 导入shape文件到SDE数据库


    /**

           * pDataSet:要导出的数据库的数据集,如果直接导入到数据库而不是数据集,则使用强制转换IWorkspace-->IDataset

           * strFeatFileDir:要导入的shape文件所在目录

           * strFeatFileName:要导入的shape文件名

           * isWorkspace:标识是导入数据库还是数据集

           *strOutName:存储于数据库的文件名  

           *failInfo:导入失败文件名  

           *errorDetail:错误信息  

           *skipInfo:发现重名情况跳过文件名

         **/

             public void FeatureClassToFeatureClass(IDataset pDataSet, string strFeatFileDir, string strFeatFileName, string strOutName, bool isWorkspace, ref string failInfo, ref string errorDetail, ref string skipInfo)

             {

                 try

                 {

                     IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();

                     IFeatureWorkspace pFeatureWK = (IFeatureWorkspace)pWSF.OpenFromFile(strFeatFileDir, 0);

                     IFeatureClass pInFeatureClass = pFeatureWK.OpenFeatureClass(strFeatFileName);

                     if (pInFeatureClass == null || pDataSet == null)

                     {

                         if (failInfo == "")

                         {

                             failInfo = strFeatFileDir + "\\" + strFeatFileName;

                             errorDetail = "错误信息:" + strFeatFileDir + "是空的矢量工作空间。";

                         }

                         else

                         {

                             failInfo += ";" + strFeatFileDir + "\\" + strFeatFileName;

                             errorDetail += ";" + "错误信息:" + strFeatFileDir + "是空的矢量工作空间。";

                         }

                         return;

                     }

                     IFeatureClassName pInFeatureclassName;

                     IDataset pIndataset = (IDataset)pInFeatureClass;

                     pInFeatureclassName = (IFeatureClassName)pIndataset.FullName;

                     //如果名称已存在

                     IWorkspace2 pWS2 = null;

                     if (isWorkspace)

                         pWS2 = pDataSet as IWorkspace2;

                     else

                         pWS2 = pDataSet.Workspace as IWorkspace2;

                     if (pWS2.get_NameExists(esriDatasetType.esriDTFeatureClass, strOutName))

                     {

                         DialogResult result;

                         result = System.Windows.Forms.MessageBox.Show(null, "矢量文件名   " + strOutName + "  在数据库中已存在!" + "\r是否覆盖?", "相同文件名", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                         //覆盖原矢量要素

                         if (result == DialogResult.Yes)

                         {

                             IFeatureWorkspace pFWS = (IFeatureWorkspace)pWS2;

                             IDataset pDataset = pFWS.OpenFeatureClass(strOutName) as IDataset;

                             pDataset.Delete();

                             pDataset = null;

                         }

                         else if (result == DialogResult.No)

                         {

                             //不覆盖,则退出for循环,忽略这个要素,转入下一个要素的导入

                             if (skipInfo == "")

                                 skipInfo = strFeatFileDir + "\\" + strFeatFileName;

                             else

                                 skipInfo += ";" + strFeatFileDir + "\\" + strFeatFileName;

                             return;

                         }

                     }

                     IFields pInFields, pOutFields;

                     IFieldChecker pFieldChecker = new FieldCheckerClass();

                     IEnumFieldError pError;

                     pInFields = pInFeatureClass.Fields;

                     pFieldChecker.Validate(pInFields, out pError, out pOutFields);

                     IField geoField = null;

                     for (int i = 0; i < pOutFields.FieldCount; i++)

                     {

                         IField pField = pOutFields.get_Field(i);

                         if (pField.Type == esriFieldType.esriFieldTypeOID)

                         {

                             IFieldEdit pFieldEdit = (IFieldEdit)pField;

                             pFieldEdit.Name_2 = pField.AliasName;

                         }

                         if (pField.Type == esriFieldType.esriFieldTypeGeometry)

                         {

                             geoField = pField;

                             break;

                         }

                     }

                     IGeometryDef geometryDef = geoField.GeometryDef;

                     IFeatureDataConverter one2another = new FeatureDataConverterClass();

                     IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();

                     IDatasetName pOutDatasetName = (IDatasetName)pOutFeatureClassName;

                     if (isWorkspace)

                         pOutDatasetName.WorkspaceName = (IWorkspaceName)pDataSet.FullName;

                     else

                         pOutDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)pDataSet.Workspace).FullName;

                     pOutDatasetName.Name = strOutName;

                     if (isWorkspace)

                     {

                         one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, geometryDef,

                         pOutFields, "", 1000, 0);

                     }

                     else

                     {

                         IFeatureDataset pFeatDS = (IFeatureDataset)pDataSet;

                         IFeatureDatasetName pOutFeatDSName = pFeatDS.FullName as IFeatureDatasetName;

                         one2another.ConvertFeatureClass(pInFeatureclassName, null, pOutFeatDSName, pOutFeatureClassName, geometryDef,

                         pOutFields, "", 1000, 0);

                         pOutFeatDSName = null;

                         pFeatDS = null;

                     }

                 }

                 catch (Exception ex)

                 {

                     if (failInfo == "")

                     {

                         failInfo = strFeatFileDir + "\\" + strFeatFileName;

                         errorDetail = "错误信息:" + ex.Message;

                     }

                     else

                     {

                         failInfo += ";" + strFeatFileDir + "\\" + strFeatFileName;

                         errorDetail += ";" + "错误信息:" + ex.Message;

                     }

                     Console.WriteLine(ex.Message);

                     return;

                 }

             }

    转载自:http://www.cnblogs.com/bluemaplestudio/archive/2010/04/13/1710747.html 蓝枫工作室

  • 相关阅读:
    可变参数宏...和__VA_ARGS__
    gitolite
    CentOS 6.4 搭建git 服务器
    github简单使用教程
    【Github教程】史上最全github使用方法:github入门到精通
    redis安装与参数说明
    如何解决redis高并发客户端频繁time out?
    Redis配置文件参数说明
    Redis系列-存储篇hash主要操作函数小结
    Redis常用命令解析——INFO, MONITOR, SLOWLOG
  • 原文地址:https://www.cnblogs.com/514687800/p/3043659.html
Copyright © 2020-2023  润新知