public void Test2() { var path = @"Data\200122_12-28-09_1.csv"; // "," dateTime: "2020/01/22 12:25:55.734" //var path = @"Data\DJIFlightRecord_2020-01-22_13-25-55-verbose.csv"; // ";" dateTime:"28:52.3" using (TextFieldParser csvParser = new TextFieldParser(path)) { csvParser.CommentTokens = new string[] { "#" }; csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; //// Skip the row with the column names //csvParser.ReadLine(); //var noko = File.ReadAllLines(path); //Processing row string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers); //foreach (string field in fields) //{ var uavLogs = new List <UavLog>(); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } string csv = String.Join(",", uavLogs); var sortUavList1 = uavLogs; sortUavList1.OrderBy(x => x.DateTime).ToList(); var sortUavList = uavLogs; sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime)); TimeSpan startTimeSpan = Helpers.GetTimeSpan("00:01:00.150"); TimeSpan endTimeStamp = Helpers.GetTimeSpan("00:02:35.150"); var newUavlogs = Helpers.TrimUavLogs(sortUavList, startTimeSpan, endTimeStamp); var filePath = Path.Combine(@"C:\Temp\", "sort_test.csv"); var csvVideoLogs = CsvUtilities.ToCsv(",", newUavlogs); var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs); } }
private static List <Election> CreateElection(List <CountyDataFormat> countyData, string root) { // Check if the electionTypeId is valid if (!Directory.Exists(root)) { throw new KeyNotFoundException($"Could not find any directory with the path: {root}"); } List <ElectionFormat> elections = CsvUtilities.CsvToList <ElectionFormat>(Path.Combine(root, "Elections.csv")); List <Election> electionModels = ModelBuilder.BuildElections(elections); string[] electionFiles = Directory.GetFiles(root); if (electionFiles.Length != elections.Count + 1) { throw new ArgumentException($"The number of elections in {root} does not match the number found in Elections.csv."); } // Iterate through the elections foreach (Election electionModel in electionModels) { string path = Path.Combine(root, electionModel.Year + ".csv"); List <County> counties = CreateCounties(countyData.Where(cD => cD.Year == electionModel.Year), path); electionModel.Counties.AddRange(counties); } return(electionModels); }
private static List <ElectionType> CreateElectionTypes(string root) { // Check if the countryId is valid if (!Directory.Exists(root)) { throw new KeyNotFoundException($"Could not find any directory with the path: {root}"); } // Get a list of ElectionTypeFormats List <ElectionTypeFormat> electionTypes = CsvUtilities.CsvToList <ElectionTypeFormat>(Path.Combine(root, "ElectionTypes.csv")); List <ElectionType> electionTypeModels = ModelBuilder.BuildElectionTypes(electionTypes); List <CountyDataFormat> countyData = CsvUtilities.CsvToList <CountyDataFormat>(Path.Combine(root, "CountyData.csv")); // Iterate through the country's election types string[] electionTypeFiles = Directory.GetDirectories(root); if (electionTypeFiles.Length != electionTypes.Count) { throw new ArgumentException($"The number of directories in {root} does not match the number found in ElectionTypes.csv."); } foreach (ElectionType electionTypeModel in electionTypeModels) { string path = Path.Combine(root, electionTypeModel.ElectionTypeCode); List <Election> elections = CreateElection(countyData, path); electionTypeModel.Elections.AddRange(elections); } return(electionTypeModels); }
public async Task <IActionResult> GetCsvVideoPosition(IFormFile uavLogsCsv, string time) { long csvFilLength = uavLogsCsv.Length; long imageFilLength = uavLogsCsv.Length; string csvFileExtension = Path.GetExtension(uavLogsCsv.FileName).ToLower(); if (csvFilLength > 0) { if (csvFileExtension != ".csv") { return(BadRequest("wrong CSV file format")); } } else { return(BadRequest("CSV File Not Found")); } List <UavLog> uavLogs = new List <UavLog>(); using (TextFieldParser csvParser = new TextFieldParser(uavLogsCsv.OpenReadStream())) { uavLogs = CsvUtilities.GetUavLogFromTextFile(csvParser); } TimeSpan timeSpan = Helpers.GetTimeSpan(time); var photolog = Helpers.GetUavLogFromVideoTimeStamp(timeSpan, uavLogs);//"03:56:22" return(Ok(photolog)); }
public DataObject MakeDataObject() { var data = new DataObject(); data.SetData(typeof(WordListEntries), this); var sb = new StringBuilder(); foreach (var item in Items) { CsvUtilities.AppendCSVValue(sb, item.Phrase); sb.Append(','); CsvUtilities.AppendCSVValue(sb, item.Translation); sb.AppendLine(); } data.SetText(sb.ToString(), TextDataFormat.CommaSeparatedValue); sb.Length = 0; foreach (var item in Items) { sb.Append(item.Phrase).Append(" -- ").AppendLine(item.Translation); } data.SetText(sb.ToString()); // TODO: More formats return(data); }
DataObject MakeDataObjectFromSelection() { var data = new DataObject(); var selection = new List <TranslationPair>(GetSelectedTranslationPairs()).ToArray(); data.SetData(typeof(TranslationPair[]), selection); var sb = new StringBuilder(); // Add CSV formatted data. foreach (var sr in selection) { CsvUtilities.AppendCSVValue(sb, sr.Phrase); sb.Append(','); CsvUtilities.AppendCSVValue(sb, sr.Translation); sb.AppendLine(); } data.SetData(DataFormats.CommaSeparatedValue, sb.ToString()); // Add plain text representation. sb.Length = 0; foreach (var sr in selection) { sb.Append(sr.Phrase).Append(" -- ").AppendLine(sr.Translation); } data.SetData(DataFormats.UnicodeText, sb.ToString()); data.SetData(DataFormats.Text, sb.ToString()); return(data); }
/// <summary> /// Parses CountyData.csv -> DistrictMetrics /// </summary> /// <param name="path"></param> /// <returns></returns> private static IEnumerable <DistrictMetrics> ParseDistrictMetrics(string path) { string filePath = Path.Combine(path, "CountyData.csv"); IEnumerable <CountyDataFormat> countyData = CsvUtilities.CsvToList <CountyDataFormat>(filePath); IEnumerable <DistrictMetrics> districtMetricsModels = ModelBuilder.BuildDistrictMetrics(countyData); return(districtMetricsModels); }
private string[][] ExtractColumnNames( string directory, string[] fileNames) { return(fileNames .Select(f => CsvUtilities.ReadHeader(directory, f)) .ToArray()); }
public static void Initialize(VDContext context) { context.Database.EnsureCreated(); if (!context.VDModels.Any()) { VDModel[] entities = CsvUtilities.CsvToVdArray("Data/States/NO/ParliamentaryElection/2017.csv"); context.VDModels.AddRange(entities); context.SaveChanges(); } }
public void TestDictionaryToVideoInfoModel() { var path = @"Data\DJIFlightRecord_2020-01-22_[13-25-55]-TxtLogToCsv.csv"; // "," dateTime: "2020/01/22 12:25:55.734" //var path = @"Data\DJIFlightRecord_2020-01-22_13-25-55-verbose.csv"; // ";" dateTime:"28:52.3" Dictionary <int, List <UavLog> > videosLogs; using (TextFieldParser csvParser = new TextFieldParser(path)) { csvParser.CommentTokens = new string[] { "#" }; csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; //// Skip the row with the column names //csvParser.ReadLine(); //var noko = File.ReadAllLines(path); //Processing row string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers); //foreach (string field in fields) //{ var uavLogs = new List <UavLog>(); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); var index = djiHeaderDictionary["VideoRecordTime"]; var noko = fields[index]; if (fields.Length > index && fields[index] != "0") { uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } } string csv = String.Join(",", uavLogs); videosLogs = Helpers.SplitVideosFromUavLog(uavLogs); } var videoInfoModels = Helpers.GetVideoInfoModels(videosLogs); }
/// <summary> /// Parses Elections.csv -> ElectionParameters /// </summary> private static IEnumerable <ElectionParameters> ParseElectionParameters( string path, IReadOnlyDictionary <int, int> yearTotalVotesMap) { string filePath = Path.Combine(path, "Elections.csv"); IEnumerable <ElectionFormat> electionData = CsvUtilities.CsvToList <ElectionFormat>(filePath); return(ModelBuilder.BuildElectionParameters( electionData, "PE", yearTotalVotesMap)); }
private static List <County> CreateCounties(IEnumerable <CountyDataFormat> countyData, string root) { List <ResultFormat> resultFormat = CsvUtilities.CsvToList <ResultFormat>(root); List <County> countyModels = ModelBuilder.BuildCounties(resultFormat, countyData); foreach (County county in countyModels) { List <Result> results = CreateResults(resultFormat.Where(r => r.Fylkenavn.Equals(county.Name))); county.Results.AddRange(results); } return(countyModels); }
/// <summary> /// Initializes the database, if the db is empty this method will build a model to seed it. /// </summary> /// <param name="context">The context to be initialized.</param> /// <param name="logger">Where to log any issues.</param> public static void Initialize(ElectionContext context, ILogger logger) { string root = Path.Combine("Data", "Countries"); // Make sure the DB is ready and empty context.Database.EnsureCreated(); if (context.Countries.Any()) { return; } // Catch all Argument/KeyNotFound/CsvFileFormatExceptions thrown by model validation try { string path = Path.Combine(root, "Countries.csv"); List <CountryFormat> countries = CsvUtilities.CsvToList <CountryFormat>(path); List <Country> countryModels = ModelBuilder.BuildCountries(countries); context.Countries.AddRange(countryModels); // Iterate through countries string[] countryDirectories = Directory.GetDirectories(root); if (countryDirectories.Length != countryModels.Count) { throw new ArgumentException($"The number of directories in {root} does not match the number found in States.csv"); } foreach (Country country in countryModels) { path = Path.Combine(root, country.CountryCode); List <ElectionType> electionTypes = CreateElectionTypes(path); country.ElectionTypes.AddRange(electionTypes); HashSet <int> validated = new HashSet <int>(); CustomValidation.ValidateCountry(country, validated); } context.SaveChanges(); } catch (ArgumentException argumentException) { logger.LogError(argumentException, "The data results in an illegal model and could not be built."); } catch (KeyNotFoundException keyNotFoundException) { logger.LogError(keyNotFoundException, "The directory name does not match any ID in the dictionary."); } catch (CsvFileFormatException csvFileFormatException) { logger.LogError(csvFileFormatException, "The csv file has a malformed format."); } }
public void GetUavLogFromVideoTimeStampTest() { List <UavLog> uavLogs = new List <UavLog>(); var path = @"Data\200122_12-28-09_1.csv"; // "," dateTime: "2020/01/22 12:25:55.734" using (TextFieldParser csvParser = new TextFieldParser(path)) { csvParser.CommentTokens = new string[] { "#" }; csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetHeaderDictionary(headers); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } string csv = String.Join(",", uavLogs); } var sortUavList = uavLogs; sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime)); //var noko = Helpers.GetfirstLog(uavLogs); TimeSpan timeSpanp = Helpers.GetTimeSpan("12:36.15"); var sortedUavLogs = Helpers.FilterUavlosAndSort(uavLogs); var videoLenght = Helpers.GetVideoLenghtInMilliseconds(sortedUavLogs); var videoLengh = Helpers.ConvertMilisecondsToHMSmm(videoLenght); if (timeSpanp.TotalMilliseconds < videoLenght) { var photolog = Helpers.GetUavLogFromVideoTimeStamp(timeSpanp, uavLogs); } else { } }
static void Main(string[] args) { try { var fileReader = new CsvUtilities <Employee, EmployeeMap>(); var employees = fileReader.ReadFile(@"CsvTemplates\employee.csv"); foreach (var employee in employees) { employee.Show(); } Console.ReadKey(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } }
public void TrimUavLogs() { List <UavLog> uavLogs = new List <UavLog>(); var path = @"Data\200122_12-28-09_1.csv"; // "," dateTime: "2020/01/22 12:25:55.734" using (TextFieldParser csvParser = new TextFieldParser(path)) { csvParser.CommentTokens = new string[] { "#" }; csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetHeaderDictionary(headers); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } string csv = String.Join(",", uavLogs); } var sortUavList = uavLogs; sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime)); //var noko = Helpers.GetfirstLog(uavLogs); TimeSpan startTimeSpan = Helpers.GetTimeSpan("01:00.15"); TimeSpan endTimeStamp = Helpers.GetTimeSpan("01:36.15"); var newUavlogs = Helpers.TrimUavLogs(sortUavList, startTimeSpan, endTimeStamp); var filePath = Path.Combine(@"C:\Temp\", "sort_test.csv"); var csvVideoLogs = CsvUtilities.ToCsv(",", newUavlogs); var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs); }
/// <summary> /// Parses <c><year>.csv</c> -> ResultFormat /// </summary> /// <param name="root"></param> /// <returns></returns> private static Dictionary <int, List <ResultFormat> > ParseResultFormat(string root) { Dictionary <int, List <ResultFormat> > resultFormats = new(); string[] filePaths = Directory.GetFiles(root); foreach (string filePath in filePaths) { if (Path.GetFileName(filePath) == "Elections.csv") { continue; } int electionYear = int.Parse(Path.GetFileNameWithoutExtension(filePath)); resultFormats[electionYear] = CsvUtilities.CsvToList <ResultFormat>(filePath); } return(resultFormats); }
public FileStreamResult Export() { List <NewsletterSubscriber> newsLettersubscribers = _context.Users .Where(x => x.IsNewsletterSubscriber == true) .Select(x => new NewsletterSubscriber() { Email = x.Email, Forename = x.Forename, Surname = x.Surname }).ToList(); var result = CsvUtilities.WriteCsvToMemory(newsLettersubscribers); var memoryStream = new MemoryStream(result); return(new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "mailchimpexport.csv" }); }
// Detect comma-separated/tab-separated based on the paste content. private void PasteCsvClick(object sender, EventArgs e) { int validCSV, validTSV; List <List <string> > csv, tsv; // TODO: Look at getting CSV directly from the clipboard. // It's more work than it sounds: there doesn't seem to be a consensus on what the exact // format of that data is. // Excel in particular writes it in UTF-8 (or the windows code page, according to some) // with a null terminator. // It's plain text: use guesswork to figure out if it's TSV or CSV. // Tab-separated is rarer, so if it works with tabs, it's probably that. string text = Clipboard.GetText(); csv = CsvUtilities.ParseCSV(',', text, out validCSV); tsv = CsvUtilities.ParseCSV('\t', text, out validTSV); Paste(validTSV >= validCSV ? tsv : csv, null); }
static WordListEntries FromDelimitedText(string text) { if (text != null) { int validCSV, validTSV; var csv = CsvUtilities.ParseCSV(',', text, out validCSV); var tsv = CsvUtilities.ParseCSV('\t', text, out validTSV); var lines = validTSV >= validCSV ? tsv : csv; var entries = new List <WordListEntry>(); foreach (var line in lines) { if (line.Count >= 2) { entries.Add(new WordListEntry(null, line[0], line[1])); } } return(new WordListEntries(null, entries)); } return(null); }
public async Task <FileStreamResult> DownloadPersonalData() { // get user var user = await _userManager.GetUserAsync(this.User); var applicationUser = _context.Users .Include(x => x.Member) .Include(x => x.Staff) .ThenInclude(staff => staff.EmergencyContact) .Include(x => x.Decoration) .Include(x => x.Regimental) .ThenInclude(regimental => regimental.Regiment) .Include(x => x.Volunteer) .ThenInclude(volunteer => volunteer.Expertise) .Include(x => x.Volunteer) .ThenInclude(volunteer => volunteer.EmergencyContact) .FirstOrDefault(x => x.Id == user.Id); var personalData = new PersonalData() { Title = applicationUser.Title, Forename = applicationUser.Forename, Initial = applicationUser.Initial, Surname = applicationUser.Surname, Decoration = applicationUser.Decoration.Name, AddressLine1 = applicationUser.AddressLine1, AddressLine2 = applicationUser.AddressLine2, AddressLine3 = applicationUser.AddressLine3, County = applicationUser.County, Country = applicationUser.Country, Postcode = applicationUser.Postcode, PhoneNumber = applicationUser.PhoneNumber, MobileTelNo = applicationUser.MobileTelNo, Email = applicationUser.Email, WorkEmail = applicationUser.WorkEmail, SubscribedToNewsLetter = applicationUser.IsNewsletterSubscriber ? "Yes" : "No", }; if (applicationUser.Member != null) { personalData.MemberType = applicationUser.Member.Type; personalData.MemberNumber = applicationUser.Member.Number; personalData.MemberStartDate = applicationUser.Member.StartDate; personalData.MemberExpiryDate = applicationUser.Member.ExpiryDate; } if (applicationUser.Staff != null) { personalData.EmergencyContactName = applicationUser.Staff.EmergencyContact.Name; personalData.EmergencyContactRelation = applicationUser.Staff.EmergencyContact.Relation; personalData.EmergencyContactTelNo = applicationUser.Staff.EmergencyContact.TelNo; personalData.EndOfEmployment = applicationUser.Staff.LeaveDate ?? null; } if (applicationUser.Volunteer != null) { // override staff emergency contact personalData.EmergencyContactName = applicationUser.Volunteer.EmergencyContact.Name; personalData.EmergencyContactRelation = applicationUser.Volunteer.EmergencyContact.Relation; personalData.EmergencyContactTelNo = applicationUser.Volunteer.EmergencyContact.TelNo; personalData.Expertise = applicationUser.Volunteer.Expertise.Name; } if (applicationUser.Regimental != null) { personalData.Regiment = applicationUser.Regimental.Regiment.Name; } List <PersonalData> list = new List <PersonalData>() { personalData }; var result = CsvUtilities.WriteCsvToMemory(list); var memoryStream = new MemoryStream(result); return(new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "export.csv" }); }
// TODO: Put exception messages into the resources. // public ICsvDataTable Parse(TextReader inputTextReader) { if (inputTextReader == null) { throw new ArgumentNullException("inputTextReader"); } P_Table table = null; var rows = new LinkedList <Row>(); try { string textLine; string columnName; var rowCtor = default(Func <ICsvDataTable, IEnumerable <string>, Row>); var textLineNumber = 0; var textLineValues = new List <string>(48); var columnNamesOrder = default(Dictionary <string, int>); for (;;) { if (textLineNumber - 1 >= MaxRowCount) { throw new DigitalFlareApplicationException(string.Format("Источник данных содержит слишком много строк. Макс. допустимое количество строк '{0}'.", MaxRowCount.TextViewInvariant("d"))); } textLine = inputTextReader.ReadLine(); if (textLine == null) { break; } textLineNumber++; if (string.IsNullOrWhiteSpace(textLine)) { throw new DigitalFlareApplicationException(string.Format("Несоответствие данных источника формату CSV. Данные, считанные из '{0}' строки источника представляют либо пустую строку, либо строку, содержащую только пробельные символы.", textLineNumber.TextViewInvariant("d"))); } if (textLineNumber == 1) { // Table columns line. // textLineValues.Clear(); CsvUtilities.SplitLine(textLine, textLineNumber, textLineValues); if (textLineValues.Count < 1) { throw new DigitalFlareApplicationException("Несоответствие данных источника формату CSV. Невозможно определить структуру CSV-таблицы."); } columnNamesOrder = new Dictionary <string, int>(ColumnNameEqualityComparer); for (var i = 0; i < textLineValues.Count; i++) { columnName = textLineValues[i]; if (string.IsNullOrEmpty(columnName)) { throw new DigitalFlareApplicationException(string.Format("Несоответствие данных источника формату CSV. Для колонки в '{0}' позиции не задано имя.", (i + 1).TextViewInvariant("d"))); } if (columnName.Length > MaxColumnNameLength) { throw new DigitalFlareApplicationException(string.Format("Для колонки в '{0}' позиции задано слишком длинное имя. Макс. длина имени составляет '{1}' символов.", (i + 1).TextViewInvariant("d"), MaxColumnNameLength.TextViewInvariant("d"))); } if (columnNamesOrder.ContainsKey(columnName)) { throw new DigitalFlareApplicationException(string.Format("Несоответствие данных источника формату CSV. Для колонки в '{0}' позиции задано неуникальное имя '{1}'.", (i + 1).TextViewInvariant("d"), columnName.TextView())); } columnNamesOrder.Add(columnName, i); } table = new P_Table(P_GenerateRowType(columnNamesOrder, ColumnNameComparer), ColumnNameEqualityComparer); table.Internal_SetColumns(columnNamesOrder.OrderBy(x => x.Value).Select(i => new P_Column(table, i.Key))); } else { // Table row line. // textLineValues.Clear(); CsvUtilities.SplitLine(textLine, textLineNumber, textLineValues); if (textLineValues.Count != columnNamesOrder.Count) { throw new DigitalFlareApplicationException(string.Format("Несоответствие данных источника формату CSV. Строка данных (номер строки '{0}') не соответствует структуре таблицы по количеству колонок в таблице.", textLineNumber.TextViewInvariant("d"))); } if (rowCtor == null) { rowCtor = ActivationUtilities.RequireConstructor <ICsvDataTable, IEnumerable <string>, Row>(concreteType: table.RowType); } rows.AddLast(rowCtor(table, textLineValues)); } } if (table == null) { throw new DigitalFlareApplicationException("Источник данных не содержит какого-либо содержимого, на основе которого можно было бы создать CSV-таблицу."); } table.Internal_SetRows(rows); return(table); } catch (Exception firstException) { if (firstException.IsCritical()) { throw; } DisposableUtilities.Dispose(firstException, table); throw new DigitalFlareApplicationException("Во время создания CSV-таблицы из указанного источника данных возникла ошибка.", firstException); } }
public async Task <IActionResult> TrimCsvLog(IFormFile uavLogsCsv, string startTime, string endTime) { long csvFilLength = uavLogsCsv.Length; string csvFileExtension = Path.GetExtension(uavLogsCsv.FileName).ToLower(); if (csvFilLength > 0) { if (csvFileExtension != ".csv") { return(BadRequest("wrong CSV file format")); } } else { return(BadRequest("CSV File Not Found")); } List <UavLog> uavLogs = new List <UavLog>(); using (TextFieldParser csvParser = new TextFieldParser(uavLogsCsv.OpenReadStream())) { csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetHeaderDictionary(headers); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } string csv = String.Join(",", uavLogs); } var sortUavList = uavLogs; sortUavList.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime)); //var newUavlogs = Helpers.TrimUavLogs(sortUavList, "01:00:22", "01:36:22"); TimeSpan startTimeSpan = Helpers.GetTimeSpan(startTime); TimeSpan endTimeSpan = Helpers.GetTimeSpan(endTime); if (startTimeSpan == TimeSpan.Zero) { return(BadRequest($"cant get video start time from string '{startTime}'")); } if (endTimeSpan == TimeSpan.Zero) { return(BadRequest($"cant get video end time from string '{endTime}'")); } var newUavlogs = Helpers.TrimUavLogs(sortUavList, startTimeSpan, endTimeSpan); var videoInfoModels = Helpers.GetVideoInfoModels(newUavlogs); if (videoInfoModels != null) { var csvVideoInfoModels = CsvUtilities.ToCsv(",", videoInfoModels); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(uavLogsCsv.FileName); var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels); // convert string to stream byte[] byteArray = Encoding.UTF8.GetBytes(string.Concat(csvVideoInfoModels)); var stream = new MemoryStream(byteArray); return(File(stream, "application/octet-stream", $"{ fileNameWithoutExtension}_resume.csv")); } return(BadRequest("No file Created")); }
public async Task <IActionResult> GetCsvVideoLogInfo(IFormFile djiCsvLog) { long size = djiCsvLog.Length; string extension = Path.GetExtension(djiCsvLog.FileName).ToLower(); if (size > 0) { if (extension != ".csv") { return(BadRequest("wrong file format")); } } else { return(BadRequest("CSV File Not Found")); } List <VideoInfoModel> videoInfoModels; using (TextFieldParser csvParser = new TextFieldParser(djiCsvLog.OpenReadStream())) { csvParser.CommentTokens = new string[] { "#" }; csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; //Processing row string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers); var uavLogs = new List <UavLog>(); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); var index = djiHeaderDictionary["VideoRecordTime"]; var noko = fields[index]; if (fields.Length > index && !string.IsNullOrEmpty(fields[index]) && fields[index] != "0") { uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } } if (!uavLogs.Any()) { return(BadRequest("No Video log found in file")); } var csv = String.Join(",", uavLogs); //TODO not working //var uavlogsSort = uavLogs.OrderBy(l => l.DateTime).ToList(); var dictionarylog = Helpers.SplitVideosFromUavLog(uavLogs); var video1LenghInMilliseconds = Helpers.GetVideoLenghtInMilliseconds(dictionarylog.FirstOrDefault().Value); foreach (var videologs in dictionarylog) { var csvVideoLogs = CsvUtilities.ToCsv(",", videologs.Value); var filename = $"{videologs.Value.FirstOrDefault().DateTime.ToString("_yyyy-MM-dd_HH-mm-ss")}_{videologs.Key}.csv"; var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", filename), csvVideoLogs); } videoInfoModels = Helpers.GetVideoInfoModels(dictionarylog); } if (videoInfoModels != null) { var csvVideoInfoModels = CsvUtilities.ToCsv(",", videoInfoModels); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(djiCsvLog.FileName); var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels); return(Ok(videoInfoModels)); } return(BadRequest("Something Went wrong")); }
public async Task <IActionResult> GetCsvFromSrt(IFormFile srtLog) { long size = srtLog.Length; string extension = Path.GetExtension(srtLog.FileName).ToLower(); if (size > 0) { if (!extension.Equals(".SRT", StringComparison.OrdinalIgnoreCase)) { return(BadRequest("wrong file format")); } } else { return(BadRequest("SRT File Not Found")); } string line; DjiSrtVideoLogModel djiSrtVideoLogModel = new DjiSrtVideoLogModel(); List <DjiSrtVideoLogModel> djiSrtVideoLogModels = new List <DjiSrtVideoLogModel>(); try { using (System.IO.StreamReader file = new System.IO.StreamReader(srtLog.OpenReadStream())) { int lineCount = 0; int id = 0; while ((line = file.ReadLine()) != null) { int idTemp; if (int.TryParse(line, out idTemp)) { djiSrtVideoLogModel = new DjiSrtVideoLogModel(); id = idTemp; lineCount++; } if (id >= 1) { switch (lineCount) { case 1: djiSrtVideoLogModel.Id = id; lineCount++; break; case 2: djiSrtVideoLogModel = SrtConverter.GetTimeStamp(line, ref djiSrtVideoLogModel); lineCount++; break; case 3: djiSrtVideoLogModel = SrtConverter.GetHomePointAndDateTime(line, ref djiSrtVideoLogModel); lineCount++; break; case 4: djiSrtVideoLogModel = SrtConverter.GetGpsPointAndBarometer(line, ref djiSrtVideoLogModel); lineCount++; break; case 5: djiSrtVideoLogModel = SrtConverter.GetCameraInfo(line, ref djiSrtVideoLogModel); djiSrtVideoLogModels.Add(djiSrtVideoLogModel); lineCount = 0; break; default: break; } } } if (djiSrtVideoLogModels.Any()) { var csvVideoInfoModels = CsvUtilities.ToCsv(",", djiSrtVideoLogModels); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(srtLog.FileName); var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels); return(Ok("Ok creating Srt to Csv")); } else { return(BadRequest("djiSrtVideoLogModels dont have any record")); } } } catch (Exception) { return(BadRequest("Problem saving Srt to csv")); } }
private void GetGpsLocationFormVideoLog_Click(object videoLogCsv, EventArgs e) { DialogResult result = openFileDialog.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string videoLogCsvPath = openFileDialog.FileName; filePathTextBox.Text = string.Format("{0}/{1}", Path.GetDirectoryName(videoLogCsvPath), videoLogCsvPath); filePathTextBox.Refresh(); DialogResult messageResult = MessageBox.Show($"You want to get screenshot '{ScreenShotTimeStamptextBox1.Text}' GPS position from file:'{Path.GetFileName(videoLogCsvPath)}'?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (messageResult == DialogResult.Yes) { try { string extension = Path.GetExtension(videoLogCsvPath).ToLower(); if (extension != ".csv") { string message = "Wrong File Type"; string title = "Error"; MessageBox.Show(message, title); GetGpsLocationFormVideoLog.PerformClick(); } else { List <UavLog> uavLogs = new List <UavLog>(); using (TextFieldParser csvParser = new TextFieldParser(videoLogCsvPath)) { uavLogs = CsvUtilities.GetUavLogFromTextFile(csvParser); } var sortedUavLogs = Helpers.FilterUavlosAndSort(uavLogs); var videoLenght = Helpers.GetVideoLenghtInMilliseconds(sortedUavLogs); var videoLengh = Helpers.ConvertMilisecondsToHMSmm(videoLenght); if (_timeSpan.TotalMilliseconds < videoLenght) { var photolog = Helpers.GetUavLogFromVideoTimeStamp(_timeSpan, uavLogs); jsonOutput.Text = JsonConvert.SerializeObject(photolog); jsonOutput.ScrollBars = ScrollBars.Vertical; jsonOutput.WordWrap = false; } else { MessageBox.Show($"Screenshot time {ScreenShotTimeStamptextBox1.Text} is bigger than the video time {videoLengh}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ScreenShotTimeStamptextBox1.Focus(); GetGpsLocationFormVideoLog.Enabled = false; filePathTextBox.Text = ""; filePathTextBox.Refresh(); } } } catch { // } } else { GetGpsLocationFormVideoLog.PerformClick(); } } }
private void LoadDjiCsv_Click(object sender, EventArgs e) { int size = -1; DialogResult result = openFileDialog.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string djiCsvLog = openFileDialog.FileName; filePathTextBox.Text = string.Format("{0}/{1}", Path.GetDirectoryName(djiCsvLog), djiCsvLog); filePathTextBox.Refresh(); DialogResult messageResult = MessageBox.Show($"Do you wanna transform: '{djiCsvLog}'?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (messageResult == DialogResult.Yes) { try { string extension = Path.GetExtension(djiCsvLog).ToLower(); if (extension != ".csv") { string message = "Wrong File Type"; string title = "Error"; MessageBox.Show(message, title); } List <VideoInfoModel> videoInfoModels; var uavLogs = new List <UavLog>(); using (TextFieldParser csvParser = new TextFieldParser(djiCsvLog)) { uavLogs = CsvUtilities.GetUavLogFromDjiCsv(csvParser); } if (!uavLogs.Any()) { string message = "No Video log found in file"; string title = "Error"; MessageBox.Show(message, title); } var csv = String.Join(",", uavLogs); var dictionarylog = Helpers.SplitVideosFromUavLog(uavLogs); var video1LenghInMilliseconds = Helpers.GetVideoLenghtInMilliseconds(dictionarylog.FirstOrDefault().Value); foreach (var videologs in dictionarylog) { var csvVideoLogs = CsvUtilities.ToCsv(",", videologs.Value); var filename = $"{videologs.Value.FirstOrDefault().DateTime.ToString("_yyyy-MM-dd_HH-mm-ss")}_{videologs.Key}.csv"; var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", filename), csvVideoLogs); } videoInfoModels = Helpers.GetVideoInfoModels(dictionarylog); if (videoInfoModels != null) { jsonOutput.Text = JArray.FromObject(videoInfoModels).ToString(); jsonOutput.ScrollBars = ScrollBars.Vertical; jsonOutput.WordWrap = false; //var csvVideoInfoModels = CsvUtilities.ToCsv(",", videoInfoModels); //var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(djiCsvLog); //var saved = CsvUtilities.SaveCsvTofile(Path.Combine(@"C:\Temp\", $"{fileNameWithoutExtension}_resume.csv"), csvVideoInfoModels); string message = "File Saved"; string title = "OK"; MessageBox.Show(message, title); } } catch (IOException) { string message = "Something Went wrong"; string title = "Error"; MessageBox.Show(message, title); } } } }
public void Test1() { var path = @"Data\DJIFlightRecord_2020-01-22_[14-00-01]-TxtLogToCsv.csv"; // "," dateTime: "2020/01/22 12:25:55.734" //var path = @"Data\DJIFlightRecord_2020-01-22_13-25-55-verbose.csv"; // ";" dateTime:"28:52.3" using (TextFieldParser csvParser = new TextFieldParser(path)) { csvParser.CommentTokens = new string[] { "#" }; csvParser.SetDelimiters(new string[] { "," }); csvParser.HasFieldsEnclosedInQuotes = true; //// Skip the row with the column names //csvParser.ReadLine(); //var noko = File.ReadAllLines(path); //Processing row string[] headers = csvParser.ReadFields(); var djiHeaderDictionary = CsvUtilities.GetDjiHeaderDictionary(headers); //foreach (string field in fields) //{ var uavLogs = new List <UavLog>(); if (djiHeaderDictionary.Any()) { int rowNumber = 1; while (csvParser.PeekChars(1) != null) { rowNumber++; string[] fields = csvParser.ReadFields(); var index = djiHeaderDictionary["VideoRecordTime"]; var noko = fields[index]; if (fields.Length > index && fields[index] != "0") { uavLogs.Add(CsvUtilities.GetUavLog(fields, djiHeaderDictionary, rowNumber)); } } } string csv = String.Join(",", uavLogs); var dictionarylog = Helpers.SplitVideosFromUavLog(uavLogs); var video1LenghInMilliseconds = Helpers.GetVideoLenghtInMilliseconds(dictionarylog[2]); var time = Helpers.ConvertMilisecondsToHMSmm(video1LenghInMilliseconds); TimeSpan timeSpan = Helpers.GetTimeSpan("03:56:22"); var photolog = Helpers.GetUavLogFromVideoTimeStamp(timeSpan, dictionarylog[2]); string filePath = null; //foreach (var videologs in dictionarylog) //{ // var csvVideoLogs = CsvUtilities.ToCsv(",", videologs.Value); // var filename = $"{videologs.Value.FirstOrDefault().DateTime.ToString("yyMMdd")}_{videologs.Key}.csv"; // filePath = Path.Combine(@"C:\Temp\", filename); // var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs); //} filePath = Path.Combine(@"C:\Temp\", "noko.csv"); var videoInfoModels = Helpers.GetVideoInfoModels(dictionarylog); var csvVideoLogs = CsvUtilities.ToCsv(",", videoInfoModels.ToArray()); var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs); CsvUtilities.ConvertCalssToCsv(videoInfoModels.ToArray(), filePath); //var csvString = CsvUtilities.ToCsv(",", uavLogs); //var saved = CsvUtilities.SaveCsvTofile(@"C:\Temp\WriteLines2.csv", csvString); } }
public void TrimUavLogs() { int counter = 0; string line; // Read the file and display it line by line. DjiSrtVideoLogModel djiSrtVideoLogModel = new DjiSrtVideoLogModel(); List <DjiSrtVideoLogModel> djiSrtVideoLogModels = new List <DjiSrtVideoLogModel>(); using (System.IO.StreamReader file = new System.IO.StreamReader(@"H:\Code\UavLogTool\UavLogToolTest\Data\DJI_0012.SRT")) { int lineCount = 0; int id = 0; while ((line = file.ReadLine()) != null) { int idTemp; if (int.TryParse(line, out idTemp)) { djiSrtVideoLogModel = new DjiSrtVideoLogModel(); id = idTemp; lineCount++; } if (id >= 1) { switch (lineCount) { case 1: djiSrtVideoLogModel.Id = id; lineCount++; break; case 2: djiSrtVideoLogModel = SrtConverter.GetTimeStamp(line, ref djiSrtVideoLogModel); lineCount++; break; case 3: djiSrtVideoLogModel = SrtConverter.GetHomePointAndDateTime(line, ref djiSrtVideoLogModel); lineCount++; break; case 4: djiSrtVideoLogModel = SrtConverter.GetGpsPointAndBarometer(line, ref djiSrtVideoLogModel); lineCount++; break; case 5: djiSrtVideoLogModel = SrtConverter.GetCameraInfo(line, ref djiSrtVideoLogModel); djiSrtVideoLogModels.Add(djiSrtVideoLogModel); lineCount = 0; break; default: break; } } counter++; } } var filePath = Path.Combine(@"C:\Temp\", "djiSrtVideo.csv"); var noko = SrtConverter.ConvertSrtToUavLog(djiSrtVideoLogModels); var csvVideoLogs = CsvUtilities.ToCsv(",", djiSrtVideoLogModels.ToArray()); var saved = CsvUtilities.SaveCsvTofile(filePath, csvVideoLogs); }