-1

My example dataset from csv file are as follows (timestamp;longitude;latitude):

2008-02-02 15:10:26.000;116.76038000;39.79758000
2008-02-02 15:10:49.000;116.76660000;39.80270000
2008-02-02 15:10:58.000;116.76660000;39.80270000
2008-02-02 15:11:10.000;116.76660000;39.80270000

Basically I want to put these locations to map. However, I want each point appears one by one based on the timestamp. For example, after data at the first row is presented, data at the second row will follow after 13 seconds because of the timestamp differences. After that, the third row will be out 9 seconds after the second data. In this case, the time interval is not the same.

Is it possible to do that? How to do that? I have tried something like this:

while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            if (!String.IsNullOrWhiteSpace(line))
            {
                if (j == 0) // to avoid reading csv header
                {
                    j = j + 1; 
                }
                else
                {                        
                    values = line.Split(';');
                    if (!double.TryParse(values[1], out longitude))
                    {
                        longitude = 0;
                    }
                    if (!double.TryParse(values[2], out latitude))
                    {
                        latitude = 0;
                    }
                    Location bre = new Location(latitude, longitude);                                                
                    if (i == 0)
                    {
                        prev = null;
                        current = bre;
                        dtcurrent = makeDT(values[0]);

                        i = i + 1;
                    }
                    else
                    {
                        dtprev = dtcurrent;                                                                                                                
                        dtcurrent = makeDT(values[0]);                            
                        prev = current;                            
                        current = bre;
                        span = dtcurrent.Subtract(dtprev);
                        int th = span.Seconds * 1000;
                        createPushpin(bre, values[0], "default");
                        Thread.Sleep(th);                            
                        i = i + 1;                            
                    }
                }
            }               
        }

