| 1 | #
|
|---|
| 2 | # wgs2tky.rb
|
|---|
| 3 | # -- GPS data converter
|
|---|
| 4 | #
|
|---|
| 5 | # kp<kp@mmho.no-ip.org>
|
|---|
| 6 | # Destributed under the GPL
|
|---|
| 7 |
|
|---|
| 8 | class Wgs2Tky
|
|---|
| 9 |
|
|---|
| 10 | Pi = Math::PI
|
|---|
| 11 | Rd = Pi/180
|
|---|
| 12 |
|
|---|
| 13 | # WGS84
|
|---|
| 14 | A = 6378137.0 # 赤道半径
|
|---|
| 15 | F = 1/298.257223563 # 扁平率
|
|---|
| 16 | E2 = F*2 - F*F # 第一離心率
|
|---|
| 17 |
|
|---|
| 18 | # Tokyo
|
|---|
| 19 | A_ = 6378137.0 - 739.845 # 6377397.155
|
|---|
| 20 | F_ = 1/298.257223563 - 0.000010037483
|
|---|
| 21 | # 1 / 299.152813
|
|---|
| 22 | E2_ = F_*2 - F_*F_
|
|---|
| 23 |
|
|---|
| 24 | Dx = +128
|
|---|
| 25 | Dy = -481
|
|---|
| 26 | Dz = -664
|
|---|
| 27 |
|
|---|
| 28 | def Wgs2Tky.conv!(lat,lon,h = 0)
|
|---|
| 29 | b = lat[0].to_f + lat[1].to_f/60 + lat[2].to_f/3600
|
|---|
| 30 | l = lon[0].to_f + lon[1].to_f/60 + lon[2].to_f/3600
|
|---|
| 31 |
|
|---|
| 32 | (x,y,z) = Wgs2Tky._llh2xyz(b,l,h,A,E2)
|
|---|
| 33 |
|
|---|
| 34 | x+=Dx
|
|---|
| 35 | y+=Dy
|
|---|
| 36 | z+=Dz
|
|---|
| 37 |
|
|---|
| 38 | (b,l,h) = Wgs2Tky._xyz2llh(x,y,z,A_,E2_)
|
|---|
| 39 |
|
|---|
| 40 | lat[0..2]=Wgs2Tky._deg2gdms(b)
|
|---|
| 41 | lon[0..2]=Wgs2Tky._deg2gdms(l)
|
|---|
| 42 | end
|
|---|
| 43 |
|
|---|
| 44 | private
|
|---|
| 45 |
|
|---|
| 46 | include Math
|
|---|
| 47 | extend Math
|
|---|
| 48 |
|
|---|
| 49 | def Wgs2Tky._llh2xyz(b,l,h,a,e2)
|
|---|
| 50 |
|
|---|
| 51 | b *= Rd
|
|---|
| 52 | l *= Rd
|
|---|
| 53 |
|
|---|
| 54 | sb = sin(b)
|
|---|
| 55 | cb = cos(b)
|
|---|
| 56 |
|
|---|
| 57 | rn = a / Math.sqrt(1-e2*sb*sb)
|
|---|
| 58 |
|
|---|
| 59 | x = (rn+h)*cb*cos(l)
|
|---|
| 60 | y = (rn+h)*cb*sin(l)
|
|---|
| 61 | z = (rn*(1-e2)+h) * sb
|
|---|
| 62 |
|
|---|
| 63 | return x,y,z
|
|---|
| 64 | end
|
|---|
| 65 |
|
|---|
| 66 | def Wgs2Tky._xyz2llh(x,y,z,a,e2)
|
|---|
| 67 |
|
|---|
| 68 | bda = sqrt(1-e2)
|
|---|
| 69 |
|
|---|
| 70 | po = sqrt(x*x+y*y)
|
|---|
| 71 | t = atan2(z,po*bda)
|
|---|
| 72 | st = sin(t)
|
|---|
| 73 | ct = cos(t)
|
|---|
| 74 | b = atan2(z+e2*a/bda*st*st*st,po-e2*a*ct*ct*ct)
|
|---|
| 75 | l = atan2(y,x)
|
|---|
| 76 |
|
|---|
| 77 | sb = sin(b)
|
|---|
| 78 | rn = a / sqrt(1-e2*sb*sb)
|
|---|
| 79 | h = po / cos(b) - rn
|
|---|
| 80 |
|
|---|
| 81 | return b/Rd,l/Rd,h
|
|---|
| 82 | end
|
|---|
| 83 |
|
|---|
| 84 | def Wgs2Tky._deg2gdms(deg)
|
|---|
| 85 | sf = deg*3600
|
|---|
| 86 | s = sf.to_i%60
|
|---|
| 87 | m = (sf/60).to_i%60
|
|---|
| 88 | d = (sf/3600).to_i
|
|---|
| 89 | s += sf - sf.to_i
|
|---|
| 90 | return d,m,s
|
|---|
| 91 | end
|
|---|
| 92 | end
|
|---|
| 93 |
|
|---|