UTM.to.Lat_Long<-function(coord){
b=6356752.3142
a=6378137
e=0.0818191909
eisq=0.0067394968
k0=0.9996
ei=(1-(1-e*e)^(1/2))/(1+(1-e*e)^(1/2))
C1=3*ei/2-27*ei^3/32
C2=21*ei^2/16-55*ei^4/32
C3=151*ei^3/96
C4=1097*ei^4/512
polo=coord[,1]
zone=coord[,2]
lon=coord[,3]
lat=coord[,4]
Corrected.Northing=ifelse(polo=="N",lat,10000000-lat)
East.Prime=500000-lon
Arc.Length=lat/k0
mu=Arc.Length/(a*(1-e^2/4-3*e^4/64-5*e^6/256))
phl=mu+C1*sin(2*mu)+C2*sin(4*mu)+C3*sin(6*mu)+C4*sin(8*mu)
C=eisq*cos(phl)^2
T=tan(phl)^2
N=a/(1-(e*sin(phl))^2)^(1/2)
R=a*(1-e*e)/(1-(e*sin(phl))^2)^(3/2)
D=East.Prime/(N*k0)
F1=N*tan(phl)/R
F2=D*D/2
F3=(5+3*T+10*C-4*C*C-9*eisq)*D^4/24
F4=(61+90*T+298*C+45*T*T-252*eisq-3*C*C)*D^6/720
LF1=D
LF2=(1+2*T+C)*D^3/6
LF3=(5-2*C+28*T-3*C^2+8*eisq+24*T^2)*D^5/120
delta.lon=(LF1-LF2+LF3)/cos(phl)
raw.lat=180*(phl-F1*(F2+F3+F4))/pi
LAT=ifelse(polo=="N",raw.lat,-1*raw.lat)
LON=zone.cm-delta.lon*180/pi
lon.deg=as.integer(LON)
lon.min=abs(as.integer(60*(LON-lon.deg)))
lon.sec=3600*(abs(LON)-abs(lon.deg)-lon.min/60)
lat.deg=as.integer(LAT)
lat.min=abs(as.integer(60*(LAT-lat.deg)))
lat.sec=3600*(abs(LAT)-abs(lat.deg)-lat.min/60)
df=data.frame(coord,LON,LAT,lon.deg,lon.min,lon.sec,lat.deg,lat.min,lat.sec)
return(df)
}
coord=data.frame(polo=sample(c("N","S"),100,r=T),zone=sample(0:23,100,r=T),lon=sample(0:90000,100,r=T),lat=sample (0:90000,100,r=T))
UTM.to.Lat_Long(coord)