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
Private Function Distance(SourceLat As Double, SourceLong As Double, DestLat As Double, DestLong As Double) As Double
Const R As Double = 6378.0
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 TextBox
es and contain the longitude and latitude).
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
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
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.
Private Function CDouble(Expression As String) As Double
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.