分享给有需要的人,代码质量勿喷。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
namespace txt坐标数据转Shp矢量点VS2010
{
public partial class MainForn : Form
{
public MainForn()
{
InitializeComponent();
}
//选择txt点文件:格式为 x,y,z//可加序号
private void btnPath_Click(object sender, EventArgs e)
{
OpenFileDialog xjTxtOpenFileDialog = new OpenFileDialog();
xjTxtOpenFileDialog.Multiselect = false;
xjTxtOpenFileDialog.Title = "打开txt坐标文件";
xjTxtOpenFileDialog.Filter = "txt坐标文件(*.txt)|*.txt";
if (xjTxtOpenFileDialog.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = xjTxtOpenFileDialog.FileName;
}
}
//设置shp点保存路径
private void btnShpPath_Click(object sender, EventArgs e)
{
SaveFileDialog xjShpSaveFileDialog = new SaveFileDialog();
xjShpSaveFileDialog.Filter = "Shape文件(*.shp)|*.shp";
if (File.Exists(this.txtPath.Text))
{
xjShpSaveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(this.txtPath.Text);
}
if (xjShpSaveFileDialog.ShowDialog() == DialogResult.OK)
{
this.txtShpPath.Text = xjShpSaveFileDialog.FileName;
}
}
//检查数据和路径
private bool CheckDataAndPath()
{
if (this.txtPath.Text == "" || !File.Exists(this.txtPath.Text))
{
MessageBox.Show("数据无效!!!", "错误", MessageBoxButtons.OK);
return false;
}
if (this.txtShpPath.Text == "" || System.IO.Path.GetExtension(this.txtShpPath.Text).ToLower() != ".shp")
{
MessageBox.Show("Shp矢量点保存路径无效!!!", "错误", MessageBoxButtons.OK);
return false;
}
return true;
}
//转换
private void btnConvert_Click(object sender, EventArgs e)
{
if (CheckDataAndPath())
{
List<Point> xjPointList = GetPoint(this.txtPath.Text);
if (xjPointList == null)
{
MessageBox.Show("选择文件是空的,转毛线啊");
}
else
{
IFeatureLayer pFeatureLayer = CreateShpFromPoints(xjPointList, this.txtShpPath.Text);
this.axMapControl1.Map.AddLayer(pFeatureLayer);
}
}
}
//结构体
struct Point
{
//public string Name;//可加序号
public double X;
public double Y;
public double Z;
}
//获取点数据
List<string> xjColumn = new List<string>();
private List<Point> GetPoint(string surveyDataFullName)
{
try
{
List<Point> xjList = new List<Point>();
char[] xjchar = new char[] { ',', ' ', '\t' }; //常用的分隔符为逗号、空格、制位符
//读取
FileStream xjFileStream = new FileStream(surveyDataFullName, FileMode.Open);
StreamReader xjStreamReader = new StreamReader(xjFileStream, Encoding.Default);
string xjstringLine = xjStreamReader.ReadLine();
if (xjstringLine != null)
{
string[] xjstrArray = xjstringLine.Split(xjchar);
if (xjstrArray.Length > 0)
{
for (int i = 0; i < xjstrArray.Length; i++)
{
xjColumn.Add(xjstrArray[i]);
}
}
while ((xjstringLine = xjStreamReader.ReadLine()) != null)
{
//点信息的读取
xjstrArray = xjstringLine.Split(xjchar);
Point xjPoint = new Point();
//xjPoint.Name = xjstrArray[0].Trim();//可加序号,下面012都加1
xjPoint.X = Convert.ToDouble(xjstrArray[0]);
xjPoint.Y = Convert.ToDouble(xjstrArray[1]);
xjPoint.Z = Convert.ToDouble(xjstrArray[2]);
xjList.Add(xjPoint);
}
}
else
{
return null;
}
xjStreamReader.Close();
return xjList;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
//创建Shp矢量图层
private IFeatureLayer CreateShpFromPoints(List<Point> xjPointList, string xjFilePath)
{
int index = xjFilePath.LastIndexOf('\');
string xjFolder = xjFilePath.Substring(0, index);
string xjShapeName = xjFilePath.Substring(index + 1);
IWorkspaceFactory xjWsF = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace xjFWs = (IFeatureWorkspace)xjWsF.OpenFromFile(xjFolder, 0);
//字段
IFields xjFields = new FieldsClass();
IFieldsEdit xjFieldsEdit;
xjFieldsEdit = (IFieldsEdit)xjFields;
IField xjField = new FieldClass();
IFieldEdit xjFieldEdit = (IFieldEdit)xjField;
xjFieldEdit.Name_2 = "Shape";
xjFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef xjGeometryDef = new GeometryDefClass();
IGeometryDefEdit xjGDefEdit = (IGeometryDefEdit)xjGeometryDef;
xjGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
//定义坐标系
ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
ISpatialReference pSpatialReference = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);
xjGDefEdit.SpatialReference_2 = pSpatialReference;
xjFieldEdit.GeometryDef_2 = xjGeometryDef;
xjFieldsEdit.AddField(xjField);
//要素集
IFeatureClass xjFeatureClass;
xjFeatureClass = xjFWs.CreateFeatureClass(xjShapeName, xjFields, null, null,
esriFeatureType.esriFTSimple, "Shape", "");
//数据
IPoint xjPoint = new PointClass();
for (int j = 0; j < xjPointList.Count; j++)
{
xjPoint.X = xjPointList[j].X;
xjPoint.Y = xjPointList[j].Y;
xjPoint.Z = xjPointList[j].Z;
IFeature xjFeature = xjFeatureClass.CreateFeature();
xjFeature.Shape = xjPoint;
xjFeature.Store();
}
//矢量图层
IFeatureLayer xjFeatureLayer = new FeatureLayerClass();
xjFeatureLayer.Name = xjShapeName;
xjFeatureLayer.FeatureClass = xjFeatureClass;
return xjFeatureLayer;
}
}
}using System;
using Syste