AE:加载文本数据

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;


using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geometry;


#region 加载文本文件

        string DataFullName; //文件路径

        private void AddtxtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开文件
            OpenFileDialog pOFD = new OpenFileDialog
            {
                Multiselect = false,
                Title = "打开文本文件",
                InitialDirectory = Directory.GetCurrentDirectory(),
                Filter = "文本文件(*.txt)|*.txt"
            };

            if (pOFD.ShowDialog() == DialogResult.OK)
            {
               DataFullName = pOFD.FileName;
            }
            else
            {
                return;
            }

            List<Point> pList = GetPoints(DataFullName);
            if (pList == null)
            {
                MessageBox.Show("所选文件为空!");
                return;
            }
            else
            {
                IFeatureLayer pfeatureLayer = CreateShpFromPoints(pList);
                axMapControl1.Map.AddLayer(pfeatureLayer);
                EagleEye(); //同步鹰眼
            }
          
        }
        
        //创建点结构
        struct Point
        {
            public string name;
            public double x;
            public double y;
        }
        //创建存储点的列表
        private List<Point> GetPoints(string DataFullName)
        {
            try
            {
                List<Point> pList = new List<Point>();

                //定义常用分割字符数组
                char[] charArray = new char[] { ',', ' ', '\t', ',' };

                //文本信息读取
                System.IO.FileStream fs = new System.IO.FileStream(DataFullName, FileMode.Open);
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                string line;
                string[] strArray;
                while ((line=sr.ReadLine())!=null)
                {
                   strArray = line.Split(charArray);
                    Point point = new Point
                    {
                        name = strArray[0],
                        x = double.Parse(strArray[1]),
                        y = double.Parse(strArray[2])
                    };
                    pList.Add(point);
                }
                return pList;  //返回列表
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

        }

        //根据点坐标创建shapefile
        private IFeatureLayer CreateShpFromPoints(List<Point> pList)
        {
            //定义字段集合
            IFields fields = new FieldsClass();
            IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

            //定义单个字段
            IField field = new FieldClass();
            IFieldEdit fieldEdit = (IFieldEdit)field;
            fieldEdit.Name_2 = "shape";
            fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

            //定义图层几何类型
            IGeometryDef geometryDef = new GeometryDefClass();
            IGeometryDefEdit defEdit = (IGeometryDefEdit)geometryDef;
            defEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

            //定义坐标系
            ISpatialReferenceFactory spatialF = new SpatialReferenceEnvironmentClass();
            ISpatialReference spatialReference = spatialF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);

            defEdit.SpatialReference_2 = spatialReference;
            fieldEdit.GeometryDef_2 = geometryDef;
            fieldsEdit.AddField(field); //添加字段

            //创建要素类
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFWS = (IFeatureWorkspace)pWSF.OpenFromFile("./", 0); //将shp暂存在刚当前文件加下
            IFeatureClass pfeatureClass=pFWS.CreateFeatureClass("暂存数据", fields,null,null,
                esriFeatureType.esriFTSimple,"Shape","");
            IPoint pPoint = new PointClass();
            for (int i = 0; i < pList.Count; i++)
            {
                pPoint.X = pList[i].x;
                pPoint.Y = pList[i].y;
                IFeature pfeature = pfeatureClass.CreateFeature(); //创建要素
                pfeature.Shape = pPoint;
                pfeature.Store(); //保存
            }

            //创建要素图层
            IFeatureLayer pfeatureLayer = new FeatureLayerClass();
            pfeatureLayer.Name = System.IO.Path.GetFileName(DataFullName);
            pfeatureLayer.FeatureClass = pfeatureClass;
            return pfeatureLayer;
        }

        #endregion

 参考:ArcGIS Engine 地理信息系统开发教程:基于C#.NET/牟乃夏等

上一篇:A-ASSOCIATE-RJ[result: 1 - rejected-permanent, source: 1 - service-user, reason: 3 - calling-AE-titl


下一篇:源码时代前端干货分享| AE如何实现文字消散效果?看这一篇就够了!