Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Latitude and longitude

0.00/5 (No votes)
1 Sep 2012CPOL 12.4K  
This is an alternative for Latitude and longitude

Introduction

I wrote some code for a dead project in VB.NET to find the nearest TV transmitters from a location. It transform a flat file with fixed fields that contains informations for west European transmitters in a SQLite base.

I use http://ipinfodb.com/  and http://www.gisgraphy.com/ to locate a place to find around.

Using the code  

The code i used to calculate distance between two place is similar to the code form Grasshopper.iic

VB
Private Function Distance(SourceLat As Double, SourceLong As Double, DestLat As Double, DestLong As Double) As Double
    Const R As Double = 6378.0 'rayon de la terre en km
    Dim retour As Double = R * (Math.PI / 2 - Math.Asin(Math.Sin(DegreeToRadian(DestLat)) * _
        Math.Sin(DegreeToRadian(SourceLat)) + Math.Cos(DegreeToRadian(DestLong - SourceLong)) * _
        Math.Cos(DegreeToRadian(DestLat)) * Math.Cos(DegreeToRadian(SourceLat))))
    Return retour
End Function     Private Function DegreeToRadian(degre As Double) As Double
    Return Math.PI * degre / 180
End Function

 But I use this code  only to find the transmitters in a circular area around a location in a square area.

SQLite doesn't have math function so i need to calculate lattude and longitude min/max around the place. 

(txbLatitude and txblongitude are TextBoxes and contain the longitude and latitude).

VB.NET
Dim latitudemax As String 
Dim latitudemin As String 
Dim longitudemax As String
Dim longitudemin As String
Private Sub CalculerCoordonneesMinMax (km as Double)
        Const kmLat As Double = 111.37 'nombre de km pour 1° de latitude
        Dim ecartlatitude As Double = km / kmLat
        Dim ecartlongitude As Double = km / (Math.Cos(DegreeToRadian(CDouble(txbLatitude.Text))) * kmLat)
        latitudemax = (CDouble(txbLatitude.Text) + ecartlatitude).ToString(Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        latitudemin = (CDouble(txbLatitude.Text) - ecartlatitude).ToString(Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        longitudemax = (CDouble(txbLongitude.Text) + ecartlongitude).ToString(Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        longitudemin = (CDouble(txbLongitude.Text) - ecartlongitude).ToString(Globalization.CultureInfo.CreateSpecificCulture("en-US"))
End Sub   

and the query for SQLite looks like that

VB.NET
Dim sql As String = "select * from frequences where " & _
"(latitude between " & latitudemin & " and " & latitudemax & " ) and " & _
"(longitude between " & longitudemin & " and " & longitudemax & ") order by NomStation desc"

 The result is DataTable that contains transmitters in square area.

 

The original code contains also a converter string to double for "." or "," separator, because I have several sources with different separators.   

VB
Private Function CDouble(Expression As String) As Double
    'remplacement de Cdbl qui NE prend PAS en compte les paramètres locaux pour le séparateur décimal
    Dim re As New Regex("(\.|,)")
    Dim str() As String = re.Split(Expression.Trim)
    If str.Length = 3 Then
        Try
            Dim retour As Double = Double.Parse(str(0)) + _
                   (Double.Parse(str(2)) / (10.0 ^ str(2).Length))
            Return retour
        Catch ex As Exception
            Throw New Exception("Erreur de convertion")
        End Try
    ElseIf str.Length = 1 Then
        Try
            Dim retour As Double = Doubl.e.Parse(str(0))
            Return retour
        Catch ex As Exception
            Throw New Exception("Erreur de convertion")
        End Try
    Else
        Throw New Exception("Erreur de convertion")
    End If
End Function 

Points of Interest 

This code has formulas to transform a distance in kilometres to latitude and longitude min/max and a light parser to convert a string to double.

 

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)