由坐标文件生成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();
        }
    }
}

三、效果图


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

目录
相关文章
|
编解码
没有地理参考的图层添加坐标系:ENVI
本文介绍基于ENVI软件,对不含有任何地理参考信息的栅格遥感影像添加地理坐标系或投影坐标系等地理参考信息的方法~
190 4
没有地理参考的图层添加坐标系:ENVI
|
图形学
GLTF编辑器如何快速重置模型原点
模型原点是一个虚拟三维空间中的参考点,它在三维建模中具有定位、对齐、变换、导出、动画和约束等多个重要作用。
238 0
|
4月前
|
存储 Cloud Native Linux
OpenCV鼠标操作(画红色方框截取图像)
OpenCV鼠标操作(画红色方框截取图像)
|
5月前
|
定位技术
ArcGIS中ArcMap创建渔网Create Fishnet:生成指定大小的格网矢量文件
ArcGIS中ArcMap创建渔网Create Fishnet:生成指定大小的格网矢量文件
182 1
|
5月前
|
存储 定位技术
ArcGIS中ArcMap导入mxd地图文档文件出现红色感叹号、地图空白的解决
ArcGIS中ArcMap导入mxd地图文档文件出现红色感叹号、地图空白的解决
154 1
|
5月前
[Qt5] 矩形、圆和多边形ROI区域的交互(List View列表视图,halcon实现)
[Qt5] 矩形、圆和多边形ROI区域的交互(List View列表视图,halcon实现)
132 0
|
12月前
|
人工智能 定位技术
如何将国家地图服务系统的EPS地图转为shp图层
如何将国家地图服务系统的EPS地图转为shp图层
1149 0
|
JSON 数据格式
使用Fiona创建Shapefile矢量数据
使用Fiona写入Shapefile数据,主要是构建一个Schema,然后将空间对象转为GeoJSON的形式进行写入。 这个Schema是一个字典结构,定义了Geometry的类型,属性字段的名称及其类型。
131 0
ArcMap转换shp坐标系
使用arcmap转换文件的坐标系
210 0
Blender突然无法平移视图
Blender突然无法平移视图
2078 0