public void FetchGPXData(string fileName) { string[] separator = { ";" }; bool validFile = true; bool headersFound = false; List <string> headerRows = new List <string>(); int rowNum = 1; int columnCount = 0; int columnContErrors = 0; using (StreamReader sr = File.OpenText(fileName)) { string row = ""; DateTime prevDateTime = DateTime.MinValue; while ((row = sr.ReadLine()) != null) { if (headersFound) { string[] rowValues = row.Split(separator, StringSplitOptions.RemoveEmptyEntries); if (rowValues.Length != columnCount) { columnContErrors += 1; DataRowErrors.Add($"The number of columns {rowValues.Length} in row {rowNum} did not match the number of columns of the headerline"); rowNum++; continue; } if (TimeValidation(rowValues)) { DateTime newDateTime; // Tehdään gpx instanssin luonti sekunnin välein newDateTime = FormGPXTime(row); TimeSpan tp = newDateTime - prevDateTime; if (tp.TotalSeconds >= 1) { MakeGPX(row, columnCount, rowNum); prevDateTime = FormGPXTime(row); // Aloitusaika otsikolle if (GpxRaceTime == DateTime.MinValue) { GpxRaceTime = prevDateTime; } } } } else { FileValidation(headerRows, rowNum, row, ref validFile, ref headersFound, ref separator); if (Regex.IsMatch(row, headerRowPattern)) { columnCount = row.Split(separator, StringSplitOptions.RemoveEmptyEntries).Length; } } rowNum++; } } }
// Haetaan rivit yhdelle tiedostolle private void ReadAndWriteDataFile(string filePath) { string[] separator = { ";" }; bool validFile = true; bool headersFound = false; List <string> headerRows = new List <string>(); int rowNum = 1; using (StreamWriter sw = File.AppendText(TmpFile)) using (StreamReader sr = File.OpenText(filePath)) { string row = ""; while ((row = sr.ReadLine()) != null && validFile && !PastEnd) { if (headersFound) { // Tarkistetaan sopiiko aika -> string splitillä haetaan aika string[] rowValues = row.Split(separator, StringSplitOptions.RemoveEmptyEntries); if (TimeValidation(rowValues, rowNum)) { if (!headersWritten) { foreach (string line in headerRows) { sw.WriteLine(line); } columnCount = headerRows.Last().Split(separator, StringSplitOptions.RemoveEmptyEntries).Length; headerRows.Clear(); headerRows.TrimExcess(); headersWritten = true; } if (rowValues.Length == columnCount) { DataRowCount++; sw.WriteLine(row); } else { DataRowErrors.Add($"There was missing data on row {rowNum}. The row was disregarded."); } } } else { FileValidation(headerRows, rowNum, row, ref validFile, ref headersFound, ref separator); } rowNum++; } if (!validFile) { DataRowErrors.Add($"There was something wrong with the xml section in file:\n{filePath}"); } } }
private void MakeGPX(string row, int columnCount, int rowNumber) { if (GpxLines == null) { GpxLines = new List <GpxLine>(); } string[] rowValues = row.Split(separatorChar); // Rivillä oltava sama määrä sarakkeita kuin otsikollakin if (rowValues.Length != columnCount) { DataRowErrors.Add($"The number of columns {rowValues.Length} in row {rowNumber} did not match to the headerline"); return; } DateTime time; try { time = DateTime.ParseExact(rowValues[23] + " " + rowValues[24], "dd.MM.yyyy HH:mm:ss.fff", cultureInfo); } catch (IndexOutOfRangeException) { DataRowErrors.Add($"Parsing time at row {rowNumber} failed"); return; } if (!Regex.IsMatch(rowValues[25], ConfigurationManager.AppSettings["patLatitude"])) { DataRowErrors.Add($"Latitude at {rowNumber} empty or not in correct format"); } if (!Regex.IsMatch(rowValues[27], ConfigurationManager.AppSettings["patLongitude"])) { DataRowErrors.Add($"Longitude at {rowNumber} empty or not in correct format"); } //GpxLine gpxLine = new GpxLine(aika, arvot[25], arvot[27], arvot[29]); // Tarkistetaan latitude ja longitude GpxLine gpxLine = new GpxLine(time); gpxLine.SetLatitude(rowValues[25]); gpxLine.LatPosition = rowValues[26]; gpxLine.SetLongitude(rowValues[27]); gpxLine.LongPosition = rowValues[28]; GpxLines.Add(gpxLine); }
// Tarkistetaan aika private bool TimeValidation(string[] values, int rowNum) { // ensimmäisessä alkiossa pvm muodossa pp.kk.vvvv ja toisessa aika hh:mm:ss.nnn DateTime eventTime = DateTime.ParseExact(values[0] + " " + values[1], "dd.MM.yyyy HH:mm:ss.fff", cultureInfo); if (eventTime > endTime) { PastEnd = true; return(false); } else if (eventTime >= startTime && eventTime <= endTime) { if (PrevEventTime != null && eventTime - PrevEventTime > maximumTimeStep) { string errMsg; if (rowNum == 14) { errMsg = "Parsing stopped due to a large time step between files."; } else { errMsg = $"Parsing stopped due to a large time step to row {rowNum}."; } DataRowErrors.Add(errMsg); PrevEventTime = eventTime; PastEnd = true; return(false); } PrevEventTime = eventTime; return(true); } else { return(false); } }