码迷,mamicode.com
首页 > Windows程序 > 详细

坐标格式提取转换的两种方法C#、Excel

时间:2016-10-02 17:13:48      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

一、引言

      受朋友之托,处理一份点云数据,格式:“X[m]  Y[m]  Z[m]  R[dB]  G[dB]  B[dB]”,总共63w个点,转换成的格式是:“点名,,X[m], Y[m], Z[m]”。如果经常有坐标文件转换就使用代码方法,偶尔使用的话就使用Excel。用Excel的话,直接把后缀名改成.xlsx,接下来就是对整列进行插入、更改等事情了,最后另存为txt格式或者dat格式。

 

二、知识准备

     1、文件读写

     2、字符串处理

 

三、需要注意的地方

     1、60几万个点,数据量还行,所以思路和数据结构要格外注意,能省则省。鄙人昨天走了弯路,拿19万个点测试时候花了0.75秒有点沾沾自喜,今天重新到回去看,写的什么玩意儿,把代码重新修改了一下,测试显示,19万个点0.45秒左右,63万个点1.15秒左右。

    2、文件读写要注意文件流的打开与关闭。

 

四、代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 点云转pt
{
    class Program
    {
        static void Main(string[] args)
        {
            //测时
            Stopwatch st = new Stopwatch();
            st.Start();
            string wjming = "坐标" + "(" + DateTime.Now.ToString("yyyyMMddhhmmss") + ")";
            string path = @"E:\C#\点云转pt2\点云转pt2\bin\Debug";
            File.WriteAllText(path + "\\" + wjming + ".txt", null);
            List<坐标> result = diaoqu(@"C:\Users\Ouy_\Desktop\all-Octree (0.1).txt");
            using (StreamWriter sw = new StreamWriter(wjming + ".txt"))
            {
                foreach (var item1 in result)
                {
                    sw.WriteLine(item1.Name + ",," + item1.X + "," + item1.Y + "," + item1.H);
                }
            }
            st.Stop();
            Console.WriteLine("OK!Time: " + st.Elapsed);
        }
        static List<坐标> diaoqu(string a)
        {
            FileStream fs = new FileStream(a, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader m_streamReader = new StreamReader(fs);
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            int index = 0;
            string strLine = m_streamReader.ReadLine();
            List<坐标> result = new List<坐标>();
            // 从数据流中读取每一行,直到文件的最后一行
            while (strLine != null)
            {
                string[] s = strLine.Split( );//txt文件格式分隔符
                try
                {
                    坐标 pt = new 坐标((++index).ToString(), s[0], s[1], s[2]);
                    result.Add(pt);
                    strLine = m_streamReader.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
            fs.Close();
            m_streamReader.Close();
            return result;
        }
    }
    class 坐标
    {
        public string Name { get; set; }
        public string X { get; set; }
        public string Y { get; set; }
        public string H { get; set; }

        public 坐标(string name, string x, string y, string h)
        {
            Name = name;
            X = x;
            Y = y;
            H = h;
        }

    }
}

 

坐标格式提取转换的两种方法C#、Excel

标签:

原文地址:http://www.cnblogs.com/zhaoyangyucaiyun/p/5927702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!