Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Processing records using Haversine Formula in c#

Posted on 2009-07-09
3
707 Views
Last Modified: 2013-12-17
I am new to c# and need to write a program that will process GPS postion data from a sql server database and then calculate the total distance traveled for each unit listed by date.  Each record contains a transaction id,  date/time stamp, unit id, latitude and longitude. I have bound this table to a dataset.  I also  have a class for the haversine formula which will accepts the fields LAT (A) LON (A), LAT(B), LON (B).  Where I am stuck is how to process the data from the data set (Keeping in mind that the data needs to be read one record at a time) and then loop through the records, apply the haversine formula then subtotal by unit and date.  The final step would be to insert a record into a new table that contains the Equipment ID, Date, and total miles .   Attached is the class I plan to use and sample data.    Any suggestions would be greatly appreciated.  
Haversine---Copy.txt
Data.txt
0
Comment
Question by:maximjon
3 Comments
 
LVL 15

Accepted Solution

by:
spprivate earned 500 total points
ID: 24818604
0
 

Author Comment

by:maximjon
ID: 25026098
This was a lot easier than I initially though.  After I setup a SQLconnection, I wrote my code to loop through the SQLdataReader one record at a time.  I simply just setup variables to retain all important info from each record (see code below)
Dim dr As SqlDataReader = myCommand.ExecuteReader()
 
            lngRecordCount = 1
            While dr.Read()
 
                If lngRecordCount > 1 Then
                    strEquipID = dr(0)
                    strInsertDate = DateValue(dr(1))
                    dblLat2 = dr(2)
                    dblLon2 = dr(3)
 
                    ' compare the Equipment ID and InsertDate of the two records to see if they changed.  Reset All variables and start over if they did.
                    If strPriorEquipID <> strEquipID Or strPriorInsertDate <> strInsertDate Then
                        'Write the prior equipID......
 
 
                        ' This IF statement is a temporary work around for faulty data.  Lat & Lon need to have a decimal else the haversine brings back bad data
                        If totalDistance >= 0 Then
                            myCommandWrite = New SqlCommand("INSERT INTO tblSalOutput VALUES ('" & strPriorEquipID & "', '" & strPriorInsertDate & "', " & totalDistance & ",'" & strBranch & "')", myConnectionWrite)
                            ra = myCommandWrite.ExecuteNonQuery()
                            '   MessageBox.Show("New Row Inserted" & ra)
                        End If
 
 
                        lngRecordCount = 1
                        totalDistance = 0
                    Else
                        ' Now that you have lat1-lat2 & lon1-lon2, apply your formula here and write back to a serpare table
 
 
                        tmpDistance = distance(dblLat1, dblLon1, dblLat2, dblLon2)
 
                        totalDistance = totalDistance + tmpDistance
 
                    End If
 
                End If
 
                strPriorEquipID = dr(0)
                strPriorInsertDate = DateValue(dr(1))
                dblLat1 = dr(2)
                dblLon1 = dr(3)
                strBranch = dr(4)
 
                lngRecordCount = lngRecordCount + 1
            End While
 
 
    Public Function distance(ByVal lat1 As Double, ByVal lon1 As Double, _
                         ByVal lat2 As Double, ByVal lon2 As Double, _
                         Optional ByVal unit As Char = "M"c) As Double
        Dim theta As Double = lon1 - lon2
        Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + _
                                Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * _
                                Math.Cos(deg2rad(theta))
        dist = Math.Acos(dist)
        dist = rad2deg(dist)
        dist = dist * 60 * 1.1515
        If unit = "K" Then
            dist = dist * 1.609344
        ElseIf unit = "N" Then
            dist = dist * 0.8684
        End If
        Return dist
    End Function
    Public Function Haversine(ByVal lat1 As Double, ByVal lon1 As Double, _
                             ByVal lat2 As Double, ByVal lon2 As Double, _
                             Optional ByVal unit As Char = "M"c) As Double
        Dim R As Double = 6371 'earth radius in km
        Dim dLat As Double
        Dim dLon As Double
        Dim a As Double
        Dim c As Double
        Dim d As Double
        dLat = deg2rad(lat2 - lat1)
        dLon = deg2rad((lon2 - lon1))
        a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(deg2rad(lat1)) * _
                Math.Cos(deg2rad(lat2)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
        c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a))
        d = R * c
        Select Case unit.ToString.ToUpper
            Case "M"c
                d = d * 0.62137119
            Case "N"c
                d = d * 0.5399568
        End Select
        Return d
    End Function
    Private Function deg2rad(ByVal deg As Double) As Double
        Return (deg * Math.PI / 180.0)
    End Function
    Private Function rad2deg(ByVal rad As Double) As Double
        Return rad / Math.PI * 180.0
    End Function

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql server query 12 25
Shrink multiple databases at once 4 24
Visual Studio npm 1 11
MS SQL SERVER and ADODB.commands 8 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question