FeatureLayer到本地shapefile文件

今天给大家介绍一段代码,可以将程序中的FeatureLayer转到本地shapefile文件,方便数据输出使用。

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;

//sFileName:shapefile保存的路径及名称

//pFC:要输出FeatureLayer的FeatureClass属性

public static void OperateConvertToShape(string sFileName, IFeatureClass pFC)

{

     try

    {

       string str = sFileName;

       string str2 = str.Substring(str.LastIndexOf(@"\") + 1);

       string str3 = str.Substring(0, (str.Length - str2.Length) - 1);

       IPropertySet set = new PropertySetClass();

       set.SetProperty("DATABASE", str3);

       IWorkspaceName pWorkSpaceName = new WorkspaceNameClass();

       pWorkSpaceName.ConnectionProperties = set;

       pWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileWorkspaceFactory.1";

       IFeatureClassName pFeatureClassName = new FeatureClassNameClass();

       IDatasetName pDatasetName = pFeatureClassName as IDatasetName;

       pDatasetName.Name = str2;

       pDatasetName.WorkspaceName = pWorkSpaceName;

       IDataset dataset = pFC as IDataset;

       IWorkspaceName sWorkSpaceName = new WorkspaceNameClass();

       sWorkSpaceName.ConnectionProperties = dataset.Workspace.ConnectionProperties;

       IWorkspaceFactory pFWorkFactory =dataset.Workspace.WorkspaceFactory;

       if (pFWorkFactory .GetType().Name.ToUpper().IndexOf("ACCESS")>-1)

       {

          sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory.1";

       }

       else if (pFWorkFactory.GetType().Name.ToUpper().IndexOf("FILEGDB") > -1)

      {

         sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory.1";

      }

      else if (pFWorkFactory.GetType().Name.ToUpper().IndexOf("SDE") > -1)

      {

         sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.SdeWorkspaceFactory.1";

      }

      IFeatureClassName sFeatureClassName = new FeatureClassNameClass();

      IDatasetName sDatasetName = sFeatureClassName as IDatasetName;

      sDatasetName.Name = dataset.Name;

      sDatasetName.WorkspaceName = sWorkSpaceName;

      new FeatureDataConverterClass().ConvertFeatureClass(sFeatureClassName, null, null, pFeatureClassName, null, null, "", 0x3e8, 0);

      MessageBox.Show("shape文件输出完毕!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

     }

     catch (Exception exception)

     {

        MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

     }

}

posted @ 2012-05-23 17:18  LinHugh  阅读(373)  评论(0编辑  收藏  举报