zoukankan      html  css  js  c++  java
  • GPS文件处理

    GPS数据导入计算机后,有多种格式,易于处理、易于识别的就是TXT和CSV格式,在本文中我处理的是TXT格式。

    建立Shapefile和处理TXT数据都比较简单,后面我就只贴代码(代码里有详细注释),不讲原理了。

    主代码:

      public static void CreateShpfromTXT(IMapControl3 map_control,progressFrm  progressfrm)

            {

              

                ProgressBar progressBar1 = progressfrm.progressBar1;

                OpenFileDialog openDia = new OpenFileDialog();

                openDia.Title = "打开GPS文件(TXT).......";

                openDia.Filter = "文本文件(*.TXT)|*.txt";

                openDia.RestoreDirectory = true;

                openDia.Multiselect = true;

                if (openDia.ShowDialog() == DialogResult.OK)

                {

                    string[] arrayFileName = openDia.FileNames;

                    string[] arraySafeName = openDia.SafeFileNames;

                    for (int i = arrayFileName.Length-1; i >=0; i--)

                    {

                        //根据用户选择的多个TXT文件  依次生成Shp文件

                        string filefullname = arrayFileName[i];

                        string filesafename = arraySafeName[i];

                        string directoryPath = filefullname.Remove(filefullname.LastIndexOf(filesafename), filesafename.Length);

                        string fileName = filesafename.Remove(filesafename.LastIndexOf(".TXT"), 4);

                        string shapeName = fileName + ".shp";

                        foreach (FileInfo file in new DirectoryInfo(directoryPath).GetFiles())

                        {

                            if (file.Name == shapeName)

                            {

                                file.Delete();//如果该Shp文件已经存在,则删除它以及与它相关的文件

                                (new FileInfo(directoryPath + fileName + ".shx")).Delete();

                                (new FileInfo(directoryPath + fileName + ".prj")).Delete();

                                (new FileInfo(directoryPath + fileName + ".dbf")).Delete();

                            }

                        }

                        #region 开始创建Shp

                        IFeatureClass newfeatureclass = CreatNewShapeFile(directoryPath, shapeName);

                        if (newfeatureclass != null)

                        {

                            string[] strs = File.ReadAllLines(filefullname);

                            int hasline = strs.Count();//获取该TXT文件有多少个点

                            progressBar1.Maximum = hasline;

                            progressBar1.Value = 0;

                            progressfrm.TipText ="";

                            progressfrm.TipText = string.Format("正在处理GPS文件:{0},请稍等……", filefullname);

                            progressfrm.Refresh();

                            System.IO.FileStream filestreamTXT = new System.IO.FileStream(filefullname, FileMode.Open);

                          

                            StreamReader streamreader = new StreamReader(filestreamTXT);

                            streamreader.BaseStream.Seek(0, SeekOrigin.Begin);

                            streamreader.ReadLine(); streamreader.ReadLine(); streamreader.ReadLine();

                            while (streamreader.Peek() > 0)

                            {

                                string strLine = streamreader.ReadLine() + " ";

                                List<string> filedArray = findWord(strLine);

                                if (filedArray != null)

                                {

                                    List<string> filedArrayUnion = GetUnionfieldList(filedArray);

                                    IFeature newfeature = newfeatureclass.CreateFeature();

                                    IPoint newpoint = new PointClass();

                                    newpoint.X = Convert.ToDouble(filedArrayUnion[2]);

                                    newpoint.Y = Convert.ToDouble(filedArrayUnion[1]);

                                    newpoint.Z = Convert.ToDouble(filedArrayUnion[3]);

                                    newfeature.Shape = newpoint as IGeometry;

                     

                                    newfeature.Store();

                                    progressBar1.Value++;

                                }

                            }

                            strs = null;

                          

                        }

                        #endregion

            

                        map_control.AddShapeFile(directoryPath, shapeName);

                    }

                }

            }

    创建一个空的Shapefile:

      public static IFeatureClass CreatNewShapeFile(string directoryPath, string shapeName)

            {

                IAoInitialize aoinitialize = new AoInitializeClass();

                aoinitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

                //步骤:打开工作空间;建立字段集;建立字段;IFeatureWorkspace.CreatFeatureClass

                //关键在于几何字段的IGeometryDefEdit,另外还有esriFieldType.esriFieldTypeGeometry,esriFeatureType.esriFTSimple

                const string strShapeFieldName = "shape";

                IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();

                IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(directoryPath, 0);

                //  directoryPath 父文件夹

                //设置字段集  

                IFields pFields = new FieldsClass();

                IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

                //设置字段  

                IField pFieldgeo = new FieldClass();

                IFieldEdit pFieldEditgeo = (IFieldEdit)pFieldgeo;

                //创建类型为几何类型的字段,添加Shape字段,OID是不用添加的

                pFieldEditgeo.Name_2 = "Shape";//Name代表只读,Name_2代表只写

                pFieldEditgeo.Type_2 = esriFieldType.esriFieldTypeGeometry;

                //为esriFieldTypeGeometry类型的字段创建几何定义,包括类型和空间参照   

                IGeometryDef pGeoDef = new GeometryDefClass();     //The geometry definition for the field if IsGeometry is TRUE.  

                IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;

                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

                // pGeoDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass() ;

                // pFieldEditgeo.GeometryDef_2 = pGeoDef;

                ISpatialReferenceFactory3 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();

                ISpatialReference spatialReference = spatialReferenceFactory.CreateSpatialReference((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

                ISpatialReferenceInfo spatialReferenceInfo = spatialReference as ISpatialReferenceInfo;

                ISpatialReference spatial = spatialReferenceInfo as ISpatialReference;

                pGeoDefEdit.SpatialReference_2 = spatial;

                pFieldEditgeo.GeometryDef_2 = pGeoDef;

                pFieldsEdit.AddField(pFieldgeo);

                //创建shapefile  

                IFeatureClass newfeatureclass = pWS.CreateFeatureClass(shapeName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

                return newfeatureclass;

            }

    作者: 风云

    出处: http://www.cnblogs.com/fengyunlishi/

    本文版权归风云和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    作者: 风云 出处: http://www.cnblogs.com/fengyunlishi/ 本文版权归风云和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    洛谷 P2327 [SCOI2005]扫雷 题解
    P1388 算式 题解
    P1281 书的复制 题解
    P2896 [USACO08FEB]一起吃饭Eating Together 题解
    P1140 相似基因 题解
    变量的解构赋值
    let 和 const 命令
    第一阶段站立会议8
    第一阶段站立会议7
    第一阶段站立会议6
  • 原文地址:https://www.cnblogs.com/fengyunlishi/p/2710574.html
Copyright © 2011-2022 走看看