.NET Compact Framework下的GPS NMEA data数据分析(二)转

简介: .NET Compact Framework下的GPS NMEA data数据分析(二)转
public class NmeaParser
    {
        public struct Coordinate
        {
            public int Hours;
            public int Minutes;
            public double Seconds;
        }
        public enum FixStatus
        {
            NotSet,
            Obtained, //A
            Lost //V
        }
        public enum FixMode
        {
            Auto,   //A
            Manual
        }
        public enum FixMethod
        {
            NotSet,
            Fix2D,
            Fix3D
        }
        public enum DifferentialGpsType
        {
            NotSet, 
            SPS,    
            DSPS,   
            PPS,    
            RTK     
        }
        public class GpsSatellite
        {
            public int PRC { get; set; }
            public int Elevation { get; set; }
            public int Azimuth { get; set; }
            public int SNR { get; set; }
            public bool InUsed { get; set; }
            public bool InView { get; set; }
            public bool NotTracking { get; set; }
        }
        private static readonly CultureInfo NmeaCultureInfo = new CultureInfo("en-US");
        private static readonly decimal KMpHPerKnot = decimal.Parse("1.852", NmeaCultureInfo);
        private Coordinate latitude;
        private Coordinate longitude;
        private decimal altitude = 0;
        private DateTime utcDateTime;
        private decimal velocity = 0;
        private decimal azimuth = 0;
        private FixStatus fixStatus;
        private DifferentialGpsType differentialGpsType;
        private FixMode fixMode;
        private FixMethod fixMethod;
        private int satellitesInView;
        private int satellitesInUsed;
        private readonly Dictionary<int, GpsSatellite> satellites;
        private decimal horizontalDilutionOfPrecision = 50;
        private decimal positionDilutionOfPrecision = 50;
        private decimal verticalDilutionOfPrecision = 50;
        public NmeaParser()
        {
            satellites = new Dictionary<int, GpsSatellite>();
        }
        public bool Parse(string sentence)
        {
            string rawData = sentence;
            try
            {
                if (!IsValid(sentence))
                {
                    return false;
                }
                sentence = sentence.Substring(1, sentence.IndexOf('*') - 1);
                string[] Words = Getwords(sentence);
                switch (Words[0])
                {
                    case "GPRMC":
                        return ParseGPRMC(Words);
                    case "GPGGA":
                        return ParseGPGGA(Words);
                    case "GPGSA":
                        return ParseGPGSA(Words);
                    case "GPGSV":
                        return ParseGPGSV(Words);
                    default:
                        return false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + rawData);
                return false;
            }
        }
        private bool IsValid(string sentence)
        {
            // GPS data can't be zero length
            if (sentence.Length == 0)
            {
                return false;
            }
            // first character must be a $
            if (sentence[0] != '$')
            {
                return false;
            }
            // GPS data can't be longer than 82 character
            if (sentence.Length > 82)
            {
                return false;
            }
            try
            {
                string checksum = sentence.Substring(sentence.IndexOf('*') + 1);
                return Checksum(sentence, checksum);
            }
            catch (Exception e)
            {
                Console.WriteLine("Checksum failure. " + e.Message);
                return false;
            }
        }
        private bool Checksum(string sentence, string checksumStr)
        {
            int checksum = 0;
            int length = sentence.IndexOf('*') - 1;
            // go from first character upto last *
            for (int i = 1; i <= length; ++i)
            {
                checksum = checksum ^ Convert.ToByte(sentence[i]);
            }
            return (checksum.ToString("X2") == checksumStr);
        }
        // Divides a sentence into individual Words
        private static string[] Getwords(string sentence)
        {
            return sentence.Split(',');
        }
        private bool ParseGPRMC(string[] Words)
        {
            if (Words[1].Length > 0 & Words[9].Length > 0)
            {
                int UtcHours = Convert.ToInt32(Words[1].Substring(0, 2));
                int UtcMinutes = Convert.ToInt32(Words[1].Substring(2, 2));
                int UtcSeconds = Convert.ToInt32(Words[1].Substring(4, 2));
                int UtcMilliseconds = 0;
                // Extract milliseconds if it is available
                if (Words[1].Length > 7)
                {
                    UtcMilliseconds = Convert.ToInt32(Words[1].Substring(7));
                }
                int UtcDay = Convert.ToInt32(Words[9].Substring(0, 2));
                int UtcMonth = Convert.ToInt32(Words[9].Substring(2, 2));
                // available for this century
                int UtcYear = Convert.ToInt32(Words[9].Substring(4, 2)) + 2000;
                utcDateTime = new DateTime(UtcYear, UtcMonth, UtcDay, UtcHours, UtcMinutes, UtcSeconds, UtcMilliseconds);
            }
            fixStatus = (Words[2][0] == 'A') ? FixStatus.Obtained : FixStatus.Lost;
            if (Words[3].Length > 0 & Words[4].Length == 1 & Words[5].Length > 0 & Words[6].Length == 1)
            {
                latitude.Hours = int.Parse(Words[3].Substring(0, 2));
                latitude.Minutes = int.Parse(Words[3].Substring(2, 2));
                latitude.Seconds = Math.Round(double.Parse(Words[3].Substring(5, 4)) * 6 / 1000.0, 3);
                if ("S" == Words[4])
                {
                    latitude.Hours = -latitude.Hours;
                }
                longitude.Hours = int.Parse(Words[5].Substring(0, 3));
                longitude.Minutes = int.Parse(Words[5].Substring(3, 2));
                longitude.Seconds = Math.Round(double.Parse(Words[5].Substring(6, 4)) * 6 / 1000.0, 3);
                if ("W" == Words[6])
                {
                    longitude.Hours = -longitude.Hours;
                }
            }
            if (Words[8].Length > 0)
            {
                azimuth = decimal.Parse(Words[8], NmeaCultureInfo);
            }
            if (Words[7].Length > 0)
            {
                velocity = decimal.Parse(Words[7], NmeaCultureInfo) * KMpHPerKnot;
            }
            return true;
        }
        private bool ParseGPGGA(string[] Words)
        {
            if (Words[6].Length > 0)
            {
                switch (Convert.ToInt32(Words[6]))
                {
                    case 0:
                        differentialGpsType = DifferentialGpsType.NotSet;
                        break;
                    case 1:
                        differentialGpsType = DifferentialGpsType.SPS;
                        break;
                    case 2:
                        differentialGpsType = DifferentialGpsType.DSPS;
                        break;
                    case 3:
                        differentialGpsType = DifferentialGpsType.PPS;
                        break;
                    case 4:
                        differentialGpsType = DifferentialGpsType.RTK;
                        break;
                    default:
                        differentialGpsType = DifferentialGpsType.NotSet;
                        break;
                }
            }
            if (Words[7].Length > 0)
            {
                satellitesInUsed = Convert.ToInt32(Words[7]);
            }
            if (Words[8].Length > 0)
            {
                horizontalDilutionOfPrecision = Convert.ToDecimal(Words[8]);
            }
            if (Words[9].Length > 0)
            {
                altitude = Convert.ToDecimal(Words[9]);
            }
            return true;
        }
        private bool ParseGPGSA(string[] Words)
        {
            if (Words[1].Length > 0)
            {
                fixMode = Words[1][0] == 'A' ? FixMode.Auto : FixMode.Manual;
            }
            if (Words[2].Length > 0)
            {
                switch (Convert.ToInt32(Words[2]))
                {
                    case 1:
                        fixMethod = FixMethod.NotSet;
                        break;
                    case 2:
                        fixMethod = FixMethod.Fix2D;
                        break;
                    case 3:
                        fixMethod = FixMethod.Fix3D;
                        break;
                    default:
                        fixMethod = FixMethod.NotSet;
                        break;
                }
            }
            foreach (GpsSatellite s in satellites.Values)
            {
                s.InUsed = false;
            }
            satellitesInUsed = 0;
            for (int i = 0; i < 12; ++i)
            {
                string id = Words[3 + i];
                if (id.Length > 0)
                {
                    int nId = Convert.ToInt32(id);
                    if (!satellites.ContainsKey(nId))
                    {
                        satellites[nId] = new GpsSatellite();
                        satellites[nId].PRC = nId;
                    }
                    satellites[nId].InUsed = true;
                    ++satellitesInUsed;
                }
            }
            if (Words[15].Length > 0)
            {
                positionDilutionOfPrecision = Convert.ToDecimal(Words[15]);
            }
            if (Words[16].Length > 0)
            {
                horizontalDilutionOfPrecision = Convert.ToDecimal(Words[16]);
            }
            if (Words[17].Length > 0)
            {
                verticalDilutionOfPrecision = Convert.ToDecimal(Words[17]);
            }
            return true;
        }
        private bool ParseGPGSV(string[] Words)
        {
            int messageNumber = 0;
            if (Words[2].Length > 0)
            {
                messageNumber = Convert.ToInt32(Words[2]);
            }
            if (Words[3].Length > 0)
            {
                satellitesInView = Convert.ToInt32(Words[3]);
            }
            if (messageNumber == 0 || satellitesInView == 0)
            {
                return false;
            }
            for (int i = 1; i <= 4; ++i)
            {
                if ((Words.Length - 1) >= (i * 4 + 3))
                {
                    int nId = 0;
                    if (Words[i * 4].Length > 0)
                    {
                        string id = Words[i * 4];
                        nId = Convert.ToInt32(id);
                        if (!satellites.ContainsKey(nId))
                        {
                            satellites[nId] = new GpsSatellite();
                            satellites[nId].PRC = nId;
                        }
                        satellites[nId].InView = true;
                    }
                    if (Words[i * 4 + 1].Length > 0)
                    {
                        satellites[nId].Elevation = Convert.ToInt32(Words[i * 4 + 1]);
                    }
                    if (Words[i * 4 + 2].Length > 0)
                    {
                        satellites[nId].Azimuth = Convert.ToInt32(Words[i * 4 + 2]);
                    }
                    if (Words[i * 4 + 3].Length > 0)
                    {
                        satellites[nId].SNR = Convert.ToInt32(Words[i * 4 + 3]);
                        satellites[nId].NotTracking = false;
                    }
                    else
                    {
                        satellites[nId].NotTracking = true;
                    }
                }
            }
            return true;
        }
    }
