由坐标文件生成shp图层

一、坐标文件

二、实现代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;

namespace CreateShapefileFromText
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private AxMapControl buddyMap;

        /// <summary>
        /// 关联地图控件
        /// </summary>
        public AxMapControl BuddyMap
        {
            get { return buddyMap; }
            set { buddyMap = value; }
        }

        string shapeFileFullName = string.Empty;
        string surveyDataFullName = string.Empty;

        List<string> pColumns = new List<string>();
        List<CPoint> pCPointList = new List<CPoint>();

        struct CPoint
        {
            public double x;
            public double y;
            public string name;
        }


        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog pOFD = new OpenFileDialog();
            pOFD.Multiselect = false;
            pOFD.Title = "打开测量数据坐标文件:";
            pOFD.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            pOFD.Filter = "测量坐标文件(*.TXT)|*.TXT";
            if (pOFD.ShowDialog() == DialogResult.OK)
            {
                surveyDataFullName = pOFD.FileName;
                this.textBox1.Text = surveyDataFullName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Shape文件(*.shp)|*.shp";
            DialogResult dialogresult = saveFileDialog.ShowDialog();
            if (dialogresult == DialogResult.OK)
            {
                shapeFileFullName = saveFileDialog.FileName;
            }
            else
            {
                shapeFileFullName = null;
                return;
            }
            this.textBox2.Text = shapeFileFullName;
        }
        
        /// <summary>
        /// 从测量数据中获取所有点
        /// </summary>
        /// <param name="surveyDataFullName">测量数据路径</param>
        /// <returns>获取的点</returns>
        private List<CPoint> GetAllPoint(string surveyDataFullName)
        {
            List<CPoint> pList = new List<CPoint>();
            try
            {           
                if (surveyDataFullName == null || surveyDataFullName == string.Empty)
                {
                    MessageBox.Show("选择野外测量数据!");
                    return null;
                }
                if (!System.IO.File.Exists(surveyDataFullName))
                {
                    MessageBox.Show("野外测量数据不存在!");
                }

                string strLine;
                char[] charArray = new char[] { ',' };
                string[] strArray;
                System.IO.FileStream aFile = new System.IO.FileStream(surveyDataFullName,
                    FileMode.Open);
                StreamReader sr = new StreamReader(aFile, Encoding.Default);
                strLine = sr.ReadLine();
                strArray = strLine.Split(charArray);
                if (strArray.Length > 0)
                {
                    for (int x = 0; x < strArray.Length; x++)
                    {
                        pColumns.Add(strArray[x]);
                    }
                }
                else
                {
                    return null;
                }

                strLine = sr.ReadLine();
                while (strLine != null)
                {
                    strArray = strLine.Split(charArray);
                    CPoint pCpoint = new CPoint();
                    pCpoint.x = Convert.ToDouble(strArray[0]);
                    pCpoint.y = Convert.ToDouble(strArray[1]);
                    pCpoint.name = strArray[2].Trim();
                    pList.Add(pCpoint);
                    strLine = sr.ReadLine();
                }
                sr.Close();                

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return pList;
        }
        
        /// <summary>
        /// 通过点创建shp文件
        /// </summary>
        /// <param name="outfileNamePath">输出文件路径</param>
        /// <returns>创建的shp图层</returns>
        private IFeatureLayer CreateShpFromPoints(string outfileNamePath)
        {
            int index = outfileNamePath.LastIndexOf('\\');
            string folder = outfileNamePath.Substring(0, index);
            shapeFileFullName = outfileNamePath.Substring(index + 1);
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFWS = (IFeatureWorkspace)pWSF.OpenFromFile(folder, 0);

            if(File.Exists(outfileNamePath))
            {
                IFeatureClass featureClass = pFWS.OpenFeatureClass(shapeFileFullName);
                IDataset pDataset = (IDataset)featureClass;
                pDataset.Delete();
            }

            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit;
            pFieldsEdit = (IFieldsEdit)pFields;

            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometryDef = new GeometryDefClass();
            IGeometryDefEdit pGDefEdit = (IGeometryDefEdit)pGeometryDef;
            pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = pColumns[2];
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            IFeatureClass pFeatureClass;
            pFeatureClass = pFWS.CreateFeatureClass(shapeFileFullName, pFields, null, null,
                esriFeatureType.esriFTSimple, "Shape", "");

            List<string> pBuildingList = new List<string>();

            for (int i = 0; i < pCPointList.Count; i++)
            {
                if(pBuildingList.Contains(pCPointList[i].name.Trim()) == false)
                {
                    pBuildingList.Add(pCPointList[i].name.Trim());
                }
            }

            for (int i = 0; i < pBuildingList.Count; i++)
            {
                IPointCollection pPointColl = new PolygonClass();
                object o = Type.Missing;

                for (int j = 0; j < pCPointList.Count; j++)
                {
                    if (pCPointList[j].name.Trim() == pBuildingList[i].Trim())
                    {
                        IPoint pPoint = new PointClass();
                        pPoint.X = pCPointList[j].x;
                        pPoint.Y = pCPointList[j].y;
                        pPointColl.AddPoint(pPoint, ref o, ref o);
                    }
                }
                if (pPointColl.PointCount > 0)
                {
                    IClone pClone = pPointColl.get_Point(0) as IClone;
                    IPoint pEndPoint = pClone.Clone() as IPoint;
                    pPointColl.AddPoint(pEndPoint, ref o, ref o);
                }

                IFeature pFeature = pFeatureClass.CreateFeature();
                pFeature.Shape = pPointColl as IPolygon;
                pFeature.Store();
                pFeature.set_Value(pFeature.Fields.FindField(pColumns[2]),
                    pBuildingList[i].Trim());
                pFeature.Store();
            }

            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            return pFeatureLayer;
            
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            pCPointList = this.GetAllPoint(surveyDataFullName);
            IFeatureLayer pFeatureLayer = CreateShpFromPoints(shapeFileFullName);
            pFeatureLayer.Name = "建筑物";
            //this.buddyMap.Map.AddLayer(pFeatureLayer);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

三、效果图

转载自:https://blog.csdn.net/foreverling/article/details/34423583

You may also like...