1. 程式人生 > >ArcEngine c# 建立shp圖層,並將要素寫入圖層儲存

ArcEngine c# 建立shp圖層,並將要素寫入圖層儲存

建立Shp圖層程式碼,可以直接拿過去用。

    using System;

    using System.Collections.Generic;

    using System.Text;

    using ESRI.ArcGIS.Geodatabase;

    using System.Windows.Forms;

    using ESRI.ArcGIS.DataSourcesFile;

    namespace topo.com

    {

    ///

    /// 匯出緩衝分析之後出現JointCount>1的點點陣圖層

    ///

    public class ExportToShp

    {

    public string shpPath;

    ///

    /// 儲存輸出JointCount>1的點點陣圖層

    ///

    ///

    public void ExportFeatureClassToShp(IFeatureClass apFeatureClass)

    {

    if (apFeatureClass == null)

    {

    MessageBox.Show("請選擇", "系統提示");

    return;

    }

    //呼叫儲存檔案函式

    SaveFileDialog sa = new SaveFileDialog();

    sa.Filter = "SHP檔案(.shp)|*.shp";

    sa.ShowDialog();

    sa.CreatePrompt = true;

    string ExportShapeFileName = sa.FileName;

    // string StrFilter = "SHP檔案(.shp)|*.shp";

    // string ExportShapeFileName = SaveFileDialog(StrFilter);

    if (ExportShapeFileName == "")

    return;

    string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ExportShapeFileName);

    string ExportFilePath = System.IO.Path.GetDirectoryName(ExportShapeFileName);

    shpPath = ExportFilePath + "\\" + ExportFileShortName + "\\" + ExportFileShortName + ".shp";

    //設定匯出要素類的引數

    IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();

    IDataset pOutDataset = (IDataset)apFeatureClass;

    pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;

    //建立一個輸出shp檔案的工作空間

    IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();

    IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();

    pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);

    //建立一個要素集合

    IFeatureDatasetName pInFeatureDatasetName = null;

    //建立一個要素類

    IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();

    IDatasetName pInDatasetClassName;

    pInDatasetClassName = (IDatasetName)pInFeatureClassName;

    pInDatasetClassName.Name = ExportFileShortName;//作為輸出引數

    pInDatasetClassName.WorkspaceName = pInWorkspaceName;

    //通過FIELDCHECKER檢查欄位的合法性,為輸出SHP獲得欄位集合

    long iCounter;

    IFields pOutFields, pInFields;

    IFieldChecker pFieldChecker;

    IField pGeoField;

    IEnumFieldError pEnumFieldError = null;

    pInFields = apFeatureClass.Fields;

    pFieldChecker = new FieldChecker();

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

    //通過迴圈查詢幾何欄位

    pGeoField = null;

    for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)

    {

    if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)

    {

    pGeoField = pOutFields.get_Field((int)iCounter);

    break;

    }

    }

    //得到幾何欄位的幾何定義

    IGeometryDef pOutGeometryDef;

    IGeometryDefEdit pOutGeometryDefEdit;

    pOutGeometryDef = pGeoField.GeometryDef;

    //設定幾何欄位的空間參考和網格

    pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;

    pOutGeometryDefEdit.GridCount_2 = 1;

    pOutGeometryDefEdit.set_GridSize(0, 1500000);

    try

    {

    //開始匯入

    IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();

    pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);

    MessageBox.Show("匯出成功", "系統提示");

    }

    catch (Exception ex)

    {

    MessageBox.Show("the following exception occurred:" + ex.ToString());

    }

    }

    }

    }

                                                 

                                                       2016-9-29  水仙花