Tuesday, May 12, 2009

Converting a Lat Long to a USGS Quarter Quad Number in VB

USGS uses an odd system of numbering the 24k quads, and quarter quads.
it uses lat, lon(lower RIGHT corner), then letters A - H from right to left, and 1 -8 from bottom to top. Quarter quads are then numbered 1 is upper right, 2 is upper left, 3 is lower left, 4 is lower right of the whole quad. eg 33106-E32 where the 2 at the end is the qq number.

An irritating little problem, but here is a solution in VB.net


Given a lat-lon, what is the USGS quad number (or USGS quarter quad in this case)

Private Function LatLonToQQName(ByVal Latitude As Double, ByVal Longitude As Double) As String

'What is the Longitude whole number part (degree) of the right corner
Dim LonDegree As Integer = Math.Ceiling(Longitude)

'Get the fractional part of lon
Dim FLon = LonDegree - Longitude

'Calculate the Number, from 1 to 8, starting from right corner
Dim Number As String = Math.Ceiling(FLon / 0.125).ToString

'Next, the number, from 1 to 8, starting from bottom
Dim LatDegree As Integer = Math.Floor(Latitude)

'Get the fractional part of lon
Dim FLat = Latitude - LatDegree

Dim Letter = Chr(Asc("A") + Math.Floor(FLat / 0.125)).ToString

'Finally, the QQ number (from 1 to 4) 1 is upper right, 2 is upper left, 3 is lower left, 4 is lower right
Dim QQNumberLon As Integer = Math.Floor((FLon / 0.125 - Math.Floor(FLon / 0.125)) * 2)
Dim QQNumberLat As Integer = Math.Floor((FLat / 0.125 - Math.Floor(FLat / 0.125)) * 2)

Dim QQNumber As String = ""
If QQNumberLat = 0 And QQNumberLon = 0 Then
QQNumber = "4"
ElseIf QQNumberLat = 0 And QQNumberLon = 1 Then
QQNumber = "3"
ElseIf QQNumberLat = 1 And QQNumberLon = 1 Then
QQNumber = "2"
ElseIf QQNumberLat = 1 And QQNumberLon = 0 Then
QQNumber = "1"
End If

Return LatDegree.ToString & (Math.Abs(LonDegree)).ToString("D3") & "-" & Letter & Number & QQNumber
End Function

No comments: