473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Calculating distance

375 256MB
Hello
I am having a table where the speed of the vehicle at the respective time are stored. Now I should calculate distance
the eg details of vehicle No(VNo), speed,datetime is given below
vno speed datetime
2117 0 9/12/07 1:00
2117 0 9/12/07 2:00
2117 0 9/12/07 3:00
2117 1 9/12/07 4:00
2117 2 9/12/07 5:00
2117 3 9/12/07 6:00
2117 4 9/12/07 7:00
2117 5 9/12/07 8:00
2117 6 9/12/07 9:00
2117 0 9/12/07 10:00
2117 0 9/12/07 11:00
2117 0 9/12/07 12:00
2117 0 9/12/07 13:00
2117 9 9/12/07 14:00
2117 6 9/12/07 15:00
2117 5 9/12/07 16:00

To calculate the distance you should take all the non zero speed values and if the value is
2117 9 9/12/07 14:00
2117 6 9/12/07 15:00
2117 5 9/12/07 16:00

then the start time is 9/12/07 14:00
and the endtime is 9/12/07 16:00
now calculate the time span take the sum of speed and calculate the distance



Now I have calculated the distance as below
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         MyCon.Open();
  4.         SqlDataAdapter MyDa1 = new SqlDataAdapter("select speed,gps_datetime from gpsdata_history where registrationno='" + DropDownList1.SelectedValue + "'and (gps_datetime >='" + TextBox1.Text + "' and gps_datetime<='" + TextBox2.Text + "' )order by gps_datetime", MyCon);
  5.         DataTable MyDt1 = new DataTable();
  6.         MyDa1.Fill(MyDt1);
  7.         MyCon.Close();
  8.         int Speed = 0;
  9.         int sumSpeed = 0;
  10.         TimeSpan timediff = TimeSpan.Zero;
  11.         int i = 1;
  12.         DateTime Startdate;
  13.         DateTime Enddate;
  14.         DateTime chkend;
  15.         chkend = DateTime.Now;
  16.         double Totaltime = 0;
  17.         int count = MyDt1.Rows.Count;
  18.  
  19.         for (i = 0; i < count; i++)
  20.         {
  21.             Speed = Convert.ToInt32(MyDt1.Rows[i][0].ToString());
  22.  
  23.             while (Speed == 0 && i < (count-1))
  24.             {
  25.                 Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
  26.                 i++;
  27.             }
  28.             Startdate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
  29.             while (Speed > 0 && i < count)
  30.             {
  31.                 Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
  32.                 if (Speed != 0)
  33.                 {
  34.                     sumSpeed = sumSpeed + Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString()); ;
  35.                     i++;
  36.                 }
  37.             }
  38.             i--;
  39.             Enddate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
  40.             timediff = timediff + (Enddate - Startdate);
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.         Totaltime = timediff.TotalHours;
  47.         double distance = Totaltime * sumSpeed;
  48.         TextBox3.Text = Totaltime.ToString();
  49.         TextBox4.Text = distance.ToString();
  50.         TextBox5.Text = sumSpeed.ToString();
  51.     }
But this is not giving me the exact distance
regards
cmrhema
Sep 14 '07 #1
4 1474
Shashi Sadasivan
1,435 Expert 1GB
Quite a bit of code happening there.
A few modification before you test it.
In your sql statement could you put a where speed > 0
and also put in order by time
this way your first record is the start time, and last record is the end time.

Then iterate throug the foreach to calc total speed, etc
cheers

-------
ok,,,,,guess i found the problemo
Apparantly you are increasin i within the for loop in the first while, so at the end of it, you have exhausted quite a bit of rows.
Then of what is left you try to find the time from that. (you might have passed the last time before.)
Try changin your approach similar to what I sent above.
cheers
Sep 14 '07 #2
cmrhema
375 256MB
Quite a bit of code happening there.
A few modification before you test it.
In your sql statement could you put a where speed > 0
and also put in order by time
this way your first record is the start time, and last record is the end time.

Then iterate throug the foreach to calc total speed, etc
cheers

-------
ok,,,,,guess i found the problemo
Apparantly you are increasin i within the for loop in the first while, so at the end of it, you have exhausted quite a bit of rows.
Then of what is left you try to find the time from that. (you might have passed the last time before.)
Try changin your approach similar to what I sent above.
cheers
thank you shashi for the reply

