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

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

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

 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 NewDistrict;
using System.IO; using System.Threading.Tasks;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry; namespace SignalDeal
{
public partial class Formtxt2shp : Form
{
public Formtxt2shp()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
InitializeComponent();
} //选择Txt文件
private void btn_TxtPath_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)
{
txt_TxtPath.Text = xjTxtOpenFileDialog.FileName;
}
} //Shp矢量点保存路径
private void btn_ShpPath_Click(object sender, EventArgs e)
{
SaveFileDialog xjShpSaveFileDialog = new SaveFileDialog();
xjShpSaveFileDialog.Filter = "Shape文件(*.shp)|*.shp";
if (File.Exists(txt_TxtPath.Text))
{
xjShpSaveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(txt_TxtPath.Text);
}
if (xjShpSaveFileDialog.ShowDialog() == DialogResult.OK)
{
txt_ShpPath.Text = xjShpSaveFileDialog.FileName;
}
} //显示保存
//检查数据和路径
private bool Check()
{
if (txt_TxtPath.Text == "" || !File.Exists(txt_TxtPath.Text))
{
MessageBox.Show("数据无效,重选", "提示", MessageBoxButtons.OK);
return false;
}
if (txt_ShpPath.Text == "" || System.IO.Path.GetExtension(txt_ShpPath.Text).ToLower() != ".shp")
{
MessageBox.Show("Shp矢量点保存路径无效,重选", "提示", MessageBoxButtons.OK);
return false;
}
return true;
}
//结构体
struct Point
{
public string Name;
public double X;
public double Y;
}
List<string> xjColumn = new List<string>();
//获取点数据
private List<Point> GetPoint(string surveyDataFullName)
{
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 > )
{
for (int i = ; i < xjstrArray.Length; i++)
{
xjColumn.Add(xjstrArray[i]);
}
} while ((xjstringLine = xjStreamReader.ReadLine()) != null)
{
//点信息的读取
xjstrArray = xjstringLine.Split(xjchar);
Point xjPoint = new Point();
xjPoint.Name = xjstrArray[].Trim();
xjPoint.X = Convert.ToDouble(xjstrArray[]);
xjPoint.Y = Convert.ToDouble(xjstrArray[]); 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(, index);
string xjShapeName = xjFilePath.Substring(index + );
IWorkspaceFactory xjWsF = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace xjFWs = (IFeatureWorkspace)xjWsF.OpenFromFile(xjFolder, ); 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.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Beijing1954_3_Degree_GK_CM_114E);
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 = ; j < xjPointList.Count; j++)
{ xjPoint.X = xjPointList[j].X;
xjPoint.Y = xjPointList[j].Y; IFeatureBuffer xjFeature = xjFeatureClass.CreateFeatureBuffer();
IFeatureCursor featureCursor = xjFeatureClass.Insert(true); xjFeature.Shape = xjPoint;
xjFeature.set_Value(xjFeature.Fields.FindField("id"), xjPointList[j].Name);
featureCursor.InsertFeature(xjFeature);
} IFeatureLayer xjFeatureLayer = new FeatureLayerClass();
xjFeatureLayer.Name = xjShapeName;
xjFeatureLayer.FeatureClass = xjFeatureClass;
return xjFeatureLayer;
}
//单击显示保存
private void btn_ShowSave_Click(object sender, EventArgs e)
{
if (Check())
{
List<Point> xjPointList = GetPoint(txt_TxtPath.Text);
if (xjPointList == null)
{
MessageBox.Show("选择文件是空的!");
}
else
{
IFeatureLayer pFeatureLayer = CreateShpFromPoints(xjPointList, txt_ShpPath.Text);
//MainForm.m_mapControl.Map.AddLayer(pFeatureLayer);
}
}
MessageBox.Show("完成!");
}
}
} Formtxt2shp.cs

Formtxt2shp.cs

最新文章

  1. 小议ARM寄存器
  2. 自动打开Accesibility Service 可以自动安装APP
  3. windows下安装beautifulsoup4
  4. [多图]Windows 10 Build 10565今推送:优化界面菜单 Cortana改进
  5. Cron表达式简单学习
  6. swift学习笔记之-自动引用计数
  7. 在oracle中使用Trigger
  8. opencv有关错误及解决办法
  9. android usb挂载分析---vold处理内核消息
  10. 英语词汇(2)fall down,fall off和fall over
  11. CRM客户关系管理系统(八)
  12. EF Core利用Scaffold从根据数据库生成代码
  13. 052 kafka对topic的增删改查操作
  14. windows下mvn verify异常
  15. HHVM 3.0 发布,执行 PHP 的虚拟机
  16. Qt ------ QPainter 和控件组件的重绘
  17. python 学习之dict和set类型
  18. 【js】中的小技巧
  19. git命令行操作详解
  20. css 文字超出省略号

热门文章

  1. sql注入测试(3)---现象分析
  2. python练习:函数2
  3. dotnetcore下解压zip文件,解决中文文件名乱码问题
  4. sftp配置多个用户权限的问题
  5. 一次腾讯云centos服务器被入侵的处理
  6. shell for 循环简单用法
  7. js 执行完setTimeout再接着执行函数
  8. man 手册--nc
  9. fastadmin 中 a标签跳转
  10. Win10系统更新提示错误0xc1900403的解决方法