操作步骤
1.选择txt文件
2.创建shp保存路径
3.保存
视图:
代码
public partial class Form1 : Form
{
public Form1()
{
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)
{
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();
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, 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;
//创建要素
IFeature xjFeature = xjFeatureClass.CreateFeature();
//设置要素的坐标
xjFeature.Shape = xjPoint;
xjFeature.Store();
}
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);
ShpMapControl.Map.AddLayer(pFeatureLayer);
}
}
}
private void axLicenseControl1_Enter(object sender, EventArgs e)
{
}
}
代码下载链接:https://pan.baidu.com/s/1vYDFcH29fLd3mcWcwOGV6Q
提取码:j3dg
txt坐标数据转内存图层线
1.创建内存图层
2.添加线要素
代码
1.在内存中创建图层
/// <summary>
/// 在内存中创建图层
/// </summary>
/// <param name="DataSetName">数据集名称</param>
/// <param name="AliaseName">别名</param>
/// <param name="SpatialRef">空间参考</param>
/// <param name="GeometryType">几何类型</param>
/// <param name="PropertyFields">属性字段集合</param>
/// <returns>IfeatureLayer</returns>
public static IFeatureLayer CreateFeatureLayerInmemeory(string DataSetName, string AliaseName, ISpatialReference SpatialRef, esriGeometryType GeometryType, IFields PropertyFields)
{
IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0);
ESRI.ArcGIS.esriSystem.IName name = (IName)workspaceName;
ESRI.ArcGIS.Geodatabase.IWorkspace inmemWor = (IWorkspace)name.Open();
IField oField = new FieldClass();
IFields oFields = new FieldsClass();
IFieldsEdit oFieldsEdit = null;
IFieldEdit oFieldEdit = null;
IFeatureClass oFeatureClass = null;
IFeatureLayer oFeatureLayer = null;
try
{
//创建字段集
oFieldsEdit = oFields as IFieldsEdit;
//创建一个字段
oFieldEdit = oField as IFieldEdit;
for (int i = 0; i < PropertyFields.FieldCount; i++)
{
//添加自定义字段
oFieldsEdit.AddField(PropertyFields.get_Field(i));
}
//定义一个几何字段
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.AvgNumPoints_2 = 5;
geometryDefEdit.GeometryType_2 = GeometryType;
geometryDefEdit.GridCount_2 = 1;
geometryDefEdit.HasM_2 = false;
geometryDefEdit.HasZ_2 = false;
geometryDefEdit.SpatialReference_2 = SpatialRef; //几何字段的参考系
geometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();//没有这一句就报错,说尝试读取或写入受保护的内存。
geometryDefEdit.SpatialReference.SetDomain(-200, 200, -200, 200);//没有这句就抛异常来自HRESULT:0x8004120E。
//创建一个字段,并且添加到字段集对象中去
oFieldEdit.Name_2 = "SHAPE";
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = geometryDef;
oFieldEdit.IsNullable_2 = true;
oFieldEdit.Required_2 = true;
oFieldsEdit.AddField(oField);
oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass(DataSetName, oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
(oFeatureClass as IDataset).BrowseName = DataSetName;
oFeatureLayer = new FeatureLayerClass();
oFeatureLayer.Name = AliaseName;
oFeatureLayer.FeatureClass = oFeatureClass;
}
catch
{
}
return oFeatureLayer;
}
2.调用内存图层的代码
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
IFieldsEdit curFileds = new FieldsClass();
IFieldEdit curField = new FieldClass();
curField = new FieldClass();
curField.Name_2 = "名称";
curField.Type_2 = esriFieldType.esriFieldTypeString;
curFileds.AddField(curField);
curField = new FieldClass();
curField.Name_2 = "经度";
curField.Type_2 = esriFieldType.esriFieldTypeDouble;
curFileds.AddField(curField);
curField = new FieldClass();
curField.Name_2 = "纬度";
curField.Type_2 = esriFieldType.esriFieldTypeDouble;
curFileds.AddField(curField);
//定义坐标系
ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
ISpatialReference pSpatialReference = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);
pFeatureLayer = CreateFeatureLayerInmemeory("Position", "采集点", new UnknownCoordinateSystemClass(), esriGeometryType.esriGeometryPolyline, curFileds as IFields);
mainMapControl.AddLayer(pFeatureLayer as ILayer);
IFeatureCursor FeatureCursor;
IFeature pFeature;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IPolyline linecollect = new PolylineClass();
for (int j = 0; j < 15; j++)
{
IPoint xjPoint = new PointClass();
xjPoint.X = -97 + j * 2;
xjPoint.Y = 85 + j * 2;
IPoint yjPoint = new PointClass();
yjPoint.X = -37 + j * 2;
yjPoint.Y = 35 + j * 2;
(linecollect as IPointCollection).AddPoint(xjPoint);
(linecollect as IPointCollection).AddPoint(yjPoint);
pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = linecollect;
pFeature.Store();
}
mainMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pFeatureLayer, null);
mainMapControl.ActiveView.ScreenDisplay.UpdateWindow();