I have used order by gps_datetime
If I do not increment i in the while loop how will I go to the second loop
But still there is a problem
Of course i did modify or rather included the speed and calculated the average speed.
There is still the problem in calculating total time and speed.
My latest code looks as below
Expand|Select|Wrap|Line Numbers
  1. MyCon.Open();
  2.         SqlDataAdapter MyDa1 = new SqlDataAdapter("select speed,gps_datetime from gpsdata_history where registrationno='" + DropDownList1.SelectedValue + "'and (gps_datetime >='" + TextBox1.Text + "' and gps_datetime<='" + TextBox2.Text + "' )order by gps_datetime", MyCon);
  3.         DataTable MyDt1 = new DataTable();
  4.         MyDa1.Fill(MyDt1);
  5.         MyCon.Close();
  6.         int Speed = 0;
  7.         int sumSpeed = 0;
  8.         TimeSpan timediff = TimeSpan.Zero;
  9.         int i = 1;
  10.         DateTime Startdate;
  11.         DateTime Enddate;
  12.         DateTime chkend;
  13.         chkend = DateTime.Now;
  14.         double Totaltime = 0;
  15.         int count = MyDt1.Rows.Count;
  16.         int avg=0;
  17.  
  18.         for (i = 0; i < count; i++)
  19.         {
  20.             Speed = Convert.ToInt32(MyDt1.Rows[i][0].ToString());
  21.  
  22.             while (Speed == 0 && i < (count-1))
  23.             {
  24.                 Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
  25.                 i++;
  26.             }
  27.             Startdate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
  28.             while (Speed > 0 && i < count)
  29.             {
  30.               avg++;
  31.                 Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
  32.                 if (Speed != 0)
  33.                 {
  34.                     sumSpeed = sumSpeed + Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString()); ;
  35.                     i++;
  36.                 }
  37.             }
  38.             i--;
  39.             Enddate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
  40.             timediff = timediff + (Enddate - Startdate);
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.         Totaltime = timediff.TotalHours;
  47.         double distance = Totaltime * sumSpeed/avg;
  48.         TextBox3.Text = Totaltime.ToString();
  49.         TextBox4.Text = distance.ToString();
  50.         TextBox5.Text = sumSpeed.ToString();
  51.     }
  52. }
Sep 14 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
I got your code to what I was expecting it to be
Please note the change in the sql statement.

Expand|Select|Wrap|Line Numbers
  1. MyCon.Open();
  2.         SqlDataAdapter MyDa1 = new SqlDataAdapter("select speed,gps_datetime from gpsdata_history where speed > 0 registrationno='" + DropDownList1.SelectedValue + "'and (gps_datetime >='" + TextBox1.Text + "' and gps_datetime<='" + TextBox2.Text + "' )order by gps_datetime", MyCon);
  3.         DataTable MyDt1 = new DataTable();
  4.         MyDa1.Fill(MyDt1);
  5.         MyCon.Close();
  6.         int Speed = 0;
  7.         int sumSpeed = 0;
  8.         TimeSpan timediff = TimeSpan.Zero;
  9.         int i = 1;
  10.         DateTime Startdate;
  11.         DateTime Enddate;
  12.         DateTime chkend;
  13.         chkend = DateTime.Now;
  14.         double Totaltime = 0;
  15.         int count = MyDt1.Rows.Count;
  16.         int avg=0;
  17.  
  18.         Startdate = Convert.ToDateTime(MyDt1.Rows[0]["gps_datetime"].ToString());
  19.         Enddate = Convert.ToDateTime(MyDt1.Rows[MyDt1.Rows.Count-1]["gps_datetime"].ToString());
  20.         timediff = (Enddate - Startdate);
  21.  
  22.         for (i = 0; i < count; i++)
  23.         {
  24.             avg++;
  25.             sumSpeed += Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
  26.         }
  27.  
  28.  
  29.         Totaltime = timediff.TotalHours;
  30.         double distance = Totaltime * sumSpeed/avg;
  31.         TextBox3.Text = Totaltime.ToString();
  32.         TextBox4.Text = distance.ToString();
  33.         TextBox5.Text = sumSpeed.ToString();
Hope this helps
Cheers
Sep 14 '07 #4
cmrhema
375 256MB
I got your code to what I was expecting it to be
Please note the change in the sql statement.

