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

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

实现坐标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
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 }199 200 Formtxt2shp.cs
Formtxt2shp.cs

 

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

你可能感兴趣的文章
mysql之TIMESTAMP(时间戳)用法详解
查看>>
jsp+servlet判断是否登录以及登录是否失效
查看>>
Java记录 -85- Reflection API的使用示例进阶
查看>>
PHP 加速插件 Zend Opcache
查看>>
我的友情链接
查看>>
Oracle的简单命令应用
查看>>
spring后置通知、异常通知、最终通知、环绕通知
查看>>
聊聊并发(一)——深入分析Volatile的实现原理
查看>>
oVirt node使用virsh命令需要登陆的问题
查看>>
php的魔术方法
查看>>
Bootstrap-button
查看>>
CentOS5.4 上将Python进行升级,已经解决yum不可用的问题(转载)
查看>>
centos 挂在smb 共享目录
查看>>
开源 java CMS - FreeCMS2.8 数据对象 searchword
查看>>
乔客JS友情链接改文本输出
查看>>
从源码角度理解Java设计模式——门面模式
查看>>
iOS中Cookie介绍(转)
查看>>
linux服务
查看>>
我的友情链接
查看>>
java程序题:打印出杨辉三角形
查看>>