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;

            }

  • 相关阅读:
    RecyclerView 数据刷新的几种方式 局部刷新 notify MD
    【图片】批量获取几万张图片
    RV BaseRecyclerViewAdapterHelper 总结 MD
    RecyclerView.ItemDecoration 间隔线
    Kotlin【简介】Android开发 配置 扩展
    Kotlin 特性 语法糖 优势 扩展 高阶 MD
    一个十分简洁实用的MD风格的UI主框架
    折叠伸缩工具栏 CollapsingToolbarLayout
    FloatingActionButton FAB 悬浮按钮
    Glide Picasso Fresco UIL 图片框架 缓存 MD
  • 原文地址:https://www.cnblogs.com/cuiguanghe/p/2980214.html
Copyright © 2011-2022 走看看