Created
May 22, 2010 14:59
-
-
Save thomasfl/410128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def Math.pow(x,y) | |
| x**y | |
| end | |
| ## Converts from utm to lat_lng. Default zone is the one in which | |
| ## Oslo and Akershus falls. | |
| def utm_to_lat_lng(easting, northing, zone=32, northernHemisphere=true) | |
| if not northernHemisphere then | |
| northing = 10000000 - northing | |
| end | |
| a = 6378137 | |
| e = 0.081819191 | |
| e1sq = 0.006739497 | |
| k0 = 0.9996 | |
| arc = northing / k0 | |
| mu = arc / (a * (1 - Math.pow(e, 2) / 4.0 - 3 * Math.pow(e, 4) / 64.0 - 5 * Math.pow(e, 6) / 256.0)) | |
| ei = (1 - Math.pow((1 - e * e), (1 / 2.0))) / (1 + Math.pow((1 - e * e), (1 / 2.0))) | |
| ca = 3 * ei / 2 - 27 * Math.pow(ei, 3) / 32.0 | |
| cb = 21 * Math.pow(ei, 2) / 16 - 55 * Math.pow(ei, 4) / 32 | |
| cc = 151 * Math.pow(ei, 3) / 96 | |
| cd = 1097 * Math.pow(ei, 4) / 512 | |
| phi1 = mu + ca * Math.sin(2 * mu) + cb * Math.sin(4 * mu) + cc * Math.sin(6 * mu) + cd * Math.sin(8 * mu) | |
| n0 = a / Math.pow((1 - Math.pow((e * Math.sin(phi1)), 2)), (1 / 2.0)) | |
| r0 = a * (1 - e * e) / Math.pow((1 - Math.pow((e * Math.sin(phi1)), 2)), (3 / 2.0)) | |
| fact1 = n0 * Math.tan(phi1) / r0 | |
| _a1 = 500000 - easting | |
| dd0 = _a1 / (n0 * k0) | |
| fact2 = dd0 * dd0 / 2 | |
| t0 = Math.pow(Math.tan(phi1), 2) | |
| q0 = e1sq * Math.pow(Math.cos(phi1), 2) | |
| fact3 = (5 + 3 * t0 + 10 * q0 - 4 * q0 * q0 - 9 * e1sq) * Math.pow(dd0, 4) / 24 | |
| fact4 = (61 + 90 * t0 + 298 * q0 + 45 * t0 * t0 - 252 * e1sq - 3 * q0 * q0) * Math.pow(dd0, 6) / 720 | |
| lof1 = _a1 / (n0 * k0) | |
| lof2 = (1 + 2 * t0 + q0) * Math.pow(dd0, 3) / 6.0 | |
| lof3 = (5 - 2 * q0 + 28 * t0 - 3 * Math.pow(q0, 2) + 8 * e1sq + 24 * Math.pow(t0, 2)) * Math.pow(dd0, 5) / 120 | |
| _a2 = (lof1 - lof2 + lof3) / Math.cos(phi1) | |
| _a3 = _a2 * 180 / Math::PI | |
| latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / Math::PI | |
| if not northernHemisphere then | |
| latitude = -latitude | |
| end | |
| longitude = ((zone > 0) and (6 * zone - 183.0) or 3.0) - _a3 | |
| return [latitude, longitude] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment