CoordConv103 B Program converts between National Grid, Eastings & Northings : and Latitude & Longitude for areas of United Kingdom = Based on calculations given in Ordnance Survey Document ? "A guide to coordinate systems in Great Britain" [D00659] ; Uses a dialogue box to easily enable tabbed controls. < Using "winlib2B" prevents ESC leaving an empty window.   @lib$+"winlib2B"  @lib$+"winlib5"  ,dlg%=_newdialog("",0,0,270,190,10,2000) @dlg%!16=&508800C4 : remove title bar and make child window _text$="Enter values in one row of boxes and use the buttons to convert to another."+13+13 Ztext$+="The datum used is OSGB36 National Grid based on Airy 1830 biaxial ellipsoids." )_static(dlg%,text$,140,5,5,260,25,1) Xtext$="Note: Latitude and Longitude may also be entered as decimal degrees when"+13 Ztext$+="the minutes and seconds entries are ignored but should be left blank."+13+13 Itext$+="Formulae derived from OS document D00659 (v1.6 May 2006)"+13 4text$+="Version 1.03 by Geoff Gibson (Mar 2007)" +_static(dlg%,text$,151,5,140,260,40,1) >_static(dlg%,"National Grid Reference",141,10,41,80,10,1) -_editbox(dlg%,"",101,90,40,60,10,&20000) *_combobox(dlg%,"",102,160,39,40,10,3) 7_pushbutton(dlg%,"Convert down",103,50,58,50,15,0) 2_static(dlg%,"Easting (m)",142,20,81,40,10,1) (_editbox(dlg%,"",104,60,80,60,10,0) 4_static(dlg%,"Northing (m)",143,130,81,40,10,1) )_editbox(dlg%,"",105,170,80,60,10,0) 7_pushbutton(dlg%,"Convert down",106,50,98,50,15,0) 6_pushbutton(dlg%,"Convert up",107,160,58,50,15,0) /_static(dlg%,"Latitude",144,5,120,25,10,1) )_editbox(dlg%,"",108,30,119,30,10,2) (_static(dlg%,"d",145,60,120,5,10,1) )_editbox(dlg%,"",109,65,119,10,10,2) (_static(dlg%,"m",146,75,120,5,10,1) )_editbox(dlg%,"",110,80,119,25,10,2) 6_static(dlg%,"s Longitude",147,105,120,40,10,1) *_editbox(dlg%,"",111,145,119,30,10,2) )_static(dlg%,"d",147,175,120,5,10,1) *_editbox(dlg%,"",112,180,119,10,10,2) )_static(dlg%,"m",148,190,120,5,10,1) *_editbox(dlg%,"",113,195,119,25,10,2) /_static(dlg%,"s E/W",149,220,120,20,10,1) +_combobox(dlg%,"",114,240,118,20,10,3) 6_pushbutton(dlg%,"Convert up",115,160,98,50,15,0) _showdialog(dlg%)  ; "SendDlgItemMessage", !dlg%, 102, &143, 0, "6 figure" ; "SendDlgItemMessage", !dlg%, 102, &143, 0, "8 figure" < "SendDlgItemMessage", !dlg%, 102, &143, 0, "10 figure" 2 "SendDlgItemMessage", !dlg%, 102, &14E, 0, 0 4 "SendDlgItemMessage", !dlg%, 114, &143, 0, "E" 4 "SendDlgItemMessage", !dlg%, 114, &143, 0, "W" 2 "SendDlgItemMessage", !dlg%, 114, &14E, 0, 0   rc{l%,t%,r%,b%} " "GetWindowRect", !dlg%, rc{}  + "GetWindowLong", @hwnd%, -16 style% 5 "SetWindowLong", @hwnd%, -16, style% &50000 4 "AdjustWindowRect", rc{}, style% &50000, 0 D "SetWindowPos", @hwnd%, 0, 0, 0, rc.r%-rc.l%, rc.b%-rc.t%, 102 G "SetWindowText", @hwnd%,"Coordinate Converter for United Kingdom"  J "GetCurrentThreadId" ht1% : Thanks to Richard Russell 2 "GetWindowThreadProcessId", @hwnd%, 0 ht2% ( "AttachThreadInput", ht1%, ht2%, 1   _closedialog(dlg%): " _closedialog(dlg%):'$: click%=0 . click% = @wparam% &FFFF:convert :  *FLOAT 64 fl$="STNOHJ" #sl$="VWXYZQRSTULMNOPFGHJKABCDE"  A Constants defined for Airy 1830 ellipsoid used by OSGB36 NG 4a = 6377563.396 : semi-major axis (m) 4b = 6356256.910 : semi-minor axis (m)  F Constants defined for Transverse Mercator projections used in UK AF0 = 0.9996012717 : scale factor on central meridian 6phi0 = (49) : latitude of true origin 7lam0 = (-2) : longitude of true origin CE0 = 400000 : map coordinates of true origin (m) N0 = -100000  % Constants calculated from above esq = (a^2-b^2)/a^2 n = (a-b)/(a+b)   @ "GetFocus" hf% : Thanks to Richard Russell  hf% = @hwnd% _setfocus(hfocus%)   "GetParent", hf% hp%  hp% = !dlg% hfocus% = hf%  (1)=0    convert  click%  103: ng_en  106: en_ll  107: en_ng  115: ll_en  click%=0   ng_en gr$=gettext(101) E=0:N=0 ng2en(gr$)  E=-111 * (E<0 N<0 E>699999 N>1299999) outside  % "SetDlgItemText",!dlg%,104,(E) % "SetDlgItemText",!dlg%,105,(N)    en_ng gr$="" E=(gettext(104)) N=(gettext(105)) * (E<0 N<0 E>699999 N>1299999) outside  6 "SendDlgItemMessage",!dlg%, 102, &147, 0, 0 d% d%=6+2*d% en2ng(E,N,d%) $ "SetDlgItemText",!dlg%,101,gr$    ll_en lad$=gettext(108) lad=(lad$)  lad$,".")=0 lad+=(gettext(109))/60 lad+=(gettext(110))/3600  phi=(lad) lod$=gettext(111) lod=(lod$)  lod$,".")=0 lod+=(gettext(112))/60 lod+=(gettext(113))/3600  lam=(lod) 4 "SendDlgItemMessage",!dlg%,114,&147,0,0 sel%  sel%=1 lam =-lam ll2en(phi,lam) * (E<0 N<0 E>699999 N>1299999) outside  % "SetDlgItemText",!dlg%,104,(E) % "SetDlgItemText",!dlg%,105,(N)    en_ll E=(gettext(104)) N=(gettext(105)) * (E<0 N<0 E>699999 N>1299999) outside  en2ll(E,N) lat=(phi) dla%=(lat) lat=(lat-dla%)*60 mla%=(lat) !sla%=((lat-mla%)*600000+0.5) lat=sla%/10000 ( "SetDlgItemText",!dlg%,108,(dla%) ( "SetDlgItemText",!dlg%,109,(mla%) ' "SetDlgItemText",!dlg%,110,(lat) lon=((lam)) dlo%=(lon) lon=(lon-dlo%)*60 mlo%=(lon) !slo%=((lon-mlo%)*600000+0.5) lon=slo%/10000 ( "SetDlgItemText",!dlg%,111,(dlo%) ( "SetDlgItemText",!dlg%,112,(mlo%) ' "SetDlgItemText",!dlg%,113,(lon) lam<0 2 "SendDlgItemMessage", !dlg%, 114, &14E, 1, 0  2 "SendDlgItemMessage", !dlg%, 114, &14E, 0, 0      gettext(id%) text%  text% 63 - "GetDlgItemText", !dlg%, id%, text%, 63 = $$text%  I Converts National Grid Reference (as a string with 2 characters and M 2 to 10 figures) to Easting and Northing in metres (as real variables).  ng2en(g$) ? (g$)<4 (g$)2=1 illegal:E=-111: : flag error x%=((g$)-2)/2 y%=10^(5-x%) x$=((g$,1))&DF) y$=((g$,2,1))&DF) E+=(g$,3,x%))*y% N+=(g$,x%))*y% E+=(fl$,x$)-1)2*500000 N+=(fl$,x$)-1)2*500000 E+=(sl$,y$)-1)5*100000 N+=(sl$,y$)-1)5*100000   K Converts Easting and Northing values in metres (as real variables) to M National Grid Reference (as a string with 2 characters and d% figures).  en2ng(E,N,d%) e%=(E+0.5) n%=(N+0.5) gr$="" (gr$+=fl$,e%500000+n%500000*2+1,1) e%-=500000*(e%500000) n%-=500000*(n%500000) (gr$+=sl$,e%100000+n%100000*5+1,1) e%-=100000*(e%100000) n%-=100000*(n%100000) (e$="0000"+((e%/(10^(5-d%2))+0.5)) (n$="0000"+((n%/(10^(5-d%2))+0.5)) gr$+=e$,d%2) gr$+=n$,d%2)   O Converts Latitude and Longitude in radians (as real variables, phi & lam) C to Easting and Northing in metres (as real variables, E & N).  ll2en(phi,lam) !nu = a*F0/(1-esq*((phi))^2) 'rho = nu*(1-esq)/(1-esq*((phi))^2) etasq = nu/rho-1 4M = b*F0*((1+n+(5/4)*n^2+(5/4)*n^3)*(phi-phi0) \ >\ -(3*n+3*n^2+(21/8)*n^3)*(phi-phi0)*(phi+phi0) \ G\ +((15/8)*n^2+(15/8)*n^3)*(2*(phi-phi0))*(2*(phi+phi0)) \ :\ -(35/24)*n^3*(3*(phi-phi0))*(3*(phi+phi0))) I = M+N0 II = (nu/2)*(phi)*(phi) :III = (nu/24)*(phi)*((phi)^3)*(5-((phi)^2)+9*etasq) CIIIA = (nu/720)*(phi)*((phi)^5)*(61-58*((phi)^2)+((phi)^4)) IV = nu*(phi) /V = (nu/6)*((phi)^3)*((nu/rho)-((phi)^2)) VVI = (nu/120)*((phi)^5)*(5-18*((phi)^2)+((phi)^4)+14*etasq-58*((phi)^2)*etasq) <N = I+II*(lam-lam0)^2+III*(lam-lam0)^4+IIIA*(lam-lam0)^6 7E = E0+IV*(lam-lam0)+V*(lam-lam0)^3+VI*(lam-lam0)^5   H Converts Easting and Northing values in metres (as real variables) J to Latitude and Longitude in radians (as real variables, phi & lam).  en2ll(E,N) M = 0 phid = phi0  phid = (N-N0-M)/(a*F0)+phid 5M = b*F0*((1+n+(5/4)*n^2+(5/4)*n^3)*(phid-phi0) \ @\ -(3*n+3*n^2+(21/8)*n^3)*(phid-phi0)*(phid+phi0) \ I\ +((15/8)*n^2+(15/8)*n^3)*(2*(phid-phi0))*(2*(phid+phi0)) \ <\ -(35/24)*n^3*(3*(phid-phi0))*(3*(phid+phi0))) (N-N0-M)<0.00001 "nu = a*F0/(1-esq*((phid))^2) (rho = nu*(1-esq)/(1-esq*((phid))^2) etasq = nu/rho-1 VII = (phid)/(2*rho*nu) LVIII = (phid)/(24*rho*nu^3)*(5+3*((phid)^2)+etasq-9*((phid)^2)*etasq) BIX = (phid)/(720*rho*nu^5)*(61+90*((phid)^2)+45*((phid)^4)) X = 1/((phid)*nu) 0XI = (nu/rho+2*((phid)^2))/((phid)*6*nu^3) >XII = (5+28*((phid)^2)+24*((phid)^4))/((phid)*120*nu^5) TXIIA = (61+662*((phid)^2)+1320*((phid)^4)+720*((phid)^6))/((phid)*5040*nu^7) 5phi = phid-VII*(E-E0)^2+VIII*(E-E0)^4-IX*(E-E0)^6 >lam = lam0+X*(E-E0)-XI*(E-E0)^3+XII*(E-E0)^5-XIIA*(E-E0)^7    illegal I "MessageBox",@hwnd%,"Must have 2, 4, 6, 8 or 10 figures","Error",48    outside 6 "MessageBox",@hwnd%,"Outside UK area","Error",48