Expand|Select|Wrap|Line Numbers
  1. MyCon.Open();
  2.         SqlDataAdapter MyDa1 = new SqlDataAdapter("select speed,gps_datetime from gpsdata_history where speed > 0 registrationno='" + DropDownList1.SelectedValue + "'and (gps_datetime >='" + TextBox1.Text + "' and gps_datetime<='" + TextBox2.Text + "' )order by gps_datetime", MyCon);
  3.         DataTable MyDt1 = new DataTable();
  4.         MyDa1.Fill(MyDt1);
  5.         MyCon.Close();
  6.         int Speed = 0;
  7.         int sumSpeed = 0;
  8.         TimeSpan timediff = TimeSpan.Zero;
  9.         int i = 1;
  10.         DateTime Startdate;
  11.         DateTime Enddate;
  12.         DateTime chkend;
  13.         chkend = DateTime.Now;
  14.         double Totaltime = 0;
  15.         int count = MyDt1.Rows.Count;
  16.         int avg=0;
  17.  
  18.         Startdate = Convert.ToDateTime(MyDt1.Rows[0]["gps_datetime"].ToString());
  19.         Enddate = Convert.ToDateTime(MyDt1.Rows[MyDt1.Rows.Count-1]["gps_datetime"].ToString());
  20.         timediff = (Enddate - Startdate);
  21.  
  22.         for (i = 0; i < count; i++)
  23.         {
  24.             avg++;
  25.             sumSpeed += Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
  26.         }
  27.  
  28.  
  29.         Totaltime = timediff.TotalHours;
  30.         double distance = Totaltime * sumSpeed/avg;
  31.         TextBox3.Text = Totaltime.ToString();
  32.         TextBox4.Text = distance.ToString();
  33.         TextBox5.Text = sumSpeed.ToString();
Hope this helps
Cheers
Thank you shashi for your help
I have solved it as below

protected void Button1_Click(object sender, EventArgs e)
{
MyCon.Open();
SqlDataAdapter MyDa1 = new SqlDataAdapter("select speed,gps_datetime from gpsdata_history where registrationno='" + DropDownList1.SelectedValue + "'and (gps_datetime >='" + TextBox1.Text + "' and gps_datetime<='" + TextBox2.Text + "' )order by gps_datetime", MyCon);
DataTable MyDt1 = new DataTable();
MyDa1.Fill(MyDt1);
MyCon.Close();
int Speed = 0;
int sumSpeed = 0;
TimeSpan timediff = TimeSpan.Zero;
int i = 1;
DateTime Startdate;
DateTime Enddate;
DateTime chkend;
chkend = DateTime.Now;
double Totaltime = 0;
int count = MyDt1.Rows.Count;
int avg=0;
int flag = 0;

for (i = 0; i < count; i++)
{
Speed = Convert.ToInt32(MyDt1.Rows[i][0].ToString());

while (Speed == 0 && i < (count-1))
{
Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
if (Speed == 0)
{
Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
i++;
}
}
Startdate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
while (Speed > 0 && i < count)
{

Speed = Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString());
if (Speed != 0)
{
avg++;
sumSpeed = sumSpeed + Convert.ToInt32(MyDt1.Rows[i]["speed"].ToString()); ;
i++;
}
flag = 1;
}
if (flag > 0)
{
flag = 0;
i--;
//Enddate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
//timediff = timediff + (Enddate - Startdate);

}

Enddate = Convert.ToDateTime(MyDt1.Rows[i]["gps_datetime"].ToString());
timediff = timediff + (Enddate - Startdate);

}



Totaltime = timediff.TotalHours;
double distance = Totaltime * sumSpeed/avg;
TextBox3.Text = Totaltime.ToString();
TextBox4.Text = distance.ToString();
TextBox5.Text = sumSpeed.ToString();
}
Sep 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
13
by: racygirl | last post by:
Hi all, I was just wondering, is it possible to write a solution to the following problem with the following criteria: 1. it must be as efficient as possible at run-time 2. be in O(1) 3. and...
10
by: Alan Johnson | last post by:
24.1.1.3 says about InputIterators: Algorithms on input iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms. In 24.3.4.4 summarizes the...
3
by: Paul Aspinall | last post by:
Hi Can anyone recommend a suitable way of calculating distances between UK post codes?? Many websites do this, and I'm wondering if there is some standard code, and where the reference DB comes...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
12
KoreyAusTex
by: KoreyAusTex | last post by:
I am having problems with my code trying to use the monte carlo method. This is what I have so far but think there might be a error in my calculations: I keep getting the same thing for my...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
1
by: tiffrobe | last post by:
I'm a little lost on my program. Everything works fine except function 3. It gives out garbage numbers. Its suppose to give the distance between two points and then the area of 2 circles. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.