C#+ArcEngine:txt点数据转Shp矢量数据
2016-12-01 11:33
609 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace AE简单开发
{
publicpartialclassForm_TxtToShpPoint :Form
{
publicForm_TxtToShpPoint()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
InitializeComponen
4000
t();
}
//选择Txt文件
privatevoidbtn_TxtPath_Click(object sender,EventArgs e)
{
OpenFileDialog xjTxtOpenFileDialog =newOpenFileDialog();
xjTxtOpenFileDialog.Multiselect =
false;
xjTxtOpenFileDialog.Title =
"打开txt坐标文件";
xjTxtOpenFileDialog.Filter =
"txt坐标文件(*.txt)|*.txt";
if(xjTxtOpenFileDialog.ShowDialog() ==DialogResult.OK)
{
txt_TxtPath.Text = xjTxtOpenFileDialog.FileName;
}
}
//Shp矢量点保存路径
privatevoidbtn_ShpPath_Click(object sender,EventArgs e)
{
SaveFileDialogxjShpSaveFileDialog =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;
}
}
//显示保存
//检查数据和路径
privateboolCheck()
{
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;
}
returntrue;
}
//结构体
structPoint
{
publicstringName;
publicdoubleX;
publicdoubleY;
}
List<string>xjColumn =new
List<string>();
//获取点数据
privateList<Point>GetPoint(string surveyDataFullName)
{
try
{
List<Point>xjList =new
List<Point>();
char[] xjchar =
newchar[]{
',', ' ','\t' }; //常用的分隔符为逗号、空格、制位符
//读取
FileStream xjFileStream =newFileStream(surveyDataFullName,FileMode.Open);
StreamReader xjStreamReader =newStreamReader(xjFileStream,Encoding.Default);
string xjstringLine =xjStreamReader.ReadLine();
if(xjstringLine !=null)
{
string[] xjstrArray =xjstringLine.Split(xjchar);
if (xjstrArray.Length > 0)
{
for(inti = 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();
xjPoint.X =
Convert.ToDouble(xjstrArray[1]);
xjPoint.Y =
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,stringxjFilePath)
{
intindex = xjFilePath.LastIndexOf('\\');
stringxjFolder = xjFilePath.Substring(0, index);
stringxjShapeName = xjFilePath.Substring(index + 1);
IWorkspaceFactory xjWsF =newShapefileWorkspaceFactoryClass();
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 =newGeometryDefClass();
IGeometryDefEdit xjGDefEdit = (IGeometryDefEdit)xjGeometryDef;
xjGDefEdit.GeometryType_2 =
esriGeometryType.esriGeometryPoint;
//定义坐标系
ISpatialReferenceFactory pSRF
a9fa
=newSpatialReferenceEnvironmentClass();
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(intj = 0; j < xjPointList.Count; j++)
{
xjPoint.X = xjPointList[j].X;
xjPoint.Y = xjPointList[j].Y;
IFeature xjFeature =xjFeatureClass.CreateFeature();
xjFeature.Shape = xjPoint;
xjFeature.Store();
}
IFeatureLayer xjFeatureLayer =newFeatureLayerClass();
xjFeatureLayer.Name = xjShapeName;
xjFeatureLayer.FeatureClass = xjFeatureClass;
returnxjFeatureLayer;
}
//单击显示保存
privatevoidbtn_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);
ShpMapControl.Map.AddLayer(pFeatureLayer);
}
}
}
}
}
VS2012+ArcEngine10.2具体窗体+代码见:点击打开链接
相关文章推荐
- C#+ArcEngine:加载打开Shp矢量数据和栅格数据(VS2010窗体+代码)
- C#+Arcengine实现GP工具中的extract by mask(提取掩膜),可以实现提取shp范围的栅格数据,可用来获得shp范围的高程
- C#+ArcEngine:shp矢量点转Tin(VS2010窗体+代码)
- C#+Arcengine实现GP工具中Data Management Tool》raster》raster processing中的clip功能(矢量数据对栅格数据的裁剪)
- Arcengine+C#实现矢量数据的裁剪
- C# 遍历textboxs 绑定相应数据txt1~txtn
- (转) C# ListView控件的数据导入、导出到txt文件
- [C#]将数据存入硬盘文件(txt)
- C#实现TXT文本数据批量导入SQL Server
- arcengine C# 按行政区范围加载图层数据
- C#下把txt文件数据读进sql server中存储所遇到的乱码问题
- 从txt文档中读取数据并做相应处理(c#)
- c# 向记事本中(.txt)写入/读取数据
- Erdas:将shp文件保存为Erdas中适用的AOI文件 矢量数据裁剪栅格
- 【041】通过调入txt的点数据来创建shp
- ArcEngine,C#删除数据几种方法和性能比较
- C# +AE加载shp、栅格、地图文档数据
- ArcEngine,C#数据删除几种方法以及性能比较
- C# asp.net页面接收的数据保存到Txt文件中, C#将数据保存到记录本中 conquer
- C#下 读取xml节点的数据总结 .txt