zoukankan      html  css  js  c++  java
  • AE开发之txt转shp

    实现坐标txt文件转shp点集数据文件的窗体Form

    txt格式为:首行为“id,x,y”

    第二行开始输入具体数值:id,x,y(x,y为具体的xy坐标)

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using NewDistrict;
     10 using System.IO;
     11 
     12 using System.Threading.Tasks;
     13 using ESRI.ArcGIS.Carto;
     14 using ESRI.ArcGIS.Controls;
     15 using ESRI.ArcGIS.DataSourcesFile;
     16 using ESRI.ArcGIS.Geodatabase;
     17 using ESRI.ArcGIS.Geometry;
     18 
     19 namespace SignalDeal
     20 {
     21     public partial class Formtxt2shp : Form
     22     {
     23         public Formtxt2shp()
     24         {
     25             ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
     26             InitializeComponent();
     27         }
     28 
     29 
     30         //选择Txt文件
     31         private void btn_TxtPath_Click(object sender, EventArgs e)
     32         {
     33             OpenFileDialog xjTxtOpenFileDialog = new OpenFileDialog();
     34             xjTxtOpenFileDialog.Multiselect = false;
     35             xjTxtOpenFileDialog.Title = "打开txt坐标文件";
     36             xjTxtOpenFileDialog.Filter = "txt坐标文件(*.txt)|*.txt";
     37             if (xjTxtOpenFileDialog.ShowDialog() == DialogResult.OK)
     38             {
     39                 txt_TxtPath.Text = xjTxtOpenFileDialog.FileName;
     40             }
     41         }
     42 
     43         //Shp矢量点保存路径
     44         private void btn_ShpPath_Click(object sender, EventArgs e)
     45         {
     46             SaveFileDialog xjShpSaveFileDialog = new SaveFileDialog();
     47             xjShpSaveFileDialog.Filter = "Shape文件(*.shp)|*.shp";
     48             if (File.Exists(txt_TxtPath.Text))
     49             {
     50                 xjShpSaveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(txt_TxtPath.Text);
     51             }
     52             if (xjShpSaveFileDialog.ShowDialog() == DialogResult.OK)
     53             {
     54                 txt_ShpPath.Text = xjShpSaveFileDialog.FileName;
     55             }
     56         }
     57 
     58 
     59         //显示保存
     60         //检查数据和路径
     61         private bool Check()
     62         {
     63             if (txt_TxtPath.Text == "" || !File.Exists(txt_TxtPath.Text))
     64             {
     65                 MessageBox.Show("数据无效,重选", "提示", MessageBoxButtons.OK);
     66                 return false;
     67             }
     68             if (txt_ShpPath.Text == "" || System.IO.Path.GetExtension(txt_ShpPath.Text).ToLower() != ".shp")
     69             {
     70                 MessageBox.Show("Shp矢量点保存路径无效,重选", "提示", MessageBoxButtons.OK);
     71                 return false;
     72             }
     73             return true;
     74         }
     75         //结构体
     76         struct Point
     77         {
     78             public string Name;
     79             public double X;
     80             public double Y;
     81         }
     82         List<string> xjColumn = new List<string>();
     83         //获取点数据
     84         private List<Point> GetPoint(string surveyDataFullName)
     85         {
     86                 List<Point> xjList = new List<Point>();
     87                 char[] xjchar = new char[] { ',', ' ', '	' };  //常用的分隔符为逗号、空格、制位符
     88                 //读取
     89                 FileStream xjFileStream = new FileStream(surveyDataFullName, FileMode.Open);
     90                 StreamReader xjStreamReader = new StreamReader(xjFileStream, Encoding.Default);
     91                 string xjstringLine = xjStreamReader.ReadLine();
     92                 if (xjstringLine != null)
     93                 {
     94                     string[] xjstrArray = xjstringLine.Split(xjchar);
     95                     if (xjstrArray.Length > 0)
     96                     {
     97                         for (int i = 0; i < xjstrArray.Length; i++)
     98                         {
     99                             xjColumn.Add(xjstrArray[i]);
    100                         }
    101                     }
    102 
    103                     while ((xjstringLine = xjStreamReader.ReadLine()) != null)
    104                     {
    105                         //点信息的读取
    106                         xjstrArray = xjstringLine.Split(xjchar);
    107                         Point xjPoint = new Point();
    108                         xjPoint.Name = xjstrArray[0].Trim();
    109                         xjPoint.X = Convert.ToDouble(xjstrArray[1]);
    110                         xjPoint.Y = Convert.ToDouble(xjstrArray[2]);
    111 
    112                         xjList.Add(xjPoint);
    113                     }
    114                 }
    115                 else
    116                 {
    117                     return null;
    118                 }
    119                 xjStreamReader.Close();
    120                 return xjList;
    121             //catch (Exception ex)
    122             //{
    123             //    MessageBox.Show(ex.Message);
    124             //    return null;
    125             //}
    126         }
    127         //创建Shp矢量图层
    128         private IFeatureLayer CreateShpFromPoints(List<Point> xjPointList, string xjFilePath)
    129         {
    130             int index = xjFilePath.LastIndexOf('\');
    131             string xjFolder = xjFilePath.Substring(0, index);
    132             string xjShapeName = xjFilePath.Substring(index + 1);
    133             IWorkspaceFactory xjWsF = new ShapefileWorkspaceFactoryClass();
    134             IFeatureWorkspace xjFWs = (IFeatureWorkspace)xjWsF.OpenFromFile(xjFolder, 0);
    135 
    136             IFields xjFields = new FieldsClass();
    137             IFieldsEdit xjFieldsEdit;
    138             xjFieldsEdit = (IFieldsEdit)xjFields;
    139 
    140             IField xjField = new FieldClass();
    141             IFieldEdit xjFieldEdit = (IFieldEdit)xjField;
    142             xjFieldEdit.Name_2 = "Shape";
    143             xjFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
    144             IGeometryDef xjGeometryDef = new GeometryDefClass();
    145             IGeometryDefEdit xjGDefEdit = (IGeometryDefEdit)xjGeometryDef;
    146             xjGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
    147             //定义坐标系
    148             ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
    149             ISpatialReference pSpatialReference = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Beijing1954_3_Degree_GK_CM_114E);
    150             xjGDefEdit.SpatialReference_2 = pSpatialReference;
    151 
    152             xjFieldEdit.GeometryDef_2 = xjGeometryDef;
    153             xjFieldsEdit.AddField(xjField);
    154 
    155             IFeatureClass xjFeatureClass;
    156             xjFeatureClass = xjFWs.CreateFeatureClass(xjShapeName, xjFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
    157 
    158             IPoint xjPoint = new PointClass();
    159 
    160             for (int j = 0; j < xjPointList.Count; j++)
    161             {
    162 
    163                 xjPoint.X = xjPointList[j].X;
    164                 xjPoint.Y = xjPointList[j].Y;
    165 
    166                 IFeatureBuffer xjFeature = xjFeatureClass.CreateFeatureBuffer();
    167                 IFeatureCursor featureCursor = xjFeatureClass.Insert(true);
    168                 
    169                 xjFeature.Shape = xjPoint;
    170                 xjFeature.set_Value(xjFeature.Fields.FindField("id"), xjPointList[j].Name);
    171                 featureCursor.InsertFeature(xjFeature); 
    172             }
    173 
    174             IFeatureLayer xjFeatureLayer = new FeatureLayerClass();
    175             xjFeatureLayer.Name = xjShapeName;
    176             xjFeatureLayer.FeatureClass = xjFeatureClass;
    177             return xjFeatureLayer;
    178         }
    179         //单击显示保存
    180         private void btn_ShowSave_Click(object sender, EventArgs e)
    181         {
    182             if (Check())
    183             {
    184                 List<Point> xjPointList = GetPoint(txt_TxtPath.Text);
    185                 if (xjPointList == null)
    186                 {
    187                     MessageBox.Show("选择文件是空的!");
    188                 }
    189                 else
    190                 {
    191                     IFeatureLayer pFeatureLayer = CreateShpFromPoints(xjPointList, txt_ShpPath.Text);
    192                     //MainForm.m_mapControl.Map.AddLayer(pFeatureLayer);
    193                 }
    194             }
    195             MessageBox.Show("完成!");
    196         }
    197     }
    198 }
    199 
    200 Formtxt2shp.cs
    Formtxt2shp.cs
  • 相关阅读:
    MySQL InnoDB 锁
    MySQL InnoDB 事务
    MySQL 执行计划详解
    php获取当前url地址的方法小结
    数据库联表统计查询 Group by & INNER JOIN
    大文件分片上传,断点续传,秒传 实现
    如何让 height:100%; 起作用
    移动前端头部标签(HTML5 head meta)
    Emoji表情符号在MySQL数据库中的存储
    HTML页面直接显示json 结构
  • 原文地址:https://www.cnblogs.com/dengyg0710/p/8722024.html
Copyright © 2011-2022 走看看