LATITUDE LONGITUDE and UTM projections CONVERTING

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
vladimir777
Posts: 94
Joined: Aug 19, 2011 18:28

LATITUDE LONGITUDE and UTM projections CONVERTING

Post by vladimir777 »

I converted it from EXCEL that I found on The Net. It is tested (in VB6), but dirty code

CONVERT
UTM to LATLON -> UTM
and
LATLON to UTM -> KALK

Code: Select all

Private Function UTM(LAT As Double, LON As Double) As String
' LATLON to UTM
'convert latitude, longitude into UTM projection
'return is  UTM = zone & "|" & north & "|" & east
'http://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system

Dim a As Double
Dim b As Double
Dim ff As Double
Dim finv As Double
Dim rm As Double
Dim k0 As Double
Dim EE As Double
Dim e1sq As Double
Dim NN As Double

a = 6378137
b = 6356752.314
ff = 0.003352811
finv = 298.2572236
rm = 6367435.68
k0 = 0.9996
EE = 0.081819191
e1sq = 0.006739497
NN = 0.00167922

Dim a0 As Double
Dim b0 As Double
Dim c0 As Double
Dim d0 As Double
Dim e0 As Double

a0 = a * (1 - NN + (5 * NN * NN / 4) * (1 - NN) + (81 * NN ^ 4 / 64) * (1 - NN))
b0 = (3 * a * NN / 2) * (1 - NN - (7 * NN * NN / 8) * (1 - NN) + 55 * NN ^ 4 / 64)
c0 = (15 * a * NN * NN / 16) * (1 - NN + (3 * NN * NN / 4) * (1 - NN))
d0 = (35 * a * NN ^ 3 / 48) * (1 - NN + 11 * NN * NN / 16)
e0 = (315 * a * NN ^ 4 / 51) * (1 - NN)


Dim n As Double
Dim o As Double
Dim p As Double
Dim q As Double
Dim r As Double



n = 31 + Int(LON / 6)
o = 6 * n - 183
p = (LON - o) * 3.14159265 / 180
q = LAT * 3.14159265 / 180

r = LON * 3.14159265 / 180


Dim s As Double
Dim t As Double
Dim v As Double
s = a * (1 - EE * EE) / ((1 - (EE * Sin(q)) ^ 2) ^ (3 / 2))
t = a / ((1 - (EE * Sin(q)) ^ 2) ^ (1 / 2))
v = a0 * q - b0 * Sin(2 * q) + c0 * Sin(4 * q) - d0 * Sin(6 * q) + e0 * Sin(8 * q)


Dim x As Double
Dim y As Double
Dim z As Double
Dim AA As Double
Dim AB As Double
Dim AC As Double
Dim AD As Double
x = v * k0

y = t * Sin(q) * Cos(q) / 2
z = ((t * Sin(q) * Cos(q) ^ 3) / 24) * (5 - Tan(q) ^ 2 + 9 * e1sq * Cos(q) ^ 2 + 4 * e1sq ^ 2 * Cos(q) ^ 4) * k0
AA = t * Cos(q) * k0
AB = (Cos(q)) ^ 3 * (t / 6) * (1 - Tan(q) ^ 2 + e1sq * Cos(q) ^ 2) * k0

AC = ((p) ^ 6 * t * Sin(q) * Cos(q) ^ 5 / 720) * (61 - 58 * Tan(q) ^ 2 + Tan(q) ^ 4 + 270 * e1sq * Cos(q) ^ 2 - 330 * e1sq * Sin(q) ^ 2) * k0
AD = (x + y * p * p + z * p ^ 4)

Dim east As Double
Dim north As Double
Dim zone As Integer

If AD < 0 Then
    north = 10000000 + AD
Else
    north = AD
End If

east = 500000 + (AA * p + AB * p ^ 3)
zone = n

UTM = zone & "|" & north & "|" & east


End Function


Private Function KALK(ne As String, zone As Integer, NN As Double, EE As Double) As String
' UTM to LATLON
' ne is "N" or "S"
' zone
' NN - NORTHING
' EE - EASTING
' return     KALK = LAT & "|" & LON
Dim k0 As Double
Dim b As Double
Dim e As Double
Dim a As Double
Dim e1sq As Double

k0 = 0.9996
b = 6356752.314
a = 6378137
e = 0.081819191
e1sq = 0.006739497


Dim E1 As Double
Dim C1 As Double
Dim C2 As Double
Dim C3 As Double
Dim C4 As Double

E1 = (1 - (1 - e * e) ^ (1 / 2)) / (1 + (1 - e * e) ^ (1 / 2))
C1 = 3 * E1 / 2 - 27 * E1 ^ 3 / 32
C2 = 21 * E1 ^ 2 / 16 - 55 * E1 ^ 4 / 32
C3 = 151 * E1 ^ 3 / 96
C4 = 1097 * E1 ^ 4 / 512



Dim H As Double
Dim i As Double
Dim j As Double
Dim K As Double

If ne = "N" Then
    H = NN
Else
    H = 10000000 - NN
End If

i = 500000 - EE
j = NN / k0


K = j / (a * (1 - e ^ 2 / 4 - 3 * e ^ 4 / 64 - 5 * e ^ 6 / 256))

Dim L As Double
Dim M As Double
Dim n As Double
Dim o As Double
Dim p As Double

Dim q As Double
Dim r As Double
Dim s As Double
Dim t As Double
Dim U As Double
Dim v As Double
Dim W As Double

Dim x As Double
Dim y As Double
Dim z As Double

Dim AA As Double
Dim AB As Double
Dim AC As Double




L = K + C1 * Sin(2 * K) + C2 * Sin(4 * K) + C3 * Sin(6 * K) + C4 * Sin(8 * K)
M = e1sq * Cos(L) ^ 2
n = Tan(L) ^ 2
o = a / (1 - (e * Sin(L)) ^ 2) ^ (1 / 2)
p = a * (1 - e * e) / (1 - (e * Sin(L)) ^ 2) ^ (3 / 2)

q = i / (o * k0)
r = o * Tan(L) / p
s = q * q / 2
t = (5 + 3 * n + 10 * M - 4 * M * M - 9 * e1sq) * q ^ 4 / 24
U = (61 + 90 * n + 298 * M + 45 * n * n - 252 * e1sq - 3 * M * M) * q ^ 6 / 720

v = q
W = (1 + 2 * n + M) * q ^ 3 / 6

x = (5 - 2 * M + 28 * n - 3 * M ^ 2 + 8 * e1sq + 24 * n ^ 2) * q ^ 5 / 120

y = (v - W + x) / Cos(L)
z = 6 * zone - 183

AA = 180 * (L - r * (s + t + U)) / 3.14159265
AB = 0
If ne = "N" Then
    AB = AA
Else
    AB = -AA
End If
AC = z - y * 180 / 3.14159265
KALK = AB & "|" & AC
End Function

Post Reply