1. 程式人生 > >C#+ArcEngine:txt點資料轉Shp向量資料

C#+ArcEngine:txt點資料轉Shp向量資料

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.Controls;

using ESRI.ArcGIS.DataSourcesFile;

using ESRI.ArcGIS.Geodatabase;

using ESRI.ArcGIS.Geometry;

namespace AE簡單開發

{

    publicpartialclassForm_TxtToShpPoint :Form

    {

        publicForm_TxtToShpPoint()

        {

           ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

           InitializeComponent();

        }

        //

選擇Txt檔案

        privatevoidbtn_TxtPath_Click(object sender,EventArgs e)

        {

            OpenFileDialog xjTxtOpenFileDialog =newOpenFileDialog();

           xjTxtOpenFileDialog.Multiselect = false;

           xjTxtOpenFileDialog.Title = "開啟txt座標檔案";

           xjTxtOpenFileDialog.Filter = "txt座標檔案(*.txt

|*.txt";

            if(xjTxtOpenFileDialog.ShowDialog() ==DialogResult.OK)

            {

               txt_TxtPath.Text = xjTxtOpenFileDialog.FileName;

            }

        }

        //Shp向量點儲存路徑

        privatevoidbtn_ShpPath_Click(object sender,EventArgs e)

        {

            SaveFileDialogxjShpSaveFileDialog =newSaveFileDialog();

           xjShpSaveFileDialog.Filter = "Shape檔案(*.shp|*.shp";

            if(File.Exists(txt_TxtPath.Text))

            {

               xjShpSaveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(txt_TxtPath.Text);

            }

            if(xjShpSaveFileDialog.ShowDialog() ==DialogResult.OK)

            {

               txt_ShpPath.Text = xjShpSaveFileDialog.FileName;

            }

        }

        //顯示儲存

        //檢查資料和路徑

        privateboolCheck()

        {

            if(txt_TxtPath.Text =="" || !File.Exists(txt_TxtPath.Text))

            {

               MessageBox.Show("資料無效哇,重選","提示",MessageBoxButtons.OK);

               return false;

            }

            if(txt_ShpPath.Text =="" ||System.IO.Path.GetExtension(txt_ShpPath.Text).ToLower()!=".shp")

            {

               MessageBox.Show("Shp向量點儲存路徑無效哇,重選","提示",MessageBoxButtons.OK);

               return false;

            }

            returntrue;

        }

        //結構體

        structPoint

        {

            publicstringName;

            publicdoubleX;

            publicdoubleY;

        }

        List<string>xjColumn =newList<string>();

        //獲取點資料

        privateList<Point>GetPoint(string surveyDataFullName)

        {

            try

            {

               List<Point>xjList =newList<Point>();

               char[] xjchar = newchar[]{ ',', ' ','\t' };  //常用的分隔符為逗號、空格、制位符

               //讀取

               FileStream xjFileStream =newFileStream(surveyDataFullName,FileMode.Open);

               StreamReader xjStreamReader =newStreamReader(xjFileStream,Encoding.Default);

               string xjstringLine =xjStreamReader.ReadLine();

                if(xjstringLine !=null)

               {

                   string[] xjstrArray =xjstringLine.Split(xjchar);

                   if (xjstrArray.Length > 0)

                   {

                        for(inti = 0; i < xjstrArray.Length; i++)

                        {

                           xjColumn.Add(xjstrArray[i]);

                        }

                   }

                   while ((xjstringLine =xjStreamReader.ReadLine()) !=null)

                   {

                        //點資訊的讀取

                        xjstrArray =xjstringLine.Split(xjchar);

                        Point xjPoint =newPoint();

                        xjPoint.Name =xjstrArray[0].Trim();

                        xjPoint.X = Convert.ToDouble(xjstrArray[1]);

                        xjPoint.Y = Convert.ToDouble(xjstrArray[2]);

                        xjList.Add(xjPoint);

                   }

               }

               else

               {

                   return null;

               }

               xjStreamReader.Close();

               return xjList;

            }

            catch(Exception ex)

            {

               MessageBox.Show(ex.Message);

               return null;

            }

        }

        //建立Shp向量圖層

        private IFeatureLayer CreateShpFromPoints(List<Point> xjPointList,stringxjFilePath)

        {

            intindex = xjFilePath.LastIndexOf('\\');

            stringxjFolder = xjFilePath.Substring(0, index);

            stringxjShapeName = xjFilePath.Substring(index + 1);

            IWorkspaceFactory xjWsF =newShapefileWorkspaceFactoryClass();

            IFeatureWorkspace xjFWs = (IFeatureWorkspace)xjWsF.OpenFromFile(xjFolder,0);

            IFields xjFields =newFieldsClass();

            IFieldsEdit xjFieldsEdit;

           xjFieldsEdit = (IFieldsEdit)xjFields;

            IField xjField =newFieldClass();

            IFieldEdit xjFieldEdit = (IFieldEdit)xjField;

           xjFieldEdit.Name_2 = "Shape";

           xjFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

            IGeometryDef xjGeometryDef =newGeometryDefClass();

            IGeometryDefEdit xjGDefEdit = (IGeometryDefEdit)xjGeometryDef;

           xjGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

            //定義座標系

            ISpatialReferenceFactory pSRF =newSpatialReferenceEnvironmentClass();

            ISpatialReference pSpatialReference =pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);

           xjGDefEdit.SpatialReference_2 = pSpatialReference;

           xjFieldEdit.GeometryDef_2 = xjGeometryDef;

           xjFieldsEdit.AddField(xjField);

            IFeatureClass xjFeatureClass;

           xjFeatureClass = xjFWs.CreateFeatureClass(xjShapeName, xjFields,null,null,esriFeatureType.esriFTSimple,"Shape","");

            IPoint xjPoint =newPointClass();

            for(intj = 0; j < xjPointList.Count; j++)

            {

               xjPoint.X = xjPointList[j].X;

               xjPoint.Y = xjPointList[j].Y;

               IFeature xjFeature =xjFeatureClass.CreateFeature();

               xjFeature.Shape = xjPoint;

               xjFeature.Store();

            }

            IFeatureLayer xjFeatureLayer =newFeatureLayerClass();

           xjFeatureLayer.Name = xjShapeName;

           xjFeatureLayer.FeatureClass = xjFeatureClass;

            returnxjFeatureLayer;

        }

        //單擊顯示儲存

        privatevoidbtn_ShowSave_Click(object sender,EventArgs e)

        {

            if(Check())

            {

               List<Point>xjPointList = GetPoint(txt_TxtPath.Text);

               if (xjPointList ==null)

               {

                   MessageBox.Show("選擇檔案是空的,轉毛線啊");

               }

               else

                {

                   IFeatureLayer pFeatureLayer =CreateShpFromPoints(xjPointList, txt_ShpPath.Text);

                   ShpMapControl.Map.AddLayer(pFeatureLayer);

               }

            }

        }

    }

}

VS2012+ArcEngine10.2具體窗體+程式碼見:點選開啟連結

VS2010+ArcEngine10.1具體窗體+程式碼見:點選開啟連結