Felix18807
Programmer
I'm trying to convert from Ordnance Survey Grid Ref to Longtitude/Latitude using VBA.
The forumla for this is freely available (
I have posted my code below. It seems to be running ok.. Except it is miles out to the east/north! Has anyone ever attempted anything like this before or is there anyone who can spot the error. Many Thanks
The forumla for this is freely available (
I have posted my code below. It seems to be running ok.. Except it is miles out to the east/north! Has anyone ever attempted anything like this before or is there anyone who can spot the error. Many Thanks
Code:
Option Compare Database
Option Explicit
Const a As Double = 6377563.396
Const b As Double = 6356256.91
Const e2 As Double = (a - b) / a
Const N0 As Double = -100000
Const E0 As Double = 400000
Const f0 As Double = 0.999601272
Const phi0 As Double = 0.855211333
Const lambda0 As Double = -0.034906585
Const n As Double = (a - b) / (a + b)
Public Function ConvertOSToLatLon(E__1 As Double, n__2 As Double, lonorlat As Variant) As Variant
Dim phi As Double
Dim M As Double
Dim v As Double
Dim p As Double
Dim vii As Double
Dim viii As Double
Dim ix As Double
Dim x As Double
Dim xi As Double
Dim xii As Double
Dim xiia As Double
Dim n2 As Double
Dim e__3 As Double
Dim lon As Double
Dim lat As Double
phi = (n__2 - N0) / (a * f0) + phi0
M = b * f0 * ((1 + n + (5 \ 4) * n * n + (5 \ 4) * n * n * n) * (phi - phi0) - (3 * n + 3 * n * n + (21 \ 8) * n * n * n) * Sin(phi - phi0) * Cos(phi + phi0) + ((15 \ 8) * n * n + (15 \ 8) * n * n * n) * Sin(2 * (phi - phi0)) * Cos(2 * (phi + phi0)) - (35 \ 24) * n * n * n * Sin(3 * (phi - phi0)) * Cos(3 * (phi + phi0)))
Do While n__2 - N0 - M >= 0.01
phi = (n__2 - N0 - M) / (a * f0) + phi
M = b * f0 * ((1 + n + (5 \ 4) * n * n + (5 \ 4) * n * n * n) * (phi - phi0) - (3 * n + 3 * n * n + (21 \ 8) * n * n * n) * Sin(phi - phi0) * Cos(phi + phi0) + ((15 \ 8) * n * n + (15 \ 8) * n * n * n) * Sin(2 * (phi - phi0)) * Cos(2 * (phi + phi0)) - (35 \ 24) * n * n * n * Sin(3 * (phi - phi0)) * Cos(3 * (phi + phi0)))
Loop
v = a * f0 * ((1 - e2 * Sin(phi) * Sin(phi)) ^ -0.5)
p = a * f0 * ((1 - e2 * Sin(phi) * Sin(phi) ^ -0.5)) * (1 - e2)
n2 = v / p - 1
vii = Tan(phi) / (2 * p * v)
viii = Tan(phi) / (24 * p * v * v * v) * (5 + 3 * Tan(phi) * Tan(phi) + n2 - 9 * Tan(phi) * Tan(phi) * n2)
ix = Tan(phi) / (720 * p * ((v) ^ 5)) * (61 + 90 * Tan(phi) * Tan(phi) + 45 * ((Tan(phi)) ^ 4))
x = (1 / Cos(phi)) / v
xi = (1 / Cos(phi)) / (6 * ((v) ^ 3)) * (v / p + 2 * ((Tan(phi) ^ 2)))
xii = (1 / Cos(phi)) / (120 * ((v) ^ 5)) * (5 + 28 * (Tan(phi) ^ 2) + 24 * (Tan(phi) ^ 4))
xiia = (1 / Cos(phi)) / (5040 * ((v) ^ 7)) * (61 + 662 * (Tan(phi) ^ 2) + 1320 * (Tan(phi) ^ 4)) + 720 * (Tan(phi) ^ 6)
e__3 = (E__1 - E0)
lon = (phi - vii * e__3 * e__3 + viii * e__3 * e__3 * e__3 * e__3 - ix * e__3 * e__3 * e__3 * e__3 * e__3 * e__3) * 180 / (4 * Atn(1))
lat = (lambda0 + x * e__3 - xi * e__3 * e__3 * e__3 + xii * e__3 * e__3 * e__3 * e__3 * e__3 - xiia * e__3 * e__3 * e__3 * e__3 * e__3 * e__3 * e__3) * 180 / (4 * Atn(1))
Select Case lonorlat
Case Is = "lon"
ConvertOSToLatLon = lon
Case Is = "lat"
ConvertOSToLatLon = lat
Case Else
ConvertOSToLatLon = "Choose Lon or Lat"
End Select
End Function