程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> ArcEngine c# 創建shp圖層,並將要素寫入圖層保存

ArcEngine c# 創建shp圖層,並將要素寫入圖層保存

編輯:C#入門知識

 


之前看了一位大俠的帖子,寫的是跟這個差不多的,所以,我就直接copy下來再vs中做了一個類。大家可以直接拿過去用。

 


using System;

using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;


namespace topo.com
{
    /// <summary>
    /// 導出緩沖分析之後出現JointCount>1的點位圖層
    /// </summary>
    public  class ExportToShp
    {
        public string shpPath;
        /// <summary>
        /// 保存輸出JointCount>1的點位圖層
        /// </summary>
        /// <param name="apFeatureClass"></param>
        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());
            }
        }


    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved