Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using PV178.Homeworks.HW05.Model;
- using PV178.Homeworks.HW05.Utils;
- namespace PV178.Homeworks.HW05.Parsers.FuzzyFormatParsers
- {
- public class FuzzyFormat1Parser : IGpsParser
- {
- private string Line { get; set; }
- private Regex Gps = new Regex(@"(4\d\.\d{1,6})|(1\d\.\d{1,6})");
- private Regex Degree = new Regex(@"°|deg");
- private List<GpsCoordinates> Coordinates { get; set; }
- private Track Way { get; set; }
- private class Degrees
- {
- public int Deg { get; set; }
- public int Min { get; set; }
- public decimal Sec { get; set; }
- }
- public FuzzyFormat1Parser()
- {
- this.Coordinates = new List<GpsCoordinates>();
- }
- private void ParseGpsFromDouble()
- {
- string[] numbers; // in right case it should contains only two strings
- if (Gps.Matches(Line).Count > 2)
- {
- numbers = Regex.Matches(Line, @"\d\d\.\d{1,6}\s*(n|N|e|E|L|l)")
- .Cast<Match>()
- .Select(m => m.Value)
- .ToArray();
- for (int i = 0; i < numbers.Length; i++)
- {
- numbers[i] = Regex.Match(numbers[i], @"\d{2}.\d{1,6}").Value;
- }
- }
- else
- {
- numbers = Gps.Matches(Line)
- .Cast<Match>()
- .Select(m => m.Value)
- .OrderByDescending(m => m)
- .ToArray();
- }
- Coordinates.Add(new GpsCoordinates(
- Helpers.ParseDouble(numbers[0]),
- Helpers.ParseDouble(numbers[1])));
- }
- private void ParseGpsFromDegree()
- {
- var latitude = new Degrees();
- var longitude = new Degrees();
- #region RegexFormats
- string latFormat1 = @"4\d°\s*(.{11})?\d{1,2}'\s*(.{10})?\d{1,2}\.\d{1,4}";
- 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}";
- string lonFormat1 = @"(degrees:\s*|°)?1\d(°|\s*')\s*\d{1,2}\s*'{1,2}(.{10})?\s*\d{1,2}\.\d{1,4}";
- string degMinFormat = @"\d{1,2}";
- string secFormat = @"\d{1,2}\.\d{1,4}";
- #endregion
- string substring;
- string num;
- int deg;
- int min;
- decimal sec;
- if (Regex.IsMatch(Line, latFormat1))
- {
- substring = Regex.Match(Line, latFormat1).Value;
- num = Regex.Match(substring, @"4\d(°|')").Value;
- deg = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
- num = Regex.Match(substring, degMinFormat + "'").Value;
- min = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
- num = Regex.Match(substring, secFormat).Value;
- sec = Helpers.ParseDecimal(num);
- }
- else
- {
- substring = Regex.Match(Line, latFormat2).Value;
- num = Regex.Match(substring, @"°?4\d,?").Value;
- deg = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
- num = Regex.Match(substring, @"(min(utes)?:\s*|')" + degMinFormat).Value;
- min = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
- num = Regex.Match(substring, secFormat).Value;
- sec = Helpers.ParseDecimal(num);
- }
- latitude.Deg = deg;
- latitude.Sec = sec;
- latitude.Min = min;
- substring = Regex.Match(Line, lonFormat1).Value;
- num = Regex.Match(substring, @"(degrees:\s*|°)?1\d°?").Value;
- longitude.Deg = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
- num = Regex.Match(substring, @"('\d{1,2}\s*''|°\s*\d{1,2}')").Value;
- longitude.Min = (int)Helpers.ParseDouble(Regex.Match(num, degMinFormat).Value);
- num = Regex.Match(substring, secFormat).Value;
- longitude.Sec = Helpers.ParseDecimal(num);
- Coordinates.Add(new GpsCoordinates(
- Helpers.ConvertDMSToDD(latitude.Deg, latitude.Min, latitude.Sec),
- Helpers.ConvertDMSToDD(longitude.Deg, longitude.Min, longitude.Sec)));
- }
- private void ReadGpsFromStream(StreamReader stream)
- {
- while (stream.Peek() >= 0)
- {
- Line = stream.ReadLine();
- if (Degree.IsMatch(Line))
- {
- ParseGpsFromDegree();
- }
- else
- {
- ParseGpsFromDouble();
- }
- }
- Way = new Track(Coordinates);
- }
- // TODO
- public Track ParseTrack(string filePath)
- {
- using (StreamReader stream = new StreamReader(filePath))
- {
- ReadGpsFromStream(stream);
- }
- return Way;
- }
- public Track ParseTrack(Stream stream)
- {
- using (StreamReader strem = new StreamReader(stream))
- {
- ReadGpsFromStream(strem);
- }
- return Way;
- }
- public Track ParseTrack(byte[] bytes)
- {
- using (MemoryStream stream = new MemoryStream(bytes))
- {
- StreamReader strem = new StreamReader(stream);
- ReadGpsFromStream(strem);
- strem.Dispose();
- }
- return Way;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement