1. 程式人生 > >txt轉shp

txt轉shp

efault array urn val creat car -a display rms

實現坐標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[] { ,, , \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 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

txt轉shp