相关文章
|
18小时前
|
开发工具 开发者
Jetbrains Rider:缺少.NET Framework 4.5.2
该文主要针对开发者,指出需下载SDK而非Runtime以进行应用程序开发。当使用Rider打开旧项目出现错误提示缺少.NET Framework 4.5.2时,需从微软官网下载相应版本的SDK(推荐开发版)。安装完成后,可能需要重启Rider以消除波浪线提示。对于.NET Core项目,若提示CLI路径未找到,同样需前往微软官网下载缺失的SDK版本,如.NET Core 3.1。安装完毕后,可考虑配置环境变量。
6 0
|
30天前
|
SQL Windows
保姆级:Windows Server 2012上安装.NET Framework 3.5
保姆级:Windows Server 2012上安装.NET Framework 3.5
|
30天前
|
开发框架 .NET C#
探索VB.NET:了解.NET Framework下的Visual Basic
【4月更文挑战第27天】Visual Basic进化为VB.NET,融入.NET Framework,提供面向对象编程、泛型、LINQ等特性。VB.NET是强类型语言,支持类型推断,通过Windows Forms和WPF构建桌面应用。广泛应用于企业级、Web和数据处理开发,是易学且功能强大的编程工具。随着.NET版本更新,VB.NET的应用仍具价值,适合初学者和资深开发者。
|
30天前
|
数据挖掘 定位技术
基于出租车GPS轨迹数据的研究:出租车行程的数据分析
基于出租车GPS轨迹数据的研究:出租车行程的数据分析
|
30天前
|
机器学习/深度学习 数据采集 数据挖掘
Python 的科学计算和数据分析: 解释什么是数据规整(Data Wrangling)?
【4月更文挑战第15天】数据规整是将原始数据转化为适合分析和建模的格式的关键步骤,涉及缺失值处理(删除、填充、插值)、异常值检测与处理、数据类型转换、重采样、数据合并、特征选择和特征变换等任务。这些预处理步骤确保数据质量和准确性,为后续的数据分析和机器学习模型构建奠定基础。
30 4
|
30天前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
346 0
|
2天前
|
数据采集 数据可视化 数据挖掘
数据分析大神养成记:Python+Pandas+Matplotlib助你飞跃!
【6月更文挑战第12天】在数字时代,Python因其强大的数据处理能力和易用性成为数据分析首选工具。结合Pandas(用于高效数据处理)和Matplotlib(用于数据可视化),能助你成为数据分析专家。Python处理数据预处理、分析和可视化,Pandas的DataFrame简化表格数据操作,Matplotlib则提供丰富图表展示数据。掌握这三个库,数据分析之路将更加畅通无阻。
|
2天前
|
JSON 数据挖掘 API
数据分析实战丨基于pygal与requests分析GitHub最受欢迎的Python库
数据分析实战丨基于pygal与requests分析GitHub最受欢迎的Python库
10 2
|
4天前
|
机器学习/深度学习 数据可视化 数据挖掘
python每日可视化分析:从过去到现代数据分析的演进
python每日可视化分析:从过去到现代数据分析的演进
|
12天前
|
Python 数据挖掘 数据可视化
Python数据分析——Pandas与Jupyter Notebook
【6月更文挑战第1天】 本文探讨了如何使用Python的Pandas库和Jupyter Notebook进行数据分析。首先,介绍了安装和设置步骤,然后展示了如何使用Pandas的DataFrame进行数据加载、清洗和基本分析。接着,通过Jupyter Notebook的交互式环境,演示了数据分析和可视化,包括直方图的创建。文章还涉及数据清洗,如处理缺失值,并展示了如何进行高级数据分析,如数据分组和聚合。此外,还提供了将分析结果导出到文件的方法。通过销售数据的完整案例,详细说明了从加载数据到可视化和结果导出的全过程。最后,讨论了进一步的分析和可视化技巧,如销售额趋势、产品销售排名和区域分布,以及
32 2