博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AE开发之shp转txt
阅读量:6141 次
发布时间:2019-06-21

本文共 7357 字,大约阅读时间需要 24 分钟。

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
xjColumn = new List
(); 83 //获取点数据 84 private List
GetPoint(string surveyDataFullName) 85 { 86 List
xjList = new List
(); 87 char[] xjchar = new char[] { ',', ' ', '\t' }; //常用的分隔符为逗号、空格、制位符 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 else116 {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
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
xjPointList = GetPoint(txt_TxtPath.Text);185 if (xjPointList == null)186 {187 MessageBox.Show("选择文件是空的!");188 }189 else190 {191 IFeatureLayer pFeatureLayer = CreateShpFromPoints(xjPointList, txt_ShpPath.Text);192 //MainForm.m_mapControl.Map.AddLayer(pFeatureLayer);193 }194 }195 MessageBox.Show("完成!");196 }197 }198 }
Formtxt2shp.cs

得到的txt包括shp属性表所有数据。(首行为字段名)

窗体如下:

 

转载于:https://www.cnblogs.com/dengyg0710/p/7685001.html

你可能感兴趣的文章
java的特殊符号
查看>>
word2010中去掉红色波浪线的方法
查看>>
fabric上下文管理器(context mangers)
查看>>
JQuery-EasyUI Datagrid数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
查看>>
并发和并行的区别
查看>>
php小知识
查看>>
Windows下安装、运行Lua
查看>>
Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解(二)
查看>>
初识中间件之消息队列
查看>>
MyBatis学习总结(三)——优化MyBatis配置文件中的配置
查看>>
Spring常用注解
查看>>
我的友情链接
查看>>
PCS子层有什么用?
查看>>
查看端口,关闭端口
查看>>
代码托管平台简介
查看>>
linux:yum和apt-get的区别
查看>>
Sentinel 1.5.0 正式发布,引入 Reactive 支持
查看>>
数据库之MySQL
查看>>
2019/1/15 批量删除数据库相关数据
查看>>
数据类型的一些方法
查看>>