However, the pins on the map appear after loop with all thread end.

  • Yes, it is possible to do that. – Llama Jan 17 '19 at 04:20
  • great to know this. anything in code? – Gauravsa Jan 17 '19 at 04:20
  • do you have any clue how to do that? – Servasius Rinta Jan 17 '19 at 04:27
  • Yes, I do. And no, SO isn't a code-writing service. – Llama Jan 17 '19 at 04:28
  • 1
    Possible duplicate of [Reading CSV file and storing values into an array](https://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array) – Gauravsa Jan 17 '19 at 05:33
  • It is not the same – Servasius Rinta Jan 17 '19 at 09:03
  • It's unclear. I'm not sure it's clear for you how to handle this first column. You don't know what data type and how to simply handle the delay you have every time you want to display it. – Drag and Drop Jan 17 '19 at 13:14
  • If it's number of second or a timeSpan you have to had before display it should be typed as such. – Drag and Drop Jan 17 '19 at 13:15
  • So your issue is not related to reading a CSV. It has Nothing to do with the CSV. You are starting from an array with DateTime and need to make it an array of TimeSpan, based on the difference between a element and the first element of the array. – Drag and Drop Jan 17 '19 at 13:18
  • Second Dont do every thing in the same loop. In fact take a paper and write down the step of your process. you will write something like "Read. Put in array. Use it for display" not "Read while array and display then sleep. then continue reading." So those action must be separated. – Drag and Drop Jan 17 '19 at 13:21
  • You should take a look at ReadAllText and ReadAllLine. It will save you time and clarify the process. – Drag and Drop Jan 17 '19 at 13:45

4 Answers4

2

Read and Store the information:

In this part I will not advocate for any 3rd part library that will simply the reading and the writing of a CSV. Like CSV Helper. Because it's obvious that you should use it.

var fileName = "input.txt";
var lines = File.ReadAllLines(fileName);//.Skip(1) // if you have an header.
var rawData = lines.Select(line => line.Split(';'))
                    .Select(item =>
                    {
                         Decimal lon, lat;
                         return new
                         {
                             date = DateTime.Parse(item[0]),
                             Lon = Decimal.TryParse(item[1], out lon) ? lon : 0,
                             Lat = Decimal.TryParse(item[2], out lat) ? lat : 0,
                         };
                     });

Decimal.TryParse(item[1], out lon) ? lon : 0 is a short way to TryParse the input and give it a default value if it fails.

At this point you have a List of custom object that is the exact data you had in the file. The reading is done you can start to work on it. To know how mutch delay each point should have we need to substract it with the first datetime.

var referenceDate = rawData.Select(x => x.date).First();
var usefullData = rawData.Select(x =>
                         new
                         {
                             Delay = (x.date - referenceDate),
                             x.Lon,
                             x.Lat,
                         });

Now we have usefull data, the delay is a TimeSpan you can express it's value in the unit you want. doc

As we have no information on the map how you will make the point appears I will assume that : When the user display the map you have to calculate the time each point should appear.

var readingTime = DateTime.Today.AddHours(19);
var displayData = usefullData.Select(x =>
                                new
                                {
                                    Date = readingTime + x.Delay,
                                    x.Lon,
                                    x.Lat,
                                });
Drag and Drop
  • 2,489
  • 3
  • 20
  • 32
0

You can read .csv file using stream reader and separate the columns using split(";") function, determining the semicolon is the separator of each row. After that you can store data into arrayList or you can store into a database table according to column.

List<string> lattitude=new List<string>();
List<string> longitude=new List<string>();
List<string> timestamp=new List<string();
using (var rd = new StreamReader("yourfile.csv"))
{
    while (!rd.EndOfStream)
    {
        var arrlst= rd.ReadLine().Split(';');
        timestamp.add(arrlst[0])
        lattitude.Add(arrlst[1]);
        longitude.Add(arrlst[2]);
    }
}

now you can use timepicker to count the seconds using a counter for tracking every 13 seconds.[you can get the interval of seconds by getting the difference of the contiguous times from the timestamp] whenever counter counts to 13 then start the counter from 0 again. You will do the above steps until all the data in the desired list gets read. when all the data gets read then you can stop the timepicker by inducing some logics. This is a way to get the data from the desired list one by one according to timestamp value and according to your needs.

Thread: create multiple threads with new Thread(). run the threads based on your intervals.

Hope this helps. Thanks

Rashedul.Rubel
  • 2,505
  • 19
  • 31
  • time interval is not the same for every loop :( – Servasius Rinta Jan 17 '19 at 09:04
  • yeah, in this case you have to manage time interval with some logics when you opted to use time picker. Or you can sort the datasets based on timestamp and then display by calculating the seconds according to your needs. I have given a hint to help you that you may think :). good luck – Rashedul.Rubel Jan 17 '19 at 12:48
  • yes, Thread can also be another way to display your data according to your needed time in seconds. – Rashedul.Rubel Jan 17 '19 at 12:51
0

Yes you can do that.

Basically you have data in CSV, you can write an API to read data from csv and return json object. You can do that by writing asp.net api.

Here is how to read CSV file in asp.net http://oniwebblog.blogspot.com/2016/01/aspnet-mvc-web-application-how-to-read_31.html

Here is reference to build .net web api Write web API

On client side you can use jquery to call API and get json data.

Now for plotting location on may you can use google API to load map and draw points.

Here is how you can draw pin on google map

In javascript you can use setTimeout function to call specific function at specific interval.

Krunal Parmar
  • 411
  • 2
  • 13
0

There are several steps you need to take, however i am only going to focus on the first one. Converting the results of file to some structure you can use..

The rest is up to you (and only for morbid academic purposes), you need to choose your own adventure

Given

public readonly struct Location
{
   public DateTime TimeStamp { get; }
   public Decimal Lat { get; }
   public Decimal Lon { get; }

   public Location(DateTime timeStamp, decimal lat, decimal lon)
   {
      TimeStamp = timeStamp;
      Lat = lat;
      Lon = lon;
   }
}

Method to convert

public static Location ConvertToLocation(string input)
{
   var split = input.Split(';');
   return new Location(DateTime.Parse(split[0]),decimal.Parse(split[1]),decimal.Parse(split[2]));
}

Usage

// List of locations
var results = File.ReadLines("SomeFileName").Select(ConvertToLocation);
TheGeneral
  • 69,477
  • 8
  • 65
  • 107