Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using PV178.Homeworks.HW05.Model;
  7. using PV178.Homeworks.HW05.Utils;
  8.  
  9. namespace PV178.Homeworks.HW05.Parsers.FuzzyFormatParsers
  10. {
  11.     public class FuzzyFormat1Parser : IGpsParser
  12.     {
  13.         private string Line { get; set; }
  14.         private Regex Gps = new Regex(@"(4\d\.\d{1,6})|(1\d\.\d{1,6})");
  15.         private Regex Degree = new Regex(@"°|deg");
  16.         private List<GpsCoordinates> Coordinates { get; set; }
  17.         private Track Way { get; set; }
  18.         private class Degrees
  19.         {
  20.             public int Deg { get; set; }
  21.             public int Min { get; set; }
  22.             public decimal Sec { get; set; }
  23.         }
  24.  
  25.         public FuzzyFormat1Parser()
  26.         {
  27.             this.Coordinates = new List<GpsCoordinates>();
  28.         }
  29.  
  30.         private void ParseGpsFromDouble()
  31.         {
  32.             string[] numbers; // in right case it should contains only two strings
  33.  
  34.             if (Gps.Matches(Line).Count > 2)
  35.             {
  36.  
  37.                 numbers = Regex.Matches(Line, @"\d\d\.\d{1,6}\s*(n|N|e|E|L|l)")
  38.                     .Cast<Match>()
  39.                     .Select(m => m.Value)
  40.                     .ToArray();
  41.  
  42.                 for (int i = 0; i < numbers.Length; i++)
  43.                 {
  44.                     numbers[i] = Regex.Match(numbers[i], @"\d{2}.\d{1,6}").Value;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 numbers = Gps.Matches(Line)
  50.                     .Cast<Match>()
  51.                     .Select(m => m.Value)
  52.                     .OrderByDescending(m => m)
  53.                     .ToArray();
  54.             }
  55.  
  56.             Coordinates.Add(new GpsCoordinates(
  57.                 Helpers.ParseDouble(numbers[0]),
  58.                 Helpers.ParseDouble(numbers[1])));
  59.         }
  60.  
  61.         private void ParseGpsFromDegree()
  62.         {
  63.             var latitude = new Degrees();
  64.             var longitude = new Degrees();
  65.            
  66.             #region RegexFormats
  67.             string latFormat1 = @"4\d°\s*(.{11})?\d{1,2}'\s*(.{10})?\d{1,2}\.\d{1,4}";
  68.             string latFormat2 = @"(deg.{1,5}\s*|°)4\d(\s*'|.{6,10}\s*)\d{1,2}\s*(''|.{4,12}\s*)\d{1,2}\.\d{1,4}";
  69.             string lonFormat1 = @"(degrees:\s*|°)?1\d(°|\s*')\s*\d{1,2}\s*'{1,2}(.{10})?\s*\d{1,2}\.\d{1,4}";
  70.             string degMinFormat = @"\d{1,2}";
  71.             string secFormat = @"\d{1,2}\.\d{1,4}";
  72.             #endregion
  73.  
  74.             string substring;
  75.             string num;
  76.             int deg;
  77.             int min;
  78.             decimal sec;
  79.  
  80.             if (Regex.IsMatch(Line, latFormat1))
  81.             {
  82.                 substring = Regex.Match(Line, latFormat1).Value;
  83.                 num = Regex.Match(substring, @"4\d(°|')").Value;
  84.                 deg = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
  85.                 num = Regex.Match(substring, degMinFormat + "'").Value;
  86.                 min = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
  87.                 num = Regex.Match(substring, secFormat).Value;
  88.                 sec = Helpers.ParseDecimal(num);
  89.             }
  90.             else
  91.             {
  92.                 substring = Regex.Match(Line, latFormat2).Value;
  93.                 num = Regex.Match(substring, @"°?4\d,?").Value;
  94.                 deg = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
  95.                 num = Regex.Match(substring, @"(min(utes)?:\s*|')" + degMinFormat).Value;
  96.                 min = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
  97.                 num = Regex.Match(substring, secFormat).Value;
  98.                 sec = Helpers.ParseDecimal(num);
  99.             }
  100.  
  101.             latitude.Deg = deg;
  102.             latitude.Sec = sec;
  103.             latitude.Min = min;
  104.  
  105.             substring = Regex.Match(Line, lonFormat1).Value;
  106.             num = Regex.Match(substring, @"(degrees:\s*|°)?1\d°?").Value;
  107.             longitude.Deg = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
  108.             num = Regex.Match(substring, @"('\d{1,2}\s*''|°\s*\d{1,2}')").Value;
  109.             longitude.Min = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
  110.             num = Regex.Match(substring, secFormat).Value;
  111.             longitude.Sec = Helpers.ParseDecimal(num);
  112.  
  113.             Coordinates.Add(new GpsCoordinates(
  114.                 Helpers.ConvertDMSToDD(latitude.Deg, latitude.Min, latitude.Sec),
  115.                 Helpers.ConvertDMSToDD(longitude.Deg, longitude.Min, longitude.Sec)));
  116.         }
  117.  
  118.         private void ReadGpsFromStream(StreamReader stream)
  119.         {
  120.             while (stream.Peek() >= 0)
  121.             {
  122.                 Line = stream.ReadLine();
  123.  
  124.                 if (Degree.IsMatch(Line))
  125.                 {
  126.                     ParseGpsFromDegree();
  127.                 }
  128.                 else
  129.                 {
  130.                     ParseGpsFromDouble();
  131.                 }
  132.             }
  133.  
  134.             Way = new Track(Coordinates);
  135.         }
  136.  
  137.         // TODO
  138.         public Track ParseTrack(string filePath)
  139.         {
  140.             using (StreamReader stream = new StreamReader(filePath))
  141.             {
  142.                 ReadGpsFromStream(stream);
  143.             }
  144.  
  145.             return Way;
  146.         }
  147.  
  148.         public Track ParseTrack(Stream stream)
  149.         {
  150.             using (StreamReader strem = new StreamReader(stream))
  151.             {
  152.                 ReadGpsFromStream(strem);
  153.             }
  154.  
  155.             return Way;
  156.         }
  157.  
  158.         public Track ParseTrack(byte[] bytes)
  159.         {
  160.             using (MemoryStream stream = new MemoryStream(bytes))
  161.             {
  162.                 StreamReader strem = new StreamReader(stream);
  163.                 ReadGpsFromStream(strem);
  164.                 strem.Dispose();
  165.                
  166.             }
  167.  
  168.             return Way;
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement