您的位置:首页 > 编程语言 > C#

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具体窗体+代码